stacks and queues › ... › docs › stacks-queues.pdf · a dequeue is a combination of a queue...
TRANSCRIPT
![Page 2: Stacks and Queues › ... › docs › stacks-queues.pdf · A Dequeue is a combination of a queue and a stack. It supports the following operations: 1. insertFront(e): place the element](https://reader033.vdocuments.us/reader033/viewer/2022042402/5f13ee50b7c5884f232da6e4/html5/thumbnails/2.jpg)
Abstract Data Types
ADT Definition
1. Abstract model for a data type
2. Defined by behavior (semantics)
3. From the user point-of-view
This is in contrast to Data Structures which are representations ofdata from the implementer point-of-view.
![Page 3: Stacks and Queues › ... › docs › stacks-queues.pdf · A Dequeue is a combination of a queue and a stack. It supports the following operations: 1. insertFront(e): place the element](https://reader033.vdocuments.us/reader033/viewer/2022042402/5f13ee50b7c5884f232da6e4/html5/thumbnails/3.jpg)
Sparse Vector ADT
Example
A sparse vector is an ADT. We have expectations of how we caninteract with a sparse vector as a user, but we don’t need to knowhow it is implemented. We saw that it could be implemented witheither an array or linked list data structure.
![Page 4: Stacks and Queues › ... › docs › stacks-queues.pdf · A Dequeue is a combination of a queue and a stack. It supports the following operations: 1. insertFront(e): place the element](https://reader033.vdocuments.us/reader033/viewer/2022042402/5f13ee50b7c5884f232da6e4/html5/thumbnails/4.jpg)
The Stack ADT
DefinitionA Stack is a last-in, first-out (LIFO) ADT that supports thefollowing operations:
1. push(e): place the element e on the stack
2. pop(): return the element most recently placed on the stackand remove it from the stack
3. top(): return the element most recently placed on the stack,but do not remove it from the stack
Stacks may provide additional operations such as size() andempty().
![Page 5: Stacks and Queues › ... › docs › stacks-queues.pdf · A Dequeue is a combination of a queue and a stack. It supports the following operations: 1. insertFront(e): place the element](https://reader033.vdocuments.us/reader033/viewer/2022042402/5f13ee50b7c5884f232da6e4/html5/thumbnails/5.jpg)
The Stack ADT
DefinitionA Stack is a last-in, first-out (LIFO) ADT that supports thefollowing operations:
1. push(e): place the element e on the stack
2. pop(): return the element most recently placed on the stackand remove it from the stack
3. top(): return the element most recently placed on the stack,but do not remove it from the stack
Stacks may provide additional operations such as size() andempty().
![Page 6: Stacks and Queues › ... › docs › stacks-queues.pdf · A Dequeue is a combination of a queue and a stack. It supports the following operations: 1. insertFront(e): place the element](https://reader033.vdocuments.us/reader033/viewer/2022042402/5f13ee50b7c5884f232da6e4/html5/thumbnails/6.jpg)
The Stack ADT
DefinitionA Stack is a last-in, first-out (LIFO) ADT that supports thefollowing operations:
1. push(e): place the element e on the stack
2. pop(): return the element most recently placed on the stackand remove it from the stack
3. top(): return the element most recently placed on the stack,but do not remove it from the stack
Stacks may provide additional operations such as size() andempty().
![Page 7: Stacks and Queues › ... › docs › stacks-queues.pdf · A Dequeue is a combination of a queue and a stack. It supports the following operations: 1. insertFront(e): place the element](https://reader033.vdocuments.us/reader033/viewer/2022042402/5f13ee50b7c5884f232da6e4/html5/thumbnails/7.jpg)
The Stack ADT
DefinitionA Stack is a last-in, first-out (LIFO) ADT that supports thefollowing operations:
1. push(e): place the element e on the stack
2. pop(): return the element most recently placed on the stackand remove it from the stack
3. top(): return the element most recently placed on the stack,but do not remove it from the stack
Stacks may provide additional operations such as size() andempty().
![Page 8: Stacks and Queues › ... › docs › stacks-queues.pdf · A Dequeue is a combination of a queue and a stack. It supports the following operations: 1. insertFront(e): place the element](https://reader033.vdocuments.us/reader033/viewer/2022042402/5f13ee50b7c5884f232da6e4/html5/thumbnails/8.jpg)
The Stack ADT
DefinitionA Stack is a last-in, first-out (LIFO) ADT that supports thefollowing operations:
1. push(e): place the element e on the stack
2. pop(): return the element most recently placed on the stackand remove it from the stack
3. top(): return the element most recently placed on the stack,but do not remove it from the stack
Stacks may provide additional operations such as size() andempty().
![Page 9: Stacks and Queues › ... › docs › stacks-queues.pdf · A Dequeue is a combination of a queue and a stack. It supports the following operations: 1. insertFront(e): place the element](https://reader033.vdocuments.us/reader033/viewer/2022042402/5f13ee50b7c5884f232da6e4/html5/thumbnails/9.jpg)
The Stack ADT
DefinitionA Stack is a last-in, first-out (LIFO) ADT that supports thefollowing operations:
1. push(e): place the element e on the stack
2. pop(): return the element most recently placed on the stackand remove it from the stack
3. top(): return the element most recently placed on the stack,but do not remove it from the stack
Stacks may provide additional operations such as size() andempty().
![Page 10: Stacks and Queues › ... › docs › stacks-queues.pdf · A Dequeue is a combination of a queue and a stack. It supports the following operations: 1. insertFront(e): place the element](https://reader033.vdocuments.us/reader033/viewer/2022042402/5f13ee50b7c5884f232da6e4/html5/thumbnails/10.jpg)
Stack Implementation
Array or List?
1. How can we implement the Stack ADT with an array?
2. How can we implement it with a linked list?
3. What is the running time of the CRUD operations?
4. Are the implementations memory-efficient?
5. Which do you prefer and why?
![Page 11: Stacks and Queues › ... › docs › stacks-queues.pdf · A Dequeue is a combination of a queue and a stack. It supports the following operations: 1. insertFront(e): place the element](https://reader033.vdocuments.us/reader033/viewer/2022042402/5f13ee50b7c5884f232da6e4/html5/thumbnails/11.jpg)
The Queue ADT
DefinitionA Queue is a first-in, first-out (FIFO) ADT that supports thefollowing operations:
1. enque(e): place the element e in the queue
2. dequeue(): return the element that has been in the queuethe longest and remove it from the queue
3. front(): return the element that has been in the queue thelongest, but do not remove it from the queue
Stacks may provide additional operations such as size() andempty().
![Page 12: Stacks and Queues › ... › docs › stacks-queues.pdf · A Dequeue is a combination of a queue and a stack. It supports the following operations: 1. insertFront(e): place the element](https://reader033.vdocuments.us/reader033/viewer/2022042402/5f13ee50b7c5884f232da6e4/html5/thumbnails/12.jpg)
The Queue ADT
DefinitionA Queue is a first-in, first-out (FIFO) ADT that supports thefollowing operations:
1. enque(e): place the element e in the queue
2. dequeue(): return the element that has been in the queuethe longest and remove it from the queue
3. front(): return the element that has been in the queue thelongest, but do not remove it from the queue
Stacks may provide additional operations such as size() andempty().
![Page 13: Stacks and Queues › ... › docs › stacks-queues.pdf · A Dequeue is a combination of a queue and a stack. It supports the following operations: 1. insertFront(e): place the element](https://reader033.vdocuments.us/reader033/viewer/2022042402/5f13ee50b7c5884f232da6e4/html5/thumbnails/13.jpg)
The Queue ADT
DefinitionA Queue is a first-in, first-out (FIFO) ADT that supports thefollowing operations:
1. enque(e): place the element e in the queue
2. dequeue(): return the element that has been in the queuethe longest and remove it from the queue
3. front(): return the element that has been in the queue thelongest, but do not remove it from the queue
Stacks may provide additional operations such as size() andempty().
![Page 14: Stacks and Queues › ... › docs › stacks-queues.pdf · A Dequeue is a combination of a queue and a stack. It supports the following operations: 1. insertFront(e): place the element](https://reader033.vdocuments.us/reader033/viewer/2022042402/5f13ee50b7c5884f232da6e4/html5/thumbnails/14.jpg)
The Queue ADT
DefinitionA Queue is a first-in, first-out (FIFO) ADT that supports thefollowing operations:
1. enque(e): place the element e in the queue
2. dequeue(): return the element that has been in the queuethe longest and remove it from the queue
3. front(): return the element that has been in the queue thelongest, but do not remove it from the queue
Stacks may provide additional operations such as size() andempty().
![Page 15: Stacks and Queues › ... › docs › stacks-queues.pdf · A Dequeue is a combination of a queue and a stack. It supports the following operations: 1. insertFront(e): place the element](https://reader033.vdocuments.us/reader033/viewer/2022042402/5f13ee50b7c5884f232da6e4/html5/thumbnails/15.jpg)
The Queue ADT
DefinitionA Queue is a first-in, first-out (FIFO) ADT that supports thefollowing operations:
1. enque(e): place the element e in the queue
2. dequeue(): return the element that has been in the queuethe longest and remove it from the queue
3. front(): return the element that has been in the queue thelongest, but do not remove it from the queue
Stacks may provide additional operations such as size() andempty().
![Page 16: Stacks and Queues › ... › docs › stacks-queues.pdf · A Dequeue is a combination of a queue and a stack. It supports the following operations: 1. insertFront(e): place the element](https://reader033.vdocuments.us/reader033/viewer/2022042402/5f13ee50b7c5884f232da6e4/html5/thumbnails/16.jpg)
Queue Implementation
Array or List?
1. How can we implement the Queue ADT with an array?
2. How can we implement it with a linked list?
3. What is the running time of the CRUD operations?
4. Are the implementations memory-efficient?
5. Which do you prefer and why?
![Page 17: Stacks and Queues › ... › docs › stacks-queues.pdf · A Dequeue is a combination of a queue and a stack. It supports the following operations: 1. insertFront(e): place the element](https://reader033.vdocuments.us/reader033/viewer/2022042402/5f13ee50b7c5884f232da6e4/html5/thumbnails/17.jpg)
Variation: Double-Ended Queue ADT
DefinitionA Dequeue is a combination of a queue and a stack. It supportsthe following operations:
1. insertFront(e): place the element e at the front of thedequeue
2. insertBack(e): place the element e at the back of thedequeue
3. eraseFront(): remove the item at the front of the dequeue
4. eraseBack(): remove the item at the back of the dequeue
5. front(): return the element at the front of the dequeue
6. back(): return the element at the back of the dequeue
Dequeues may also provide size() and empty().
![Page 18: Stacks and Queues › ... › docs › stacks-queues.pdf · A Dequeue is a combination of a queue and a stack. It supports the following operations: 1. insertFront(e): place the element](https://reader033.vdocuments.us/reader033/viewer/2022042402/5f13ee50b7c5884f232da6e4/html5/thumbnails/18.jpg)
Variation: Double-Ended Queue ADT
DefinitionA Dequeue is a combination of a queue and a stack. It supportsthe following operations:
1. insertFront(e): place the element e at the front of thedequeue
2. insertBack(e): place the element e at the back of thedequeue
3. eraseFront(): remove the item at the front of the dequeue
4. eraseBack(): remove the item at the back of the dequeue
5. front(): return the element at the front of the dequeue
6. back(): return the element at the back of the dequeue
Dequeues may also provide size() and empty().
![Page 19: Stacks and Queues › ... › docs › stacks-queues.pdf · A Dequeue is a combination of a queue and a stack. It supports the following operations: 1. insertFront(e): place the element](https://reader033.vdocuments.us/reader033/viewer/2022042402/5f13ee50b7c5884f232da6e4/html5/thumbnails/19.jpg)
Variation: Double-Ended Queue ADT
DefinitionA Dequeue is a combination of a queue and a stack. It supportsthe following operations:
1. insertFront(e): place the element e at the front of thedequeue
2. insertBack(e): place the element e at the back of thedequeue
3. eraseFront(): remove the item at the front of the dequeue
4. eraseBack(): remove the item at the back of the dequeue
5. front(): return the element at the front of the dequeue
6. back(): return the element at the back of the dequeue
Dequeues may also provide size() and empty().
![Page 20: Stacks and Queues › ... › docs › stacks-queues.pdf · A Dequeue is a combination of a queue and a stack. It supports the following operations: 1. insertFront(e): place the element](https://reader033.vdocuments.us/reader033/viewer/2022042402/5f13ee50b7c5884f232da6e4/html5/thumbnails/20.jpg)
Variation: Double-Ended Queue ADT
DefinitionA Dequeue is a combination of a queue and a stack. It supportsthe following operations:
1. insertFront(e): place the element e at the front of thedequeue
2. insertBack(e): place the element e at the back of thedequeue
3. eraseFront(): remove the item at the front of the dequeue
4. eraseBack(): remove the item at the back of the dequeue
5. front(): return the element at the front of the dequeue
6. back(): return the element at the back of the dequeue
Dequeues may also provide size() and empty().
![Page 21: Stacks and Queues › ... › docs › stacks-queues.pdf · A Dequeue is a combination of a queue and a stack. It supports the following operations: 1. insertFront(e): place the element](https://reader033.vdocuments.us/reader033/viewer/2022042402/5f13ee50b7c5884f232da6e4/html5/thumbnails/21.jpg)
Variation: Double-Ended Queue ADT
DefinitionA Dequeue is a combination of a queue and a stack. It supportsthe following operations:
1. insertFront(e): place the element e at the front of thedequeue
2. insertBack(e): place the element e at the back of thedequeue
3. eraseFront(): remove the item at the front of the dequeue
4. eraseBack(): remove the item at the back of the dequeue
5. front(): return the element at the front of the dequeue
6. back(): return the element at the back of the dequeue
Dequeues may also provide size() and empty().
![Page 22: Stacks and Queues › ... › docs › stacks-queues.pdf · A Dequeue is a combination of a queue and a stack. It supports the following operations: 1. insertFront(e): place the element](https://reader033.vdocuments.us/reader033/viewer/2022042402/5f13ee50b7c5884f232da6e4/html5/thumbnails/22.jpg)
Variation: Double-Ended Queue ADT
DefinitionA Dequeue is a combination of a queue and a stack. It supportsthe following operations:
1. insertFront(e): place the element e at the front of thedequeue
2. insertBack(e): place the element e at the back of thedequeue
3. eraseFront(): remove the item at the front of the dequeue
4. eraseBack(): remove the item at the back of the dequeue
5. front(): return the element at the front of the dequeue
6. back(): return the element at the back of the dequeue
Dequeues may also provide size() and empty().
![Page 23: Stacks and Queues › ... › docs › stacks-queues.pdf · A Dequeue is a combination of a queue and a stack. It supports the following operations: 1. insertFront(e): place the element](https://reader033.vdocuments.us/reader033/viewer/2022042402/5f13ee50b7c5884f232da6e4/html5/thumbnails/23.jpg)
Variation: Double-Ended Queue ADT
DefinitionA Dequeue is a combination of a queue and a stack. It supportsthe following operations:
1. insertFront(e): place the element e at the front of thedequeue
2. insertBack(e): place the element e at the back of thedequeue
3. eraseFront(): remove the item at the front of the dequeue
4. eraseBack(): remove the item at the back of the dequeue
5. front(): return the element at the front of the dequeue
6. back(): return the element at the back of the dequeue
Dequeues may also provide size() and empty().
![Page 24: Stacks and Queues › ... › docs › stacks-queues.pdf · A Dequeue is a combination of a queue and a stack. It supports the following operations: 1. insertFront(e): place the element](https://reader033.vdocuments.us/reader033/viewer/2022042402/5f13ee50b7c5884f232da6e4/html5/thumbnails/24.jpg)
Variation: Double-Ended Queue ADT
DefinitionA Dequeue is a combination of a queue and a stack. It supportsthe following operations:
1. insertFront(e): place the element e at the front of thedequeue
2. insertBack(e): place the element e at the back of thedequeue
3. eraseFront(): remove the item at the front of the dequeue
4. eraseBack(): remove the item at the back of the dequeue
5. front(): return the element at the front of the dequeue
6. back(): return the element at the back of the dequeue
Dequeues may also provide size() and empty().
![Page 25: Stacks and Queues › ... › docs › stacks-queues.pdf · A Dequeue is a combination of a queue and a stack. It supports the following operations: 1. insertFront(e): place the element](https://reader033.vdocuments.us/reader033/viewer/2022042402/5f13ee50b7c5884f232da6e4/html5/thumbnails/25.jpg)
Variation: Double-Ended Queue ADT
DefinitionA Dequeue is a combination of a queue and a stack. It supportsthe following operations:
1. insertFront(e): place the element e at the front of thedequeue
2. insertBack(e): place the element e at the back of thedequeue
3. eraseFront(): remove the item at the front of the dequeue
4. eraseBack(): remove the item at the back of the dequeue
5. front(): return the element at the front of the dequeue
6. back(): return the element at the back of the dequeue
Dequeues may also provide size() and empty().
![Page 26: Stacks and Queues › ... › docs › stacks-queues.pdf · A Dequeue is a combination of a queue and a stack. It supports the following operations: 1. insertFront(e): place the element](https://reader033.vdocuments.us/reader033/viewer/2022042402/5f13ee50b7c5884f232da6e4/html5/thumbnails/26.jpg)
How to pronounce “dequeue”?
Your book says this is pronounced like “deck” to avoid confusionwith the dequeue() operator for queues. I’ve never liked that.How about...
I Since it’s two-ended, we could just as well call it a bi-queueand pronounce it “bike”
I Since it’s a combination of a queue and a stack, how about“quest?”
I don’t think either will catch on, so it’s safer to stick with “deck.”
![Page 27: Stacks and Queues › ... › docs › stacks-queues.pdf · A Dequeue is a combination of a queue and a stack. It supports the following operations: 1. insertFront(e): place the element](https://reader033.vdocuments.us/reader033/viewer/2022042402/5f13ee50b7c5884f232da6e4/html5/thumbnails/27.jpg)
How to pronounce “dequeue”?
Your book says this is pronounced like “deck” to avoid confusionwith the dequeue() operator for queues. I’ve never liked that.How about...
I Since it’s two-ended, we could just as well call it a bi-queueand pronounce it “bike”
I Since it’s a combination of a queue and a stack, how about“quest?”
I don’t think either will catch on, so it’s safer to stick with “deck.”
![Page 28: Stacks and Queues › ... › docs › stacks-queues.pdf · A Dequeue is a combination of a queue and a stack. It supports the following operations: 1. insertFront(e): place the element](https://reader033.vdocuments.us/reader033/viewer/2022042402/5f13ee50b7c5884f232da6e4/html5/thumbnails/28.jpg)
How to pronounce “dequeue”?
Your book says this is pronounced like “deck” to avoid confusionwith the dequeue() operator for queues. I’ve never liked that.How about...
I Since it’s two-ended, we could just as well call it a bi-queueand pronounce it “bike”
I Since it’s a combination of a queue and a stack, how about“quest?”
I don’t think either will catch on, so it’s safer to stick with “deck.”
![Page 29: Stacks and Queues › ... › docs › stacks-queues.pdf · A Dequeue is a combination of a queue and a stack. It supports the following operations: 1. insertFront(e): place the element](https://reader033.vdocuments.us/reader033/viewer/2022042402/5f13ee50b7c5884f232da6e4/html5/thumbnails/29.jpg)
Reading Assignment
Read the following sections in the textbook
1. Section 5.1: Stacks; including the applications in 5.1.6 and5.1.7
2. Section 5.2: Queues
3. Section 5.3: Double-Ended Queues; omit Adapters (5.3.4)