Ch 17 Stacks Queues
-
Upload
timothy-wood -
Category
Documents
-
view
236 -
download
1
Transcript of Ch 17 Stacks Queues
![Page 1: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/1.jpg)
Chris Kiekintveld CS 2401 (Fall 2010)
Elementary Data Structures and Algorithms
Stacks and Queues
![Page 2: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/2.jpg)
Two New ADTs
Define two new abstract data types Both are restricted lists Can be implemented using arrays or linked lists
Stacks “Last In First Out” (LIFO)
Queues “First In First Out” (FIFO)
Java Programming: Program Design Including Data Structures 2
![Page 3: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/3.jpg)
Java Programming: Program Design Including Data Structures 3
Stacks
List of the same kind of elements Addition and deletion of elements occur only at one
end, called the top of the stack
Computers use stacks to implement method calls Stacks are also used to convert recursive algorithms
into nonrecursive algorithms
![Page 4: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/4.jpg)
Java Programming: Program Design Including Data Structures 4
Conceptual Stacks
Figure 17-1 Various types of stacks
![Page 5: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/5.jpg)
Java Programming: Program Design Including Data Structures 5
Stacks (continued)
Stacks are also called Last Input First Output (LIFO) data structures
Operations performed on stacks Push: adds an element to the stack Pop: removes an element from the stack Peek: looks at the top element of the stack
![Page 6: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/6.jpg)
Java Programming: Program Design Including Data Structures 6
Stacks (continued)
Figure 17-3 Stack operations
![Page 7: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/7.jpg)
Java Programming: Program Design Including Data Structures 7
Stacks (continued)
Figure 17-4 UML diagram of the interface StackADT
![Page 8: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/8.jpg)
Java Programming: Program Design Including Data Structures 8
StackException Class
Adding an element to a full stack and removing an element from an empty stack would generate errors or exceptions Stack overflow exception Stack underflow exception
Classes that handle these exceptions StackException extends RunTimeException StackOverflowException extends StackException StackUnderflowException extends StackException
![Page 9: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/9.jpg)
Java Programming: Program Design Including Data Structures 9
Implementation of Stacks as Arrays
The array implementing a stack is an array of reference variables
Each element of the stack can be assigned to an array slot
The top of the stack is the index of the last element added to the stack
To keep track of the top position, declare a variable called stackTop
![Page 10: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/10.jpg)
Java Programming: Program Design Including Data Structures 10
Implementation of Stacks as Arrays (continued)
Figure 17-6 Example of a stack
![Page 11: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/11.jpg)
Java Programming: Program Design Including Data Structures 11
Constructors
Default constructor public StackClass()
{
maxStackSize = 100;
stackTop = 0; //set stackTop to 0
list = (T[]) new Object[maxStackSize]; //create the array
}//end default constructor
![Page 12: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/12.jpg)
Java Programming: Program Design Including Data Structures 12
Initialize Stack
Method initializeStack public void initializeStack()
{
for (int i = 0; i < stackTop; i++)
list[i] = null;
stackTop = 0;
}//end initializeStack
![Page 13: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/13.jpg)
Java Programming: Program Design Including Data Structures 13
Empty Stack
Method isEmptyStack public boolean isEmptyStack()
{
return (stackTop == 0);
}//end isEmptyStack
![Page 14: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/14.jpg)
Java Programming: Program Design Including Data Structures 14
Full Stack
Method isFullStack public boolean isFullStack()
{
return (stackTop == maxStackSize);
}//end isFullStack
![Page 15: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/15.jpg)
Java Programming: Program Design Including Data Structures 15
Push
Method push public void push(T newItem) throws StackOverflowException
{
if (isFullStack())
throw new StackOverflowException();
list[stackTop] = newItem; //add newItem at the
//top of the stack
stackTop++; //increment stackTop
}//end push
![Page 16: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/16.jpg)
Java Programming: Program Design Including Data Structures 16
Peek
Method peek public T peek() throws StackUnderflowException
{
if (isEmptyStack())
throw new StackUnderflowException();
return (T) list[stackTop - 1];
}//end peek
![Page 17: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/17.jpg)
Java Programming: Program Design Including Data Structures 17
Pop
Method pop public void pop() throws StackUnderflowException
{
if (isEmptyStack())
throw new StackUnderflowException();
stackTop--; //decrement stackTop
list[stackTop] = null;
}//end pop
![Page 18: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/18.jpg)
Java Programming: Program Design Including Data Structures 18
Linked Implementation of Stacks Arrays have fixed sizes
Only a fixed number of elements can be pushed onto the stack
Dynamically allocate memory using reference variables Implement a stack dynamically
Similar to the array representation, stackTop is used to locate the top element stackTop is now a reference variable
![Page 19: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/19.jpg)
Java Programming: Program Design Including Data Structures 19
Linked Implementation of Stacks (continued)
Figure 17-13 Nonempty linked stack
![Page 20: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/20.jpg)
Java Programming: Program Design Including Data Structures 20
Default Constructor
Default constructor public LinkedStackClass()
{
stackTop = null;
}//end constructor
![Page 21: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/21.jpg)
Java Programming: Program Design Including Data Structures 21
Initialize Stack
Method initializeStack public void initializeStack()
{
stackTop = null;
}//end initializeStack
![Page 22: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/22.jpg)
Java Programming: Program Design Including Data Structures 22
Empty Stack and Full Stack
Methods isEmptyStack and isFullStack public boolean isEmptyStack()
{
return (stackTop == null);
}//end isEmptyStack
public boolean isFullStack()
{
return false;
}//end isFullStack
![Page 23: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/23.jpg)
Java Programming: Program Design Including Data Structures 23
Push
Method push public void push(T newElement)
{
StackNode<T> newNode; //reference variable to create
//the new node
newNode = new
StackNode<T>(newElement, stackTop); //create
//newNode and insert
//before stackTop
stackTop = newNode; //set stackTop to point to
//the top element
} //end push
![Page 24: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/24.jpg)
Java Programming: Program Design Including Data Structures 24
Peek
Method peek public T peek() throws StackUnderflowException
{
if (stackTop == null)
throw new StackUnderflowException();
return stackTop.info;
}//end top
![Page 25: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/25.jpg)
Java Programming: Program Design Including Data Structures 25
Pop
Method pop public void pop() throws StackUnderflowException
{
if (stackTop == null)
throw new StackUnderflowException();
stackTop = stackTop.link; //advance stackTop to the
//next node
}//end pop
![Page 26: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/26.jpg)
Java Programming: Program Design Including Data Structures 26
Applications of Stacks: Postfix Expression Calculator
Infix notation The operator is written between the operands
Prefix or Polish notation Operators are written before the operands Does not require parentheses
Reverse Polish or postfix notation Operators follow the operands Has the advantage that the operators appear in the
order required for computation
![Page 27: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/27.jpg)
Java Programming: Program Design Including Data Structures 27
Applications of Stacks: Postfix Expression Calculator (continued)
Table 17-1 Infix expressions and their equivalent postfix expressions
![Page 28: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/28.jpg)
Java Programming: Program Design Including Data Structures 28
Applications of Stacks: Postfix Expression Calculator (continued)
Algorithm to evaluate postfix expressions Scan the expression from left to right When an operator is found, back up to get the
required number of operands Perform the operation Continue processing the expression
![Page 29: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/29.jpg)
Java Programming: Program Design Including Data Structures 29
Main Algorithm Main algorithm in pseudocode for processing a
postfix expression Get the next expression while more data to process { a. initialize the stack b. process the expression c. output result d. get the next expression }
![Page 30: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/30.jpg)
Java Programming: Program Design Including Data Structures 30
Method evaluateExpression General algorithm for evaluateExpression get the next token while (token != '=') { if (token is a number) { output number push number into stack } else { token is an operation call method evaluateOpr to evaluate the operation } if (no error in the expression) get next token else discard the expression }
![Page 31: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/31.jpg)
Java Programming: Program Design Including Data Structures 31
Method evaluateOpr This method evaluates an operation Two operands are needed to evaluate an operation Operands are stored in the stack
The stack must contain at least two operands Otherwise, the operation cannot be evaluated
![Page 32: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/32.jpg)
Java Programming: Program Design Including Data Structures 32
Removing Recursion: Nonrecursive Algorithm to Print a Linked List
Backward Naïve approach
Get the last node of the list and print it Traverse the link starting at the first node
Repeat this process for every node Traverse the link starting at the first node until you
reach the desired node Very inefficient
![Page 33: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/33.jpg)
Java Programming: Program Design Including Data Structures 33
Removing Recursion: Nonrecursive Algorithm to Print a Linked List
Backward (continued) current = first; //Line 1
while (current != null) //Line 2 {
stack.push(current); //Line 3
current = current.link; //Line 4
}
![Page 34: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/34.jpg)
Java Programming: Program Design Including Data Structures 34
Removing Recursion: Nonrecursive Algorithm to Print a Linked List
Backward (continued)
Figure 17-36 List and stack after the staments stack.push(current); and current = current.link; executes
![Page 35: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/35.jpg)
Java Programming: Program Design Including Data Structures 35
The class Stack
Table 17-2 Members of the class Stack
![Page 36: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/36.jpg)
Java Programming: Program Design Including Data Structures 36
Queues Data structure in which the elements are added at
one end, called the rear, and deleted from the other end, called the front
A queue is a First In First Out data structure As in a stack, the middle elements of the queue are
inaccessible
![Page 37: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/37.jpg)
Java Programming: Program Design Including Data Structures 37
Queue Operations Queue operations
initializeQueue isEmptyQueue isFullQueue front back addQueue deleteQueue
![Page 38: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/38.jpg)
Java Programming: Program Design Including Data Structures 38
QueueException Class Adding an element to a full queue and removing an
element from an empty queue would generate errors or exceptions Queue overflow exception Queue underflow exception
Classes that handle these exceptions QueueException extends RunTimeException QueueOverflowException extends QueueException QueueUnderflowException extends QueueException
![Page 39: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/39.jpg)
Java Programming: Program Design Including Data Structures 39
Implementation of Queues as Arrays
Instance variables An array to store the queue elements queueFront: keeps track of the first element queueRear: keeps track of the last element maxQueueSize: specifies the maximum size of the
queues
![Page 40: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/40.jpg)
Java Programming: Program Design Including Data Structures 40
Implementation of Queues as Arrays (continued)
Figure 17-42 Queue after two more addQueue operations
![Page 41: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/41.jpg)
Java Programming: Program Design Including Data Structures 41
Implementation of Queues as Arrays (continued)
Figure 17-43 Queue after the deleteQueue operation
![Page 42: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/42.jpg)
Java Programming: Program Design Including Data Structures 42
Implementation of Queues as Arrays (continued)
Problems with this implementation Arrays have fixed sizes After various insertion and deletion operations, queueRear will point to the last array position Giving the impression that the queue is full
Solutions Slide all of the queue elements toward the first array
position Use a circular array
![Page 43: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/43.jpg)
Java Programming: Program Design Including Data Structures 43
Implementation of Queues as Arrays (continued)
Figure 17-45 Circular queue
![Page 44: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/44.jpg)
Java Programming: Program Design Including Data Structures 44
Implementation of Queues as Arrays (continued)
Figure 17-46 Queue with two elements at positions 98 and 99
![Page 45: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/45.jpg)
Java Programming: Program Design Including Data Structures 45
Implementation of Queues as Arrays (continued)
Figure 17-47 Queue after one more addQueue operation
![Page 46: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/46.jpg)
Java Programming: Program Design Including Data Structures 46
Constructors Default constructor //Default constructor
public QueueClass()
{
maxQueueSize = 100;
queueFront = 0; //initialize queueFront
queueRear = maxQueueSize - 1; //initialize queueRear
count = 0;
list = (T[]) new Object[maxQueueSize]; //create the
//array to implement the queue
}
![Page 47: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/47.jpg)
Java Programming: Program Design Including Data Structures 47
initializeQueue Method initilializeQueue public void initializeQueue()
{
for (int i = queueFront; i < queueRear;
i = (i + 1) % maxQueueSize)
list[i] = null;
queueFront = 0;
queueRear = maxQueueSize - 1;
count = 0;
}
![Page 48: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/48.jpg)
Java Programming: Program Design Including Data Structures 48
Empty Queue and Full Queue Methods isEmptyQueue and isFullQueue public boolean isEmptyQueue()
{
return (count == 0);
}
public boolean isFullQueue()
{
return (count == maxQueueSize);
}
![Page 49: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/49.jpg)
Java Programming: Program Design Including Data Structures 49
Front Method front public T front() throws QueueUnderflowException
{
if (isEmptyQueue())
throw new QueueUnderflowException();
return (T) list[queueFront];
}
![Page 50: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/50.jpg)
Java Programming: Program Design Including Data Structures 50
Back Method back public T back() throws QueueUnderflowException
{
if (isEmptyQueue())
throw new QueueUnderflowException();
return (T) list[queueRear];
}
![Page 51: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/51.jpg)
Java Programming: Program Design Including Data Structures 51
addQueue Method addQueue public void addQueue(T queueElement)
throws QueueOverflowException
{
if (isFullQueue())
throw new QueueOverflowException();
queueRear = (queueRear + 1) % maxQueueSize; //use the
//mod operator to advance queueRear
//because the array is circular
count++;
list[queueRear] = queueElement;
}
![Page 52: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/52.jpg)
Java Programming: Program Design Including Data Structures 52
deleteQueue Method deleteQueue public void deleteQueue() throws QueueUnderflowException
{
if (isEmptyQueue())
throw new QueueUnderflowException();
count--;
list[queueFront] = null;
queueFront = (queueFront + 1) % maxQueueSize; //use the
//mod operator to advance queueFront
//because the array is circular
}
![Page 53: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/53.jpg)
Java Programming: Program Design Including Data Structures 53
Linked Implementation of Queues
Simplifies many of the special cases of the array implementation
Because the memory to store a queue element is allocated dynamically, the queue is never full
Class LinkedQueueClass implements a queue as a linked data structure It uses nodes of type QueueNode
![Page 54: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/54.jpg)
Java Programming: Program Design Including Data Structures 54
Linked Implementation of Queues (continued)
Method initializeQueue public void initializeQueue()
{ queueFront = null;
queueRear = null;
}
![Page 55: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/55.jpg)
Java Programming: Program Design Including Data Structures 55
Linked Implementation of Queues (continued)
Methods isEmptyQueue and isFullQueue public boolean isEmptyQueue()
{ return (queueFront == null);
}
public boolean isFullQueue()
{
return false;
}
![Page 56: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/56.jpg)
Java Programming: Program Design Including Data Structures 56
addQueue
Method addQueue public void addQueue(T newElement) { QueueNode<T> newNode; newNode = new QueueNode<T>(newElement, null); //create
//newNode and assign newElement to newNode if (queueFront == null) //if initially the queue is empty {
queueFront = newNode; queueRear = newNode; } else //add newNode at the end
{ queueRear.link = newNode; queueRear = queueRear.link;
} }//end addQueue
![Page 57: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/57.jpg)
Java Programming: Program Design Including Data Structures 57
front and back
Methods front and back public T front() throws QueueUnderflowException
{
if (isEmptyQueue())
throw new QueueUnderflowException();
return queueFront.info;
}
public T back() throws QueueUnderflowException
{
if (isEmptyQueue())
throw new QueueUnderflowException();
return queueRear.info;
}
![Page 58: Ch 17 Stacks Queues](https://reader031.fdocuments.net/reader031/viewer/2022021421/577cca081a28aba711a530c6/html5/thumbnails/58.jpg)
Java Programming: Program Design Including Data Structures 58
deleteQueue
Method deleteQueue public void deleteQueue() throws QueueUnderflowException
{
if (isEmptyQueue())
throw new QueueUnderflowException();
queueFront = queueFront.link; //advance queueFront
if (queueFront == null) //if after deletion the queue
queueRear = null; //is empty, set queueRear to null
} //end deleteQueue