05-Stacks Queues
05-Stacks Queues
Queues
E top
D top D D top
C top C C C
B top B B B B
A top A A A A A
Direct applications
Page-visited history in a Web browser
Undo sequence in a text editor
Saving local variables when one function calls another, and this
one calls another, and so on.
Indirect applications
Auxiliary data structure for algorithms
Component of other data structures
0 1 2 3 4 5 6 7 8 9
stk: 17 23 97 44
top = 3 or count = 4
If the bottom of the stack is at location 0, then an empty stack is
represented by top = -1 or count = 0
To add (push) an element, either:
Increment top and store the element in stk[top], or
Store the element in stk[count] and increment count
To remove (pop) an element, either:
Get the element from stk[top] and decrement top, or
Decrement count and get the element in stk[count]
0 1 2 3 4 5 6 7 8 9
stk: 17 23 97 44
top = 2 or count = 3
When you pop an element, do you just leave the “deleted” element
sitting in the array?
The surprising answer is, “it depends”
If this is an array of primitives, or if you are programming in C or C++,
then doing anything more is just a waste of time
If you are programming in Java, and the array contains objects, you should
set the “deleted” array element to null
Why? To allow it to be garbage collected!
0 1 2 3 4 5 6 7 8 9
stk: 44 97 23 17
top = 6 or count = 4
topStk1 = 2 topStk2 = 6
Friday, September 8, 2023 9
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Error Checking
myStack:
44 97 23 17
myStack:
44 97 23 17
D rear D rear
C rear C C
B rear B B B front
rear front A
A A A front A front
front
dequeue enqueue
Direct applications
Waiting lists, bureaucracy
Access to shared resources (e.g., printer)
Multiprogramming
Indirect applications
Auxiliary data structure for algorithms
Component of other data structures
0 1 2 3 4 5 6 7
myQueue: 17 23 97 44
front = 0 rear = 3
front = 0 rear = 4
Friday, September 8, 2023 16
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Array Implementation of Queues
A queue is a first in, first out (FIFO) data structure
This is accomplished by inserting at one end (the rear) and deleting
from the other (the front)
front = 0 rear = 4
front = 1 rear = 4
Friday, September 8, 2023 17
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Array Implementation of Queues
front = 0 rear = 3
Initial queue: 17 23 97 44
front = 1 rear = 4
Notice how the array contents “crawl” to the right as elements are inserted and deleted
This will be a problem after a while!
0 1 2 3 4 5 6 7
myQueue: 44 55 11 22 33
rear = 1 front = 5
Elements were added to this queue in the order 11, 22, 33, 44,
55, and will be removed in the same order
Use: front = (front + 1) % myQueue.length;
and: rear = (rear + 1) % myQueue.length;
Friday, September 8, 2023 19
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Full and Empty Queues
If the queue were to become completely full, it would look like this:
0 1 2 3 4 5 6 7
myQueue: 44 55 66 77 88 11 22 33
rear = 4 front = 5
This is a problem!
Again, if we were to remove all eight elements, making the queue
completely empty, it would look like this:
0 1 2 3 4 5 6 7
myQueue:
rear = 4 front = 5
Friday, September 8, 2023 20
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Full and Empty Queues: Solutions
rear = 3 front = 5
Friday, September 8, 2023 21
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Linked-list Implementation of Queues
If you know where the last node in a list is, it’s hard to
remove that node, but it’s easy to add a node after it
Hence,
Use the first element in an SLL as the front of the queue
Use the last element in an SLL as the rear of the queue
Keep pointers to both the front and the rear of the SLL
Node to be
last enqueued
first
44 97 23 17
last
first
44 97 23 17