Chapter 4: Stacks & Queues
Chapter 4: Stacks & Queues
Queues
Stacks & Queues
Acknowledgement:
These slides are adapted from slides provided with Data Structures and Algorithms in C++
Goodrich, Tamassia and Mount (Wiley, 2004)
Stacks
Outline and Reading
• The Stack ADT (§4.2.1)
• Applications of Stacks (§4.2.3)
• Array-based implementation (§4.2.2)
• Growable array-based stack
…
S
0 1 2 t
Elementary Data Structures 10
Array-based Stack (cont.)
• The array storing the
stack elements may Algorithm push(o)
become full if t = S.length 1 then
• A push operation will throw FullStackException
then throw a else
FullStackException tt+1
Limitation of the array- S[t] o
based implementation
Not intrinsic to the
Stack ADT
S …
0 1 2 t
Elementary Data Structures 11
Performance and Limitations
- array-based implementation of stack ADT
• Performance
• Let n be the number of elements in the stack
• The space used is O(n)
• Each operation runs in time O(1)
• Limitations
• The maximum size of the stack must be defined a
priori , and cannot be changed
• Trying to push a new element into a full stack
causes an implementation-specific exception
normal configuration
Q
0 1 2 f r
wrapped-around configuration
Q
0 1 2 r f
Elementary Data Structures 25
Queue Operations
• We use the Algorithm size()
return (N - f + r) mod N
modulo operator
(remainder of Algorithm isEmpty()
return (f = r)
division)
Q
0 1 2 f r
Q
0 1 2 r f
Q
0 1 2 f r
Q
0 1 2 r f
Q
0 1 2 f r
Q
0 1 2 r f
Elementary Data Structures 28
Performance and Limitations
- array-based implementation of queue ADT
• Performance
• Let n be the number of elements in the stack
• The space used is O(n)
• Each operation runs in time O(1)
• Limitations
• The maximum size of the stack must be defined a
priori , and cannot be changed
• Trying to push a new element into a full stack
causes an implementation-specific exception
A B C D
f
elements
Elementary Data Structures 32
Informal C++ Queue Interface
template <typename Object>
• Informal C++ class Queue {
interface for our public:
Queue ADT int size();
• Requires the bool isEmpty();
Object& front()
definition of class
throw(EmptyQueueException);
EmptyQueueException void enqueue(Object o);
• No corresponding Object dequeue()
built-in STL class throw(EmptyQueueException);
};
elements
Elementary Data Structures 36
Deque with a Doubly Linked List
• We can implement a deque with a doubly linked list
• The front element is stored at the first node
• The rear element is stored at the last node
• The space used is O(n) and each operation of the
Deque ADT takes O(1) time
first last
elements
Elementary Data Structures 37
Performance and Limitations
- doubly linked list implementation of deque ADT
• Performance
• Let n be the number of elements in the stack
• The space used is O(n)
• Each operation runs in time O(1)
• Limitations
• NOTE: we do not have the limitation of the array
based implementation on the size of the stack b/c
the size of the linked list is not fixed, I.e., the
deque is NEVER full.