Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stacks and Queues CSE 373 Data Structures.

Similar presentations


Presentation on theme: "Stacks and Queues CSE 373 Data Structures."— Presentation transcript:

1 Stacks and Queues CSE 373 Data Structures

2 Readings Reading Goodrich and Tamassia, Chapter 4 4/15/2019
Stacks and Queues

3 Stack ADT A list for which Insert and Delete are allowed only at one end of the list (the top) the implementation defines which end is the "top" LIFO – Last in, First out Push: Insert element at top Pop: Remove and return top element (aka TopAndPop) IsEmpty: test for emptiness a tray stack 4/15/2019 Stacks and Queues

4 An Application of Stacks
Parsing phase in compilers Traversal of the tree in postorder yields the reverse Polish (postfix) notation: ab+c*d+ No parentheses needed! A stack algorithm makes evaluation easy parse tree d + (a+b)*c+d c * a b 4/15/2019 Stacks and Queues

5 Stack Evaluation of Postfix
Initialize an empty stack Read one “token” at a time from the input If token is data (operand), push it on the stack If token is operator: Pop two elements: P1 and then P2. Apply the current operand: P2 op P1 Push the result on the stack Continue to end of input, then examine the stack If it has one element – that is the final answer If it is empty or has > 1 element – the expression was illegal 4/15/2019 Stacks and Queues

6 Another Stack Application: () Matching
Legal: () (()()(())) Not legal: (() )(() Read one “token” at a time from the input If token is a left symbol, push it on the stack If token is a right symbol if stack is empty, an error has occurred – stop. otherwise pop (and discard) one (left) symbol from the stack Continue to end of input, then examine the stack If it is empty – expression is good If it not empty – expression was bad 4/15/2019 Stacks and Queues

7 Read one “token” at a time from the input
If token is a left symbol, push it on the stack If token is a right symbol if stack is empty, an error has occurred – stop. otherwise pop (and discard) one (left) symbol from the stack Continue to end of input, then examine the stack If it is empty – expression is good If it not empty – expression was bad 4/15/2019 Stacks and Queues

8 Beyond () Matching Expressions with other tokens
(add (sub 3 60) (add 5 x)) Expressions with multiple types of brackets: { ( ( ) [] ()) { () } } Expressions with non-distinctive element pairs “ ‘ ‘ “ 4/15/2019 Stacks and Queues

9 XML Parsing XML requires strict nested pairing of tags
<student><name>Jenny</name> <scholarship>$10,000</scholarship> </student> See textbook for detailed code of stack-based XML checking 4/15/2019 Stacks and Queues

10 Another Important Application of Stacks
Call stack in run time systems When a function (method, procedure) is called the work area (local variables, copies of parameters, return location in code) for the new function is pushed on to the stack. When the function returns, the stack is popped. So, calling a recursive procedure with a depth of N requires O(N) memory space. 4/15/2019 Stacks and Queues

11 Two Basic Implementations of Stacks
Linked List Push is InsertFront Pop is DeleteFront (Top is “access” the element at the top of the stack) IsEmpty is test for null Array The k items in the stack are the first k items in the array. 4/15/2019 Stacks and Queues

12 Linked List Implementation
Stack of blobs null node a blob Pointer to next node Pointer to blob 4/15/2019 Stacks and Queues

13 Array Implementation Stack of blobs A bottom top
4 12 holder = blob pointer array size = number in stack maxsize = max size of stack 4/15/2019 Stacks and Queues

14 Push and Pop (array impl.)
IsEmpty(A : blobstack pointer) : boolean { return A.size = 0 } IsFull(A : blobstack pointer) : boolean { return A.size = A.maxsize; Pop(A : blobstack pointer) : blob pointer { // Precondition: A is not empty // A.size := A.size – 1; return A.holder[A.size + 1]; Push(A : blobstack pointer, p : blob pointer): { // precondition: A is not full// A.size := A.size + 1; A.holder[A.size] := p; 4/15/2019 Stacks and Queues

15 Linked Lists vs Array Linked list implementation Array Implementation
+ flexible – size of stack can be anything + constant time per operation - Call to memory allocator can be costly Array Implementation + Memory pre-allocated + constant time per operation. - Not all allocated memory is used Overflow possible - Resizing can be used but some ops will be more than constant time. 4/15/2019 Stacks and Queues

16 Queue Insert at one end of List, remove at the other end
Queues are “FIFO” – first in, first out Primary operations are Enqueue and Dequeue A queue ensures “fairness” customers waiting on a customer hotline processes waiting to run on the CPU 4/15/2019 Stacks and Queues

17 Queue ADT Operations: Enqueue - add an entry at the end of the queue (also called “rear” or “tail”) Dequeue - remove the entry from the front of the queue IsEmpty IsFull may be needed 4/15/2019 Stacks and Queues

18 A Sample of Applications of Queues
File servers: Users needing access to their files on a shared file server machine are given access on a FIFO basis Printer Queue: Jobs submitted to a printer are printed in order of arrival Phone calls made to customer service hotlines are usually placed in a queue 4/15/2019 Stacks and Queues

19 Pointer Implementation
Q front rear null Header Not always there front rear 4/15/2019 Stacks and Queues

20 List Implementation IsEmpty(Q : blobqueue pointer) : boolean {
return Q.front = Q.rear } Dequeue(Q : blobqueue pointer) : blob pointer { // Precondition: Q is not empty // B : blob pointer; B := Q.front.next; Q.front.next := Q.front.next.next; return B; Enqueue(Q : blobqueue pointer, p : blob pointer): { Q.rear.next := new node; Q.rear := Q.rear.next; Q.rear.value := p; 4/15/2019 Stacks and Queues

21 Array Implementation Circular array front rear Q
4 2 12 holder = blob pointer array size = number in queue front = index of front of queue maxsize = max size of queue rear = (front + size) mod maxsize 4/15/2019 Stacks and Queues

22 Wrap Around front Q rear 0 1 2 3 4 5 6 7 8 9 10 11 4 10 12
4 10 12 rear = (front + size) mod maxsize = (10 + 4) mod 12 = 14 mod 12 = 2 4/15/2019 Stacks and Queues

23 Enqueue front Q rear 0 1 2 3 4 5 6 7 8 9 10 11 4 10 12 p 4/15/2019
4 10 12 p 4/15/2019 Stacks and Queues

24 Enqueue front Q rear 0 1 2 3 4 5 6 7 8 9 10 11 5 10 12 p 4/15/2019
5 10 12 p 4/15/2019 Stacks and Queues

25 Enqueue Enqueue(Q : blobqueue pointer, p : blob pointer) : { // precondition : queue is not full // Q.holder[(Q.front + Q.size) mod Q.maxsize] := p; Q.size := Q.size + 1; } Constant time! 4/15/2019 Stacks and Queues

26 Dequeue front Q rear 4 10 12 4/15/2019 Stacks and Queues

27 Dequeue Q rear front 0 1 2 3 4 5 6 7 8 9 10 11 3 11 12 return
3 11 12 return 4/15/2019 Stacks and Queues

28 Try Dequeue Define the circular array implementation of Dequeue
4/15/2019 Stacks and Queues

29 Solution to Dequeue Dequeue(Q : blobqueue pointer) : blob pointer {
// precondition : queue is not empty // p : blob pointer p := Q.holder[Q.front]; Q.front := (Q.front + 1) mod Q.maxsize; Q.size := Q.size - 1; return p; } 4/15/2019 Stacks and Queues


Download ppt "Stacks and Queues CSE 373 Data Structures."

Similar presentations


Ads by Google