Professional Documents
Culture Documents
Stacks Queues
Stacks Queues
Ibrahim Albluwi
Stacks (a new ADT)
Stack
Stack
Stack
Stack<int> s;
for (int i = 0; i < size; i++)
s.push(a[i]);
Stack<int> s;
for (int i = 0; i < size; i++)
s.push(a[i]);
Application # 1
1. Reversing sequences.
2. Managing function calls.
void f2() {
...
}
void f1() {
...
f2();
...
}
int main() {
... Top of the stack =
main()
f1(); Active function call
...
} Memory Stack
Stacks (Applications)
1. Reversing sequences.
2. Managing function calls.
void f2() {
...
}
void f1() {
...
f2();
...
} Top of the stack =
f1()
Active function call
int main() {
...
main()
f1();
...
} Memory Stack
Stacks (Applications)
1. Reversing sequences.
2. Managing function calls.
void f2() {
...
}
void f1() {
...
Top of the stack =
f2()
Active function call
f2();
...
} f1()
Return from a
int main() { function = pop
...
main()
f1();
...
} Memory Stack
Stacks (Applications)
void f1() {
...
Top of the stack =
f2()
Active function call
f2();
...
} f1()
Return from a
int main() { function = pop
...
main()
f1();
...
} Memory Stack
Stacks (Applications)
1. Reversing sequences.
2. Managing function calls.
3. Undo and Redo in a word processor
3. Back and Forward in a web browser
→ ←
Back Forward Undo Redo
Stacks (Applications)
1. Reversing sequences.
2. Managing function calls.
3. Undo and Redo in a word processor
3. Back and Forward in a web browser
1. Reversing sequences.
2. Managing function calls.
3. Undo and Redo in a word processor
3. Back and Forward in a web browser
visit(page)
undo.push(page)
show(undo.top())
Stacks (Applications)
1. Reversing sequences.
2. Managing function calls.
3. Undo and Redo in a word processor
3. Back and Forward in a web browser
visit(page)
undo.push(page)
show(undo.top())
Stacks (Applications)
1. Reversing sequences.
2. Managing function calls.
3. Undo and Redo in a word processor
3. Back and Forward in a web browser
visit(page)
undo.push(page)
show(undo.top())
Stacks (Applications)
1. Reversing sequences.
2. Managing function calls.
3. Undo and Redo in a word processor
3. Back and Forward in a web browser
visit(page)
undo.push(page)
show(undo.top())
Stacks (Applications)
1. Reversing sequences.
2. Managing function calls.
3. Undo and Redo in a word processor
3. Back and Forward in a web browser
visit(page)
undo.push(page)
show(undo.top())
Stacks (Applications)
1. Reversing sequences.
2. Managing function calls.
3. Undo and Redo in a word processor
3. Back and Forward in a web browser
Back
visit(page) back()
undo.push(page) redo.push(undo.pop())
show(undo.top()) show(undo.top())
Stacks (Applications)
1. Reversing sequences.
2. Managing function calls.
3. Undo and Redo in a word processor
3. Back and Forward in a web browser
Back
visit(page) back()
undo.push(page) redo.push(undo.pop())
show(undo.top()) show(undo.top())
Stacks (Applications)
1. Reversing sequences.
2. Managing function calls.
3. Undo and Redo in a word processor
3. Back and Forward in a web browser
Back
visit(page) back()
undo.push(page) redo.push(undo.pop())
show(undo.top()) show(undo.top())
Stacks (Applications)
1. Reversing sequences.
2. Managing function calls.
3. Undo and Redo in a word processor
3. Back and Forward in a web browser
Forward
Back
1. Reversing sequences.
2. Managing function calls.
3. Undo and Redo in a word processor
3. Back and Forward in a web browser
Forward
Back
1. Reversing sequences.
2. Managing function calls.
3. Undo and Redo in a word processor
3. Back and Forward in a web browser
4. Balancing parentheses.
()[{}]([]{}) VALID
Which of the following are reasonable implementations for the Stack ADT?
Which of the following are reasonable implementations for the Stack ADT?
Which of the following are reasonable implementations for the Stack ADT?
public:
bool is_empty() const { return list.is_empty(); }
void clear() { list.clear(); }
void push(const T& val) { list.add_to_head(val); }
T top() const { return list.head_val(); }
T pop() {
T val temp = list.head_val(); throws an
list.remove_head(); exception
if the list is empty!
return temp;
}
};
Last
data = 3 A B C # # # # #
capacity = 8 0 1 2 3 4 5 6 7
size = 3
After calling
push(...)
3 times
Last
data = 3 A B C D E F # #
capacity = 8 0 1 2 3 6
4 5 7
size = 6
After calling
pop() 2 times
Last
data = 3 A B C D # # # #
capacity = 8
0 1 2 3 4 5 6 7
size = 4
Exercise
B. void clear() {
last = -1;
delete [] data;
}
C. void clear() {
last = -1;
delete [] data;
data = new data[capacity];
}
void clear() {
last = -1;
delete [] data; The array is not usable a!er this
} statement.
void clear() {
last = -1;
delete [] data; Unnecessarily removes the array
data = new data[capacity]; and crates a new one with the same
} capacity.
Queue
enqueue(val) dequeue()
5 4 3 2 1
Queue
1. Ticketing systems.
5 4 3 2 1
2. Printer queue.
3. Website requests queue.
4. Video streaming buffer. FIFO: Fast In First Out
LILO: Last In Last Out
etc. (too many to be listed)
Exercise
Which of the following are reasonable implementations for the #eue ADT?
Which of the following are reasonable implementations for the #eue ADT?
Problem.
• A #eue must modify the head
(either to remove or to add).
• Adding to or removing from the head
of a regular array list requires shi$ing
(costs O(n))
A Queue As An Array
Problem. Solution.
• A #eue must modify the head • Avoid shi$ing: Store the index of where
(either to remove or to add). the elements start in the queue and
• Adding to or removing from the head where they end.
of a regular array list requires shi$ing • Wrap-around if needed.
(costs O(n))
A Queue As An Array
Problem. Solution.
• A #eue must modify the head • Avoid shi$ing: Store the index of where
(either to remove or to add). the elements start in the queue and
• Adding to or removing from the head where they end.
of a regular array list requires shi$ing • Wrap-around if needed.
(costs O(n))
A full queue
First Last
A B C D E F G H
0 1 2 3 4 5 6 7
A Queue As An Array
Problem. Solution.
• A #eue must modify the head • Avoid shi$ing: Store the index of where
(either to remove or to add). the elements start in the queue and
• Adding to or removing from the head where they end.
of a regular array list requires shi$ing • Wrap-around if needed.
(costs O(n))
A full queue
First Last
A B C D E F G H
First Last
# # # # # F G H
A Queue As An Array
Problem. Solution.
• A #eue must modify the head • Avoid shi$ing: Store the index of where
(either to remove or to add). the elements start in the queue and
• Adding to or removing from the head where they end.
of a regular array list requires shi$ing • Wrap-around if needed.
(costs O(n))
A B C D E F G H
First Last
# # # # # F G H
A Queue As An Array
Problem. Solution.
• A #eue must modify the head • Avoid shi$ing: Store the index of where
(either to remove or to add). the elements start in the queue and
• Adding to or removing from the head where they end.
of a regular array list requires shi$ing • Wrap-around if needed.
(costs O(n))
A B C D E F G H A B C # # F G H
First Last
# # # # # F G H
A Queue As An Array
Problem. Solution.
• A #eue must modify the head • Avoid shi$ing: Store the index of where
(either to remove or to add). the elements start in the queue and
• Adding to or removing from the head where they end.
of a regular array list requires shi$ing • Wrap-around if needed.
(costs O(n))
A B C D E F G H A B C # # F G H
First Last
# # # # # F G H
A Queue As An Array
Problem. Solution.
• A #eue must modify the head • Avoid shi$ing: Store the index of where
(either to remove or to add). the elements start in the queue and
• Adding to or removing from the head where they end.
of a regular array list requires shi$ing • Wrap-around if needed.
(costs O(n))
A B C D E F G H A B C # # F G H
# # # # # F G H A B C # # # 3 #
Queues as Arrays (data members)
first = -1
last = -1
A B C D E F G H
capacity = 8
size = 0 0 1 2 3 4 5 6 7
first last
capacity = 8
A B C D E F G H
size = 3
0 1 2 3 4 5 6 7
last first
capacity = 8
A B C D E F G H
size = 5
0 1 2 3 4 5 6 7
Exercise
A B C D E F G H A B C D E F G H
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
A B C D E F G H A B C D E F G H
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
Exercise
A B C D E F G H A B C D E F G H
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
A B C D E F G H A B C D E F G H
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
A B C D E F G H A B C D E F G H
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
A B C D E F G H A B C D E F G H
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
F L
A B C D
0 1 2 3
enqueue(E)
F L
A B C D E # # #
0 1 2 3 4 5 6 7
Queues (Implementation Plan)
Resize up if full
last = (last + 1) % capacity;
Queue data[last] = val;
F L F L
A B C D A B C # # # # #
0 1 2 3 0 1 2 3 4 5 6 7
enqueue(E) dequeue()
F L F L
A B C D E # # # B C # # #
0 1 2 3 4 5 6 7 0 1 2 3 4
Exercise
What is the running time of the following code? Assume the queue is initially of size 1.
A. O(n 2)
B. O(n log n)
C. O(n)
D. O(log n)
Exercise
What is the running time of the following code? Assume the queue is initially of size 1.
A. O(n 2)
Too pessimistic!
B. O(n log n)
C. O(n) Correct!
D. O(log n) Incorrect
Exercise
What is the running time of the following code? Assume the queue is initially of size 1.
for (int i = 0; i < n; i++)
myQueue.enqueue(i);
x 1
Exercise
What is the running time of the following code? Assume the queue is initially of size 1.
for (int i = 0; i < n; i++)
myQueue.enqueue(i);
x 1
x x 1 (insert) + 1 (resize)
Exercise
What is the running time of the following code? Assume the queue is initially of size 1.
for (int i = 0; i < n; i++)
myQueue.enqueue(i);
x 1
x x 1 (insert) + 1 (resize)
x x x 1 (insert) + 2 (resize)
Exercise
What is the running time of the following code? Assume the queue is initially of size 1.
for (int i = 0; i < n; i++)
myQueue.enqueue(i);
x 1
x x 1 (insert) + 1 (resize)
x x x 1 (insert) + 2 (resize)
x x x x 1 (insert)
Exercise
What is the running time of the following code? Assume the queue is initially of size 1.
for (int i = 0; i < n; i++)
myQueue.enqueue(i);
x 1
x x 1 (insert) + 1 (resize)
x x x 1 (insert) + 2 (resize)
x x x x 1 (insert)
x x x x x 1 (insert) + 4 (resize)
Exercise
What is the running time of the following code? Assume the queue is initially of size 1.
for (int i = 0; i < n; i++)
myQueue.enqueue(i);
x 1
x x 1 (insert) + 1 (resize)
x x x 1 (insert) + 2 (resize)
x x x x 1 (insert)
x x x x x 1 (insert) + 4 (resize)
x x x x x x 1 (insert)
Exercise
What is the running time of the following code? Assume the queue is initially of size 1.
for (int i = 0; i < n; i++)
myQueue.enqueue(i);
x 1
x x 1 (insert) + 1 (resize)
x x x 1 (insert) + 2 (resize)
x x x x 1 (insert)
x x x x x 1 (insert) + 4 (resize)
x x x x x x 1 (insert)
x x x x x x x 1 (insert)
x x x x x x x x 1 (insert)
Exercise
What is the running time of the following code? Assume the queue is initially of size 1.
for (int i = 0; i < n; i++)
myQueue.enqueue(i);
x 1
x x 1 (insert) + 1 (resize)
x x x 1 (insert) + 2 (resize)
x x x x 1 (insert)
x x x x x 1 (insert) + 4 (resize)
x x x x x x 1 (insert)
x x x x x x x 1 (insert)
x x x x x x x x 1 (insert)
x x x x x x x x x 1 (insert) + 8 (resize)
Exercise
What is the running time of the following code? Assume the queue is initially of size 1.
for (int i = 0; i < n; i++)
myQueue.enqueue(i);
x 1
x x 1 (insert) + 1 (resize)
x x x 1 (insert) + 2 (resize)
x x x x 1 (insert)
x x x x x 1 (insert) + 4 (resize)
x x x x x x 1 (insert)
x x x x x x x 1 (insert)
x x x x x x x x 1 (insert)
x x x x x x x x x 1 (insert) + 8 (resize)
x x x x x x x x x x 1 (insert)
x x x x x x x x x x x 1 (insert)
x x x x x x x x x x x x 1 (insert)
x x x x x x x x x x x x x 1 (insert)
x x x x x x x x x x x x x x 1 (insert)
x x x x x x x x x x x x x x x 1 (insert)
x x x x x x x x x x x x x x x x 1 (insert)
Exercise
What is the running time of the following code? Assume the queue is initially of size 1.
for (int i = 0; i < n; i++)
myQueue.enqueue(i);
x 1
x x 1 + 1
x x x 1 + 2
x x x x 1
x x x x x 1 + 4
x x x x x x 1
x x x x x x x 1
x x x x x x x x 1
x x x x x x x x x 1 + 8
x x x x x x x x x x 1
x x x x x x x x x x x 1
x x x x x x x x x x x x 1
x x x x x x x x x x x x x 1
x x x x x x x x x x x x x x 1
x x x x x x x x x x x x x x x 1
x x x x x x x x x x x x x x x x 1 1 + 16
x x x x x x x x x x x x x x x x x
Exercise
What is the running time of the following code? Assume the queue is initially of size 1.
for (int i = 0; i < n; i++)
myQueue.enqueue(i);
x 1
x x 1 + 1 In general. %e total number
x x x 1 + 2
of copied elements is:
x x x x 1 n log n
x x x x x 1 + 4 ≤ ∑ 1 + ∑ 2i ≤ 3n + 1
x x x x x x 1 i=0 i=0 = O(n)
x x x x x x x 1
x x x x x x x x 1
x x x x x x x x x 1 + 8
x x x x x x x x x x 1
x x x x x x x x x x x 1
x x x x x x x x x x x x 1
x x x x x x x x x x x x x 1
x x x x x x x x x x x x x x 1
x x x x x x x x x x x x x x x 1
x x x x x x x x x x x x x x x x 1 1 + 16
x x x x x x x x x x x x x x x x x
Exercise
What is the running time of the following code? Assume the queue is initially of size 1.
for (int i = 0; i < n; i++)
myQueue.enqueue(i);
x 1
x x 1 + 1 In general. %e total number
x x x 1 + 2
of copied elements is:
x x x x 1 n log n
x x x x x 1 + 4 ≤ ∑ 1 + ∑ 2i ≤ 3n + 1
x x x x x x 1 i=0 i=0 = O(n)
x x x x x x x 1
x x x x x x x x 1 Implication.
x x x x x x x x x 1 + 8
Since enqueue is called n times
x x x x x x x x x x 1
and the worst case running
x x x x x x x x x x x 1
time is O(n) in total, the
x x x x x x x x x x x x 1
running time of each call to
x x x x x x x x x x x x x 1
enqueue is O(1) on average!
x x x x x x x x x x x x x x 1
x x x x x x x x x x x x x x x 1
x x x x x x x x x x x x x x x x 1 1 + 16
x x x x x x x x x x x x x x x x x
Resizing The Queue (resize and reorder the elements)
resize up
size = 3
data F L
# # A B C
0 1 2 3 4
new_data
A B C # # # # #
0 1 2 3 4 5 6 7
F L
Resizing The Queue (resize and reorder the elements)
resize up resize down
size = 3 size = 3
data F L data F L
# # A B C # # A B C
0 1 2 3 4
0 1 2 3 4
new_data new_data
A B C # # # # # A B C
0 1 2 3 4 5 6 7 0 1 2
F L F L
Resizing The Queue (resize and reorder the elements)
resize up resize down
size = 3 size = 3
data F L data F L
# # A B C # # A B C
0 1 2 3 4
0 1 2 3 4
new_data new_data
A B C # # # # # A B C
0 1 2 3 4 5 6 7 0 1 2
F L F L
resize up
size = 4
data L F
C D # A B
0 1 2 3 4
new_data
A B C D # # # #
0 1 2 3 4 5 6 7
F L
Resizing The Queue (resize and reorder the elements)
resize up resize down
size = 3 size = 3
data F L data F L
# # A B C # # A B C
0 1 2 3 4
0 1 2 3 4
new_data new_data
A B C # # # # # A B C
0 1 2 3 4 5 6 7 0 1 2
F L F L
new_data new_data
A B C D # # # # A B C D
0 1 2 3 4 5 6 7 0 1 2 3
F L F L
https://cdn.iconscout.com/icon/premium/png-256-thumb/ticket-queue-1560378-1321382.png
https://chainstrading.net/img/p/1/2/4/9/1249-thickbox_default.jpg