Stack and Queue
Stack and Queue
Stacks
Stacks are a straight forward and simple data structure Access is allowed only at one point of the structure, normally termed the top of the stack, so the operations are limited:
push (add item to stack) pop (remove top item from stack) top (get top item without removing it) clear isEmpty size
2
Stack Operations
Assume a simple stack for integers. Stack s = new Stack(); s.push(12); s.push(4); s.push( s.top() + 2 ); s.pop() s.push( s.top() ); //what are contents of stack?
3
Stack Operations
Write a method to print out contents of stack in reverse order.
Corrected Version
Stack s = new Stack(); // put stuff in stack for(int i = 0; i < 7; i++) s.push( i ); // print out contents of stack // while emptying it int limit = s.size(); for(int i = 0; i < limit; i++) System.out.println( s.pop() ); //or // while( !s.isEmpty() ) // System.out.println( s.pop() );
6
Algorithm?
7
At the end of the file if the stack is not empty report an error
8
Algorithm in practice
list[i] = 3 * ( 44 - method( foo( list[ 2 * (i + 1) + foo( list[i - 1] ) ) / 2 *) - list[ method(list[0])]; Processing a file
Tokenization: the process of scanning an input stream. Each independent chunk is a token.
Mathematical Calculations
What is 3 + 2 * 4? 2 * 4 + 3? 3 * 2 + 4? The precedence of operators affects the order of operations. A mathematical expression cannot simply be evaluated left to right. A challenge when evaluating a program. Lexical analysis is the process of interpreting a program. Involves Tokenization What about 1 - 2 - 4 ^ 5 * 3 * 6 / 7 ^ 2 ^ 2
10
when the expression has been exhausted the result is the top (and only element) of the stack
12
Infix to Postfix
Convert the following equations from infix to postfix:
2^3^3+5*1 233^^51*+ 11 + 2 - 1 * 3 / 3 + 2 ^ 2 / 3 11 2 + 1 3 * 3 / - 2 2 ^ 3 / + Problems: parentheses in expression
13
Operands: add to expression Close parenthesis: pop stack symbols until an open parenthesis appears Operators: Pop all stack symbols until a symbol of lower precedence appears. Then push the operator End of input: Pop all remaining stack symbols and add to the expression
14
Simple Example
Infix Expression: PostFix Expression: Operator Stack: 3+2*4
15
Simple Example
Infix Expression: PostFix Expression: Operator Stack: +2*4 3
16
Simple Example
Infix Expression: PostFix Expression: Operator Stack: 2*4 3 +
17
Simple Example
Infix Expression: PostFix Expression: Operator Stack: *4 32 +
18
Simple Example
Infix Expression: PostFix Expression: Operator Stack: 4 32 +*
19
Simple Example
Infix Expression: PostFix Expression: Operator Stack: 324 +*
20
Simple Example
Infix Expression: PostFix Expression: Operator Stack: 324* +
21
Simple Example
Infix Expression: PostFix Expression: Operator Stack: 324*+
22
Example
1-2^3^3-(4+5*6)*7 Show algorithm in action on above equation
23
Applications of Stacks
Direct applications
Page-visited history in a Web browser Undo sequence in a text editor Chain of method calls in the Java Virtual Machine Validate XML
Indirect applications
Auxiliary data structure for algorithms Component of other data structures
24
foo(int j) { int k; Local variables and return value Program counter, keeping track of k = j+1; the statement being executed bar(k); When a method ends, its frame } is popped from the stack and control is passed to the method bar(int m) { on top of the stack } Allows for recursion
Implementing a stack
need an underlying collection to hold the elements of the stack 2 basic choices
array (native or ArrayList) linked list
array implementation linked list implementation Some of the uses for a stack are much more interesting than the implementation of a stack
26
Array-based Stack
A simple way of implementing the Stack ADT uses an array We add elements from left to right A variable keeps track of the index of the top element S 0 1 2 Algorithm size() return t + 1 Algorithm pop() if isEmpty() then throw EmptyStackException else tnt1 return S[t + 1] t
27
Queues
28
Queues
Closely related to Stacks Like a line
In Britain people dont get in line they queue up.
Add items to the end of the queue Access and remove from the front Used extensively in operating systems
Queues of processes, I/O requests, and much more
29
Queue operations
add(Object item)
a.k.a. enqueue(Object item)
Object get()
a.k.a. Object front()
Object remove()
a.k.a. Object dequeue()
Queue Example
Operation enqueue(5) enqueue(3) dequeue() enqueue(7) dequeue() front() dequeue() dequeue() isEmpty() enqueue(9) enqueue(7) size() enqueue(3) enqueue(5) dequeue() Output Q 5 3 7 7 error true 2 9 (5) (5, 3) (3) (3, 7) (7) (7) () () () (9) (9, 7) (9, 7) (9, 7, 3) (9, 7, 3, 5) (7, 3, 5)
31
Applications of Queues
Direct applications
Waiting lists, bureaucracy Access to shared resources (e.g., printer) Multiprogramming
Indirect applications
Auxiliary data structure for algorithms Component of other data structures
32
Queue Operations
We use the modulo operator (remainder of division)
Q 0 1 2 Q 0 1 2 r f
34
Q 0 1 2 Q 0 1 2 r f
36
37
38
Implementing a Queue
Given the internal storage container and choice for front and back of queue what are the Big O of the queue operations?
ArrayList enqueue front dequeue isEmpty LinkedList (Singly Linked) LinkedList (Doubly Linked)
39
Performance:
insert takes O(1) time since we can insert the item at the beginning or end of the sequence removeMin and min take O(n) time since we have to traverse the entire sequence to find the smallest key
Performance:
insert takes O(n) time since we have to find the place where to insert the item removeMin and min take O(1) time, since the smallest key is at the beginning
40
41