Chapter 4: Stacks and Queues
4.2 Stack
4.2.1 The Stack ADT
Stacks.pdf
1-2-3-4
example: push(3), push(4), pop()...
Stacks.pdf
5
html-4.10
(StackEmptyException)
Access to elements (avoid making a copy) :
html-4.11
(Stack2)
4.2.2 A Simple Array-Based Implementation
Stacks.pdf
8-9
html-4.13
(ArrayStack1) ;
html-4.14
(ArrayStack2)
ArrayStack.cpp
;
StackExcept.cpp
4.3 Queues
4.3.1 The Queue ADT
Queues.pdf
3
example: enqueue(5), enqueue(3), dequeue(), ...
Queues.pdf
4
4.3.2 A Simple Array-Based Implementation
html-4.16
(Queue)
Queues.pdf
5-10
4.4 Linked Lists
4.4.1 Singly Linked Lists
DataStructures.pdf
13
4.4.2 Implementing a Stack with a Singly Linked List
html-4.18
(Node)
html-4.19
(LinkedStack1)
html-4.20
(LinkedStack2)
html-4.21
(LinkedStack3)
LinkedStack.cpp
4.4.3 Implementing a Queue with a Singly Linked List
html-4.22
(LinkedQueue)
4.5 Double-Ended Queue (Deque)
Supports insertion and deletion at both the front and the rear of the queue.
4.5.1 The Deque ADT
The fundamental functions of the deque ADT are as follows:
insertFirst(o), insertLast(o)
removeFirst(), removeLast()
The deque includes the following support functions:
first(), last()
size(), isEmpty()
Example:
insertFirst(3), insertFirst(5), first(), removeFirst(), insertLast(7), last(), removeFirst(), removeLast(), first(), size()
4.5.2 Implementing a Deque with a Doubly Linked List
Singly linked list is inefficient
Inserting and removing elements at either end of a
doubly linked list
is straightforward to do in
O
(1) time.
Nodes
store:
element (a datum)
link (pointer) to the previous node
link (pointer) to the next node
Special
trailer
and
header
nodes (sentinel nodes). They simplify the code for insertion and removal operations.
html-4.23
(Node)
html-4.24
(LinkedDeque1)
LinkedDeque.h
;
LinkedDeque.cpp
The running times of functions in the realization of a deque ADT by a doubly linked list are all
O
(1).
4.5.3 The Adapter Design Pattern
The
adapter
design pattern adjusts functions from one class so they can be used to implement functions of another class.
There is a simple mapping that takes the member functions of the stack (or queue) ADT and implements them using deque operations.
Stack method
Deque implementation
size()
isEmpty()
top()
push(o)
pop()
size()
isEmpty()
first()
insertFirst(o)
removeFirst()
html-4.25
(DequeStack)
We adapt class
LinkedDeque
, which implements the more powerful Deque interface, so that it can be used to implement the simpler Stack interface.