CEN 235 4. Abstract Data Types - Queue and Stack
CEN 235 4. Abstract Data Types - Queue and Stack
For example:
5
Queue concepts
• A queue is a first-in-first-out sequence
of elements.
• Elements can be added only at one end
(the rear of the queue) and removed
only at the other end (the front of the
queue).
• The length/size of a queue is the
number of elements it contains.
• An empty queue has length zero.
6
Queue ADT: requirements
• Requirements:
1) It must be possible to make a queue empty.
2) It must be possible to test whether a queue is
empty.
3) It must be possible to obtain the length of a
queue.
4) It must be possible to add an element at the rear
of a queue.
5) It must be possible to remove the front element
from a queue.
6) It must be possible to access the front element in
a queue without removing it.
It must be possible to make a queue empty.
A function is needed that will set the values in the array to
a value that signifies it is clear and set the front and back
of the queue to the first index of the array.
0 1 2 3 4
MyQueue
Initialisation Length = 0
Front = 0
Back = 0
0 1 2 3 4
MyQueue
front back
Assign item to array[back]
addtoqueue
Increment back
Increment Length
0 1 2 3 4
MyQueue 6
front back
Front = 0
Back = 1
Length = 1
Assign item to array[back]
addtoqueue
Increment back
Increment Length
0 1 2 3 4
MyQueue 6 5
front back
Front = 0
Back = 2
length = 2
Remove from queue
0 1 2 3 4
MyQueue 6
5
front back
Increment front
Front = 1
decrement length
Back = 2
length = 1
Add to queue? The array is not full but we have reached
the length of the array
0 1 2 3 4
MyQueue 6 5 4
7 8
front
back
0 1 2 3 4
MyQueue 5 4
12 7 8
0 1 2 3 4
MyQueue 12 3 46 7 8
If array full
exit
Assign item to array[back]
Increment back
Increment length
Front = 3
If back = array length
Back = 3
length = 5 Set back to 0
Remove an item
If length == 0
MyQueue
exit
else
Increment front
Size = 0 decrement length
Remove an item
0 1 2 3 4
MyQueue 12 3 46 7 8
Front = 4
If length == 0
exit
else
Increment front
decrement size
25
Stack concepts (2)
• Illustration (stack of books):
Initially: After remov- After adding After adding
ing a book: “Misérables”: “2001”:
2001
Rob Roy Misérables Misérables
War & Peace War & Peace War & Peace War & Peace
Moby Dick Moby Dick Moby Dick Moby Dick
26
Stack ADT: requirements
• Requirements:
1) It must be possible to make a stack empty.
2) It must be possible to add (‘push’) an element to
the top of a stack.
3) It must be possible to remove (‘pop’) the topmost
element from a stack.
4) It must be possible to test whether a stack is
empty.
5) It should be possible to access the topmost
element in a stack without removing it.
It must be possible to make a stack empty.
A function is needed that will set the values in the array to a value that
signifies it is clear and set the top of the stack to the first index of
the array
[4]
Memory
[3]
MyStack [2]
[1]
5 Top
[0] element
Assign element
to depth position
Top_element 0
Increment depth
Depth 1
Increment top
element
Add/push an item on the stack
[4]
Memory
[3]
MyStack
[2] depth
Top
[1] 10
element
[0] 5
Assign element to
depth position
Increment depth
We now have a stack of:
Increment top element
top_element 1
Depth 2
pop an item from the stack
[4]
Memory [3]
MyStack [2]
[1] depth
Top
[0] 5 element
Memory
[4] depth
[1] 16
[4] 6 Top
Memory element
[3] 4
MyStack [2] 10
[1] 16
[0] 5
If depth = array length
stack full
We now have a stack of: Else
Top_element 4 Assign the element
[4]
Memory
[3]
MyStack
[2]
[1]
If stack not empty
Depth 0 [0]
decrement the top
element
Decrement depth
• Points to note:
else{
String topElem = elems[--depth];
return topElem;
}
}