Download presentation
Presentation is loading. Please wait.
Published byMatthew Holland Modified over 9 years ago
1
ADT Stacks and Queues
2
Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.” A stack is also called a Last In First Out (LIFO) data structure.
3
Stack: Logical Level Stack Operations: void clear() void push (E it) E pop () E topValue () int length();
4
Stack: Application Level A runtime stack of activation records (ar) is maintained as a program executes to track function calls and scopes. Each activation record contains – space for local variables and parameters – ptr to dynamic parent – ptr to static parent – return address
5
Consider this code outline: Public class SomeMethods () { // ------------------------------ public static void B ( ) { } // ------------------------------ public static void A ( ) { B (); } // ------------------------------ public static void main (String[] args ) { A (); }
6
Consider the following: main begins executing main calls method A method A calls method B method B returns method A returns main returns Push (main’s ar) Push (A’s ar) Push (B’s ar) Use info in Top ar to return control to A Pop Use info in Top ar to return control to main Pop Use info in Top ar to return control to OS Pop main A B Runtime Stack
7
Stack: Application Level Stacks can be used to analyze nested expressions with grouping symbols to determine if they are well-formed (all grouping symbols occur in matching pairs and are nested properly.) ( ( {xxx} ) x [ ] xx) is well-formed ( ( {xxx} x [ ] ) is ill-formed
8
General Algorithm get next symbol set balanced flag to true while (there are more input symbols and expression still balanced) if (next symbol is opening symbol) Push symbol onto stack else if (next symbol is closing symbol) if (stack is empty) // check that length is 0 set balanced to false else pop the stack to get top opening symbol if (opening symbol does not match closing symbol) set balanced to false else ignore symbol get next symbol if (balanced and stack is empty) well-formed else ill-formed ( ( { x x x } ) x [ ] x x ) ( ( { [ Stack { ( [( Popped
9
Stack: Implementation Level Using an array:...... listArray [0] [maxSize - 1] 0 top * 10 maxSize * Note that top indicates position where next pushed item will go
10
Stack: Implementation Level Using an array: 70...... listArray [0] [MAX_ITEMS - 1] 1 top push ( 70 ) 10 maxSize
11
Stack: Implementation Level Using an array: 70 28...... listArray [0] [MAX_ITEMS - 1] 2 top push ( 70 ) push ( 28) 10 maxSize
12
Stack: Implementation Level Using an array: 70 28 88...... listArray [0] [MAX_ITEMS - 1] 3 top push ( 70 ) push ( 28) push ( 88) 10 maxSize
13
Stack: Implementation Level Using an array: 70 28 88...... listArray [0] [MAX_ITEMS - 1] 2 top push ( 70 ) push ( 28) push ( 88) pop () 10 maxSize
14
Stack: Implementation Level Using an array: 70 28 95...... listArray [0] [MAX_ITEMS - 1] 3 top push ( 70 ) push ( 28) push ( 88) pop () push ( 95) 10 maxSize
15
Stack: Implementation Level Using an array: 70 28 95...... listArray [0] [MAX_ITEMS - 1] 2 top push ( 70 ) push ( 28) push ( 88) pop () push ( 95) pop () 10 maxSize
16
Stack: Implementation Level Using an array: 70 28 95...... listArray [0] [MAX_ITEMS - 1] 1 top push ( 70 ) push ( 28) push ( 88) pop () push ( 95) pop () 10 maxSize
17
Stack: Implementation Level Using an array: 70 28 95...... listArray [0] [MAX_ITEMS - 1] 0 top push ( 70 ) push ( 28) push ( 88) pop () push ( 95) pop () 10 maxSize
18
Stack: Implementation Level Using a linked list: top NULL 0 size
19
Stack: Implementation Level Using a linked list: top NULL push ( 70 ) 70 1 size
20
Stack: Implementation Level Using a linked list: top NULL push ( 70 ) push ( 28 ) 2870 2 size
21
Stack: Implementation Level Using a linked list: top NULL push ( 70 ) push ( 28 ) push ( 88 ) 882870 3 size
22
Stack: Implementation Level Using a linked list: top NULL 2870 push ( 70 ) push ( 28 ) push ( 88 ) pop () 2 size
23
Stack: Implementation Level Using a linked list: top NULL push ( 70 ) push ( 28 ) push ( 88 ) pop () push ( 95 ) 952870 3
24
Stack: Implementation Level Using a linked list: top NULL 2870 push ( 70 ) push ( 28 ) push ( 88 ) pop () push ( 95 ) pop () 2 size
25
Stack: Implementation Level Using a linked list: top NULL 70 push ( 70 ) push ( 28 ) push ( 88 ) pop () push ( 95 ) pop () 1 size
26
Stack: Implementation Level Using a linked list: top NULL push ( 70 ) push ( 28 ) push ( 88 ) pop () push ( 95 ) pop () 0 size
27
Queue: Logical Level “An ordered group of homogeneous items or elements in which items are added at one end (the rear) and removed from the other end (the front.)” A queue is also called a First In First Out (FIFO) data structure.
28
Queue: Logical Level Queue Operations: void clear () void enqueue (E it) E dequeue () E frontValue () int length()
29
Queue: Application Level Perfect for modeling a waiting line in a simulation program Key simulation parameters – # of servers – # of queues (waiting lines) – statistics for customer arrival patterns Want to minimize customer waiting time Want to minimize server idle time
30
Queue: Application Level Queues found all over operating system! – I/O buffers – Job queues waiting for various resources – Spool (print) queue
31
Queue: Implementation Level Using an array: Option 1 items [0] [maxSize - 1] 0... rear front - fixed at [0] (similar to bottom of stack) * Note that rear indicates position where next enqueued item will go
32
Queue: Implementation Level Using an array: Option 1 items [0] [maxSize - 1] 1 A... rear front - fixed at [0] (similar to bottom of stack) enqueue (A)
33
Queue: Implementation Level Using an array: Option 1 items [0] [maxSize - 1] 2 AB... rear front - fixed at [0] (similar to bottom of stack) enqueue (A) enqueue (B)
34
Queue: Implementation Level Using an array: Option 1 items [0] [maxSize - 1] 3 ABC... rear front - fixed at [0] (similar to bottom of stack) enqueue (A) enqueue (B) enqueue (C)
35
Queue: Implementation Level Using an array: Option 1 items [0] [maxSize - 1] 3 ABC... rear front - fixed at [0] (similar to bottom of stack) enqueue (A) enqueue (B) enqueue (C) dequeue() But now front is at position[1], not [0] Need to shift remaining items down!
36
Queue: Implementation Level Using an array: Option 1 items [0] [maxSize - 1] 2 BC... rear front - fixed at [0] (similar to bottom of stack) enqueue (A) enqueue (B) enqueue (C) dequeue() After the shifting Is this a very efficient implementation? Θ(n)
37
Queue: Implementation Level Using an array: Option 2 items [0][4] 0 rear front 1 Note: Let maxSize = 5 for the example Keep track of both front and rear rear is actual rear (except when empty) Note that: length = rear – front + 1 front is actual front (except when empty)
38
Queue: Implementation Level Using an array: Option 2 items [0][4] 1 A rear front 1 Note: Let maxSize = 5 for the example enqueue (A) Note that: length = rear – front + 1
39
Queue: Implementation Level Using an array: Option 2 items [0][4] 2 AB rear front 1 Note: Let maxSize = 5 for the example enqueue (A) enqueue (B) Note that: length = rear – front + 1
40
Queue: Implementation Level Using an array: Option 2 items [0][4] 3 ABC rear front 1 Note: Let maxSize = 5 for the example enqueue (A) enqueue (B) enqueue (C) Note that: length = rear – front + 1
41
Queue: Implementation Level Using an array: Option 2 items [0][4] 4 ABCD rear front 1 Note: Let maxSize = 5 for the example enqueue (A) enqueue (B) enqueue (C) enqueue (D) Note that: length = rear – front + 1 Hmm... Queue now appears full... but
42
Queue: Implementation Level Using an array: Option 2 items [0][4] 4 ABCD rear front 2 Note: Let maxSize = 5 for the example enqueue (A) enqueue (B) enqueue (C) enqueue (D) dequeue () Note that: length = rear – front + 1
43
Queue: Implementation Level Using an array: Option 2 items [0][4] 4 A B CD rear front 3 Note: Let maxSize = 5 for the example enqueue (A) enqueue (B) enqueue (C) enqueue (D) dequeue () Note that: length = rear – front + 1 What if we want to Enqueue a couple of more items? Why not let Queue elements “wrap around” in array?
44
Queue: Implementation Level Using an array: Option 2 items [0][4] 0 EABCD rear front 3 Note: Let maxSize = 5 for the example Enqueue (A) Enqueue (B) Enqueue (C) Enqueue (D) Dequeue () Enqueue (E) Note that: length = rear – front + 1 ??? Note: to advance the rear indicator : rear = (rear + 1) % maxSize correction: length = ((rear+maxSize) – front + 1) % maxSize
45
Queue: Implementation Level Using an array: Option 2 items [0][4] 1 EFBCD rear front 3 Note: Let maxSize = 5 for the example enqueue (A) enqueue (B) enqueue (C) enqueue (D) dequeue () enqueue (E) enqueue (F) remember: length = ((rear+maxSize) – front + 1) % maxSize
46
Queue: Implementation Level Using an array: Option 2 items [0][4] 2 EFGCD rear front 3 Note: Let maxSize = 5 for the example enqueue (A) enqueue (B) enqueue (C) enqueue (D) dequeue () enqueue (E) enqueue (F) enqueue (G) remember: length = ((rear+maxSize) – front + 1) % maxSize
47
Queue: Implementation Level Using an array: Option 2 items [0][4] 2 EFGCD rear front 3 Note: Let maxSize = 5 for the example Also Note: to advance the rear or front indicators: rear = (rear + 1) % maxSize front = (front+1) % maxSize Now queue REALLY IS full! But look at values of front and rear and... Oops! This is supposed to mean queue is empty !!! ??? remember: length = ((rear+maxSize) – front + 1) % maxSize length = (( 2 + 5) – 3 + 1) % 5 = 0 Solution: Don’t let this happen; “waste” an array position!
48
Queue: Implementation Level Using a linked list: front NULL rear 0 size
49
Queue: Implementation Level Using a linked list: front NULL rear enqueue (A) A 1 size
50
Queue: Implementation Level Using a linked list: front NULL rear enqueue (A) enqueue (B) AB 2 size
51
Queue: Implementation Level Using a linked list: front NULL rear enqueue (A) enqueue (B) enqueue (C) ABC 3 size
52
Queue: Implementation Level Using a linked list: front NULL rear enqueue (A) enqueue (B) enqueue (C) dequeue () BC 2 size
53
Queue: Implementation Level Using a linked list: front NULL rear enqueue (A) enqueue (B) enqueue (C) dequeue () C 0 size
54
Queue: Implementation Level Using a linked list: front NULL rear enqueue (A) enqueue (B) enqueue (C) dequeue () 0 size
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.