Download as pdf or txt
Download as pdf or txt
You are on page 1of 68

CS11212 - Spring 2023 

Data Structures &


Introduction to Algorithms
 
Data Structures
Stacks & Queues

Ibrahim Albluwi
Stacks (a new ADT)

Stack

void push(const T& val) add val to the collection of elements


T pop() remove the most recently added element
T top() const return the most recently added element
Stacks (a new ADT)

Stack

void push(const T& val) add val to the collection of elements


T pop() remove the most recently added element
T top() const return the most recently added element

Elements cannot be accessed


! or modified in any other way
push(val) pop()

LIFO: Last In First Out


FILO: First In Last Out
Stacks (a new ADT)

Stack

void push(const T& val) add val to the collection of elements


T pop() remove the most recently added element
T top() const return the most recently added element

Elements cannot be accessed


! or modified in any other way
push(val) pop()

Eat this first

Inaccessible until LIFO: Last In First Out


the makdous
‫ﻣﻜـــﺪوس‬ above is eaten. FILO: First In Last Out
Exercise

What does the following code do?

void easy_to_trace(int a[], int size)

Stack<int> s;
for (int i = 0; i < size; i++)
s.push(a[i]);

for (int i = 0; i < size; i++)


cout << s.pop() << endl;
Exercise

What does the following code do?

void easy_to_trace(int a[], int size)

Stack<int> s;
for (int i = 0; i < size; i++)
s.push(a[i]);

for (int i = 0; i < size; i++)


cout << s.pop() << endl;

Answer. Prints the array elements in reverse order.

Application # 1

Stacks are used to reverse the order of a sequence.


Stacks (Applications)

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)

1. Reversing sequences. Since recursion uses the memory


2. Managing function calls. stack, recursive algorithms typically
can be converted to iterative
algorithms using an explicit stack.
Stay tuned for examples when we cover
void f2() {
the binary search tree data structure.
...
}

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

Undo / Back Redo / Forward


Stack 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

visiting order Undo / Back Redo / Forward


Stack Stack

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

visiting order Undo / Back Redo / Forward


Stack Stack

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

visiting order Undo / Back Redo / Forward


Stack Stack

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

visiting order Undo / Back Redo / Forward


Stack Stack

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

visiting order Undo / Back Redo / Forward


Stack Stack

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

visiting order Undo / Back Redo / Forward


Stack Stack

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

visiting order Undo / Back Redo / Forward


Stack Stack

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

visiting order Undo / Back Redo / Forward


Stack Stack

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

visiting order Undo / Back Redo / Forward


Stack Stack

visit(page) back() forward()

undo.push(page) redo.push(undo.pop()) undo.push(redo.pop())


show(undo.top()) 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

visiting order Undo / Back Redo / Forward


Stack Stack

visit(page) back() forward()

undo.push(page) redo.push(undo.pop()) undo.push(redo.pop())


show(undo.top()) 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
4. Balancing parentheses.

()[{}]([]{}) VALID

(([{}]{[]{}} INVALID (opened but not closed)

[{}])){[]{}} INVALID (closed but not opened)

[(]){()[}][] INVALID (misbalanced)


Stacks (Applications)

1. Reversing sequences. CHECK_PARANTHS(string str)

2. Managing function calls. for (int i=0; i<str.length(); i++) {


3. Undo and Redo in a word processor if ( /* str[i] is (, {, or [ */ )
3. Back and Forward in a web browser stack.push(str[i]);
else if (stack.is_empty() ||
4. Balancing parentheses.
/* str[i] does not
()[{}]([]{}) VALID match stack.top() */ )
return false;
(([{}]{[]{}} INVALID
else
[{}])){[]{}} INVALID stack.pop();
}
[(]){()[}][] INVALID
return stack.is_empty();

Try tracing the code!


Exercise

Which of the following are reasonable implementations for the Stack ADT?

Doubly-linked list: push(val) = add_to_head(val)


pop() = remove_head()

Doubly-linked list: push(val) = add_to_tail(val)


pop() = remove_tail()

Singly-linked list: push(val) = add_to_tail(val)


pop() = remove_tail()

Singly-linked list: push(val) = add_to_head(val)


pop() = remove_head()

Array list: push(val) = add_to_head(val)


pop() = remove_head()

Array list: push(val) = add_to_tail(val)


pop() = remove_tail()
Exercise

Which of the following are reasonable implementations for the Stack ADT?

Doubly-linked list: push(val) = add_to_head(val)


pop() = remove_head()

Doubly-linked list: push(val) = add_to_tail(val)


pop() = remove_tail()

Singly-linked list: push(val) = add_to_tail(val)


pop() = O(n)
pop() = remove_tail()

Singly-linked list: push(val) = add_to_head(val)


pop() = remove_head()

Array list: push(val) = add_to_head(val) pop() = O(n)


pop() = remove_head() push() = O(n)

Array list: push(val) = add_to_tail(val)


pop() = remove_tail()
Exercise

Which of the following are reasonable implementations for the Stack ADT?

Doubly-linked list: push(val) = add_to_head(val)


pop() = remove_head()

Doubly-linked list: push(val) = add_to_tail(val)


pop() = remove_tail()

Singly-linked list: push(val) = add_to_tail(val)


pop() = O(n)
pop() = remove_tail()

Uses less memory than


Singly-linked list: push(val) = add_to_head(val)
DLList & avoids
pop() = remove_head()
array resizing!

Array list: push(val) = add_to_head(val) pop() = O(n)


pop() = remove_head() push() = O(n)

Array list: push(val) = add_to_tail(val) Makes good use of


pop() = remove_tail() cache memory and
avoids creating and
deleting nodes.
Stack as a DLL (Implementation)

template <class T>


class Stack {
private:
DLList<T> list; delegate all the work to this object

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;
}
};

No need to implement a default constructor, a copy constructor, or a destructor, or


overload the assignment operator because this was already done in class DLList!
Stack as an Array (Implementation)

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

Which of the following are reasonable implementation for function clear()?

A. void clear() { last = -1; }

B. void clear() {
last = -1;
delete [] data;
}

C. void clear() {
last = -1;
delete [] data;
data = new data[capacity];
}

D. None of the above.


Exercise

Which of the following are reasonable implementation for function clear()?

The next push(…) statement


void clear() { last = -1; }
moves last to index 0

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.

D. None of the above.


Queues (a new ADT)

Queue

void enqueue(const T& val) add val to the collection of elements


T dequeue() remove the first added element
T get_first() const return the first added element
T get_last() const return the last added element

Elements cannot be accessed


! or modified in any other way

enqueue(val) dequeue()

5 4 3 2 1

FIFO: Fast In First Out


LILO: Last In Last Out
First come first serve
Queues (a new ADT)

Queue

void enqueue(const T& val) add val to the collection of elements


T dequeue() remove the first added element
T get_first() const return the first added element
T get_last() const return the last added element

Elements cannot be accessed


! or modified in any other way

Applications. enqueue(val) dequeue()

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?

Doubly-linked list: enqueue(val) = add_to_head(val)


dequeue() = remove_tail()

Doubly-linked list: enqueue(val) = add_to_tail(val)


dequeue() = remove_head()

Singly-linked list: enqueue(val) = add_to_tail(val)


dequeue() = remove_head()

Singly-linked list: enqueue(val) = add_to_head(val)


dequeue() = remove_tail()

Array list: enqueue(val) = add_to_head(val)


dequeue() = remove_tail()

Array list: enqueue(val) = add_to_tail(val)


dequeue() = remove_head()
Exercise

Which of the following are reasonable implementations for the #eue ADT?

Doubly-linked list: enqueue(val) = add_to_head(val)


dequeue() = remove_tail()

Doubly-linked list: enqueue(val) = add_to_tail(val)


dequeue() = remove_head()

Singly-linked list: enqueue(val) = add_to_tail(val)


dequeue() = remove_head()

Singly-linked list: enqueue(val) = add_to_head(val)


dequeue() = remove_tail() O(n)

Array list: enqueue(val) = add_to_head(val) O(n)


dequeue() = remove_tail()

Array list: enqueue(val) = add_to_tail(val)


dequeue() = remove_head() O(n)
A Queue As An Array

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

After calling dqueue() 5 times

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 full queue After calling enqueue(...) 3 times


First Last

A B C D E F G H

After calling dqueue() 5 times

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 full queue After calling enqueue(...) 3 times


First Last Last First

A B C D E F G H A B C # # F G H

After calling dqueue() 5 times

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 full queue After calling enqueue(...) 3 times


First Last Last First

A B C D E F G H A B C # # F G H

After calling dqueue() 5 times After calling dqueue() 3 times

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 full queue After calling enqueue(...) 3 times


First Last Last First

A B C D E F G H A B C # # F G H

After calling dqueue() 5 times After calling dqueue() 3 times

First Last First Last

# # # # # 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

Implement function is_full()

First Last Last First

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

First Last Last First

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

Implement function is_full()

First Last Last First

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

First Last Last First

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

bool is_full() const { bool is_full() const {


return (last+1) % capacity == first; return
} last-first+1 == capacity ||
last+1 == first;
bool is_full() const { }
return size == cap;
}
Exercise

Implement function is_full()

First Last Last First

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

First Last Last First

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

bool is_full() const { bool is_full() const {


return (last+1) % capacity == first; return
} last-first+1 == capacity ||
last+1 == first;
bool is_full() const { }
Buggy code!
return size == cap;
What if the queue is empty and capacity = 1?
}
Queues (Implementation Plan)
Resize up if full
last = (last + 1) % capacity;
Queue data[last] = val;

void enqueue(const T& val)


T dequeue()

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;

void enqueue(const T& val)


T dequeue() first = (first + 1) % capacity;
1
Resize down if 4
full

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.

for (int i = 0; i < n; i++)


myQueue.enqueue(i);

Choose the best answer.

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.

for (int i = 0; i < n; i++)


myQueue.enqueue(i);

Choose the best answer.

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

resize up resize down


size = 4 size = 4
data L F data L F
C D # A B C D # # # A B
0 1 2 3 4 0 1 2 3 4 5 6

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

You might also like