Stacks And Queues Chapter 18. 2 The Stack ADT Stack: a LIFO (last in, first out) data structure...
-
Upload
audrey-cross -
Category
Documents
-
view
216 -
download
0
Transcript of Stacks And Queues Chapter 18. 2 The Stack ADT Stack: a LIFO (last in, first out) data structure...
![Page 1: Stacks And Queues Chapter 18. 2 The Stack ADT Stack: a LIFO (last in, first out) data structure Examples: –plates in a cafeteria –return addresses for.](https://reader036.fdocuments.us/reader036/viewer/2022082518/56649f3d5503460f94c5c629/html5/thumbnails/1.jpg)
Stacks And Queues
Chapter 18
![Page 2: Stacks And Queues Chapter 18. 2 The Stack ADT Stack: a LIFO (last in, first out) data structure Examples: –plates in a cafeteria –return addresses for.](https://reader036.fdocuments.us/reader036/viewer/2022082518/56649f3d5503460f94c5c629/html5/thumbnails/2.jpg)
2
The Stack ADT
• Stack: a LIFO (last in, first out) data structure
• Examples:– plates in a cafeteria– return addresses for function calls
• Implementation:– static: fixed size, implemented as array– dynamic: variable size, implemented as
linked list
![Page 3: Stacks And Queues Chapter 18. 2 The Stack ADT Stack: a LIFO (last in, first out) data structure Examples: –plates in a cafeteria –return addresses for.](https://reader036.fdocuments.us/reader036/viewer/2022082518/56649f3d5503460f94c5c629/html5/thumbnails/3.jpg)
3
Stack Operations And Functions
• Operations:– push: add a value onto the top of the stack– pop: remove a value from the top of the stack
• Functions:– isFull: true if the stack is currently full, i.e.,
has no more space to hold additional elements– isEmpty: true if the stack currently contains
no elements
![Page 4: Stacks And Queues Chapter 18. 2 The Stack ADT Stack: a LIFO (last in, first out) data structure Examples: –plates in a cafeteria –return addresses for.](https://reader036.fdocuments.us/reader036/viewer/2022082518/56649f3d5503460f94c5c629/html5/thumbnails/4.jpg)
4
Stack Operations - Example
• A stack that can hold char values:
K
E
G
K
EEpush('E'); push('K'); push('G');
![Page 5: Stacks And Queues Chapter 18. 2 The Stack ADT Stack: a LIFO (last in, first out) data structure Examples: –plates in a cafeteria –return addresses for.](https://reader036.fdocuments.us/reader036/viewer/2022082518/56649f3d5503460f94c5c629/html5/thumbnails/5.jpg)
5
Stack Operations - Example
• A stack that can hold char values:
E
K
Epop();(remove G)
pop();(remove K)
pop();(remove E)
![Page 6: Stacks And Queues Chapter 18. 2 The Stack ADT Stack: a LIFO (last in, first out) data structure Examples: –plates in a cafeteria –return addresses for.](https://reader036.fdocuments.us/reader036/viewer/2022082518/56649f3d5503460f94c5c629/html5/thumbnails/6.jpg)
6
Dynamic Stacks
• Grow and shrink as necessary
• Can't ever be full as long as memory is available
• Implemented as a linked list
![Page 7: Stacks And Queues Chapter 18. 2 The Stack ADT Stack: a LIFO (last in, first out) data structure Examples: –plates in a cafeteria –return addresses for.](https://reader036.fdocuments.us/reader036/viewer/2022082518/56649f3d5503460f94c5c629/html5/thumbnails/7.jpg)
7
The Queue ADT
• Queue: a FIFO (first in, first out) data structure.
• Examples:– people in line at the theatre box office– print jobs sent to a printer
• Implementation:– static: fixed size, implemented as array– dynamic: variable size, implemented as linked
list
![Page 8: Stacks And Queues Chapter 18. 2 The Stack ADT Stack: a LIFO (last in, first out) data structure Examples: –plates in a cafeteria –return addresses for.](https://reader036.fdocuments.us/reader036/viewer/2022082518/56649f3d5503460f94c5c629/html5/thumbnails/8.jpg)
8
Queue Locations And Operations
• rear: position where elements are added
• front: position from which elements are removed
• enqueue: add an element to the rear of the queue
• dequeue: remove an element from the front of a queue
![Page 9: Stacks And Queues Chapter 18. 2 The Stack ADT Stack: a LIFO (last in, first out) data structure Examples: –plates in a cafeteria –return addresses for.](https://reader036.fdocuments.us/reader036/viewer/2022082518/56649f3d5503460f94c5c629/html5/thumbnails/9.jpg)
9
Queue Operations - Example
• A currently empty queue that can hold char values:
• enqueue('E');
E
front
rear
![Page 10: Stacks And Queues Chapter 18. 2 The Stack ADT Stack: a LIFO (last in, first out) data structure Examples: –plates in a cafeteria –return addresses for.](https://reader036.fdocuments.us/reader036/viewer/2022082518/56649f3d5503460f94c5c629/html5/thumbnails/10.jpg)
10
Queue Operations - Example
• enqueue('K');
• enqueue('G');
E K
E K G
front
rear
front
rear
![Page 11: Stacks And Queues Chapter 18. 2 The Stack ADT Stack: a LIFO (last in, first out) data structure Examples: –plates in a cafeteria –return addresses for.](https://reader036.fdocuments.us/reader036/viewer/2022082518/56649f3d5503460f94c5c629/html5/thumbnails/11.jpg)
11
Queue Operations - Example
• dequeue(); // remove E
• dequeue(); // remove K
K G
G
front
rear
front
rear
![Page 12: Stacks And Queues Chapter 18. 2 The Stack ADT Stack: a LIFO (last in, first out) data structure Examples: –plates in a cafeteria –return addresses for.](https://reader036.fdocuments.us/reader036/viewer/2022082518/56649f3d5503460f94c5c629/html5/thumbnails/12.jpg)
12
dequeue Issue, Solutions
• When removing an element from a queue, remaining elements must shift to front
• Solutions:– Let front index move as elements are removed
(works as long as rear index is not at end of array)
– Use above solution, and also let rear index "wrap around" to front of array, treating array as circular instead of linear (more complex enqueue, dequeue code)
![Page 13: Stacks And Queues Chapter 18. 2 The Stack ADT Stack: a LIFO (last in, first out) data structure Examples: –plates in a cafeteria –return addresses for.](https://reader036.fdocuments.us/reader036/viewer/2022082518/56649f3d5503460f94c5c629/html5/thumbnails/13.jpg)
13
Dynamic Queues
• Like a stack, a queue can be implemented using a linked list
• Allows dynamic sizing, avoids issue of shifting elements or wrapping indices
front rear
NULL