Lecture 7 - StackQueue - s2024
Lecture 7 - StackQueue - s2024
Spring 2024
2
Stack
What it is (conceptual)
How we implement it (implementation)
Why we use it (applications) 3
Stack ADT
Definition:
• Stack is a linear sequence (list) for which all
insertions and deletions (and usually all
accesses) are made at one end of the list.
(LIFO: Last-in-first-out)
• Also called:
• Last-in-first-out (LIFO) list
• Pushdown list.
4
Stack ADT
• The position that insertions and deletions can be
performed at the end of the list, called the Top.
• Data:
• Primitive data type or other (object)
• The 2 basic operations:
• push (insert)
• pop (delete)
Top
5
Stack ADT
Stack operations:
• Create an empty stack.
• Determine whether a stack is empty.
• Determine whether a stack is full.
• Add a new item into the stack (Push).
• Take out the most recently added (Pop).
• Retrieve the item most recently added (Peak)
6
Stack ADT
• Stack
•
• +isEmpty(): boolean
• +isFull(): boolean
• +push(ItemType d): void
• +pop(): ItemType
• +peak(): ItemType 7
Stack ADT: Example
Stack s = new Stack();
s.push(a);
s f
s.push(b);
d
s.push(c); top
d = s.pop();
c
a b
s.push(e);
f=s.peak(); 8
Stack Implementation
• Array-based implementation
• List-based implementation
9
Array-based stack [1]
• Array-based:
• An array is used for storing stack items
10
Array-based stack [2]
• Array-based:
• An array is used for storing stack items
• The array's first element should represent the
bottom of the stack
• The last occupied location in the array represents
the stack's top
• This avoids shifting of elements of the array
when we push or pop items from stack
11
Array-based stack [3]
• Array-based:
array
0 1 2 3 4 5 6 7 8 9
A B C D E F G
Top
12
Array-based stack [4]
if (top == -1)
return true;
• isEmpty(): boolean else
• If top equals -1 return false;
14
List-based stack[1]
• Using a SL List
to implement
a stack with
each node
reference one
item in stack
15
List-based stack[2]
• The first node should reference the stack's
top:
• With the head reference, we can add a new
node.
16
List-based stack[3]
• The first node should reference the stack's
top:
• With the head reference, we can remove a node.
17
List-based stack[4]
SLStack SLNode
18
List-based stack[5]
if (top == null)
return true;
• isEmpty(): boolean else
• If top equals null return false;
newNode.setNext(top);
top=newNode;
19
List-based stack[6]
if (!isEmpty())
• Pop(): ItemType {
SLNode topNode=top;
• If stack is not empty top=top.getNext();
• Take out and return topNode;
return top node }
else
• Update topreference return null;
• Peak(): ItemType
if (!isEmpty())
• If stack is not empty return top;
• Return item at the else
top position return null;
20
Queue
What it is (conceptual)
How we implement it (implementation)
Why we use it (applications)
21
Queue ADT
Definitions:
• A FIFO Queue: is a linear list for which all
insertions are made at one end of the list
(rear). All deletions (or all accesses) aremade
at the other end (front).
22
Queue ADT
• FIFO Queue
• Front: dequeue() or remove()operation
• Rear (or Back): enqueue() or insert() operation
23
Queue ADT
• Operations on Queue
• Create an empty queue
• Determine whether a queue is empty
• Determine whether a queue is full
• Add a new item to the queue (enqueue)
• Remove theitem that was added earliest (dequeue)
• Retrieve the item that was addedearliest
24
Queue ADT
• Queue
•
• +isEmpty(): boolean
• +isFull(): boolean
• +enqueue(ItemType d): void
• +dequeue(): ItemType
• +retrieve(): ItemType
25
Queue implementation
• Array-based implementation
• List-based implementation
26
Array-based Queue [1]
• An array of the size maxSize is used to
implement the queue:
• Two indexing front and rear must be maintained
• enqueue(80) rear
front
• enqueue(50)
• enqueue(100) 70 80 50 100 28
rear
Array-based Queue [3] front
70 80 50 100
• z=dequeue() 100 90
rear
• After a few enqueue and dequeue operations the
rear might reach the end of the queue and no
more items can be inserted although there is 29
front rear
• enqueue(10)
100 90 10
• enqueue(20) rear
front
• enqueue(30) 30
20 30 40 100 90 10
• enqueue(40)
rear
Array-based Queue [5]
• Circular queue
• Queue is full when rear == front front
20 30 40 100 90 10
rear
front
• x1=dequeue()
20 30 40 90 10
• x2=dequeue() rear
•… front
• x6=dequeue() 31
rear
Array-based Queue [6]
• Circular queue
• When rear==front, the queue can be either
empty or full front
rear
front
20 30 40 100 90 10
rear
• Thus, we cannot distinguish between empty and
32
full.
Array-based Queue [7]
• Circular queue
• We may avoid this situation by maintaining one empty
position, so that front will never equal to rear unless the
queue is empty
front
20 30 100 90 10
rear
front
33
rear
Array-based Queue [8]
if (front == rear)
• isEmpty() return true;
• front equals to rear else
return false;
• isFull()
• rear equals to front-1
if (rear == front-1)
return true;
else
return false;
34
Array-based Queue [9]
• enqueue(item) if (!isFull())
• add item at rear position {
q[rear]=item;
• update new rear rear=(rear+1) % maxSize;
}
front
100 90
rear
front
100 90 item
35
rear
Array-based Queue [10]
• dequeue() if (!isEmpty())
{
• return item at pos=front;
front position front=(front+1) % maxSize;
return q[pos];
• update new front }
front
90
rear
front
36
rear
List-based Queue [1]
• A linked list is used to implement the queue:
37
List-based Queue [2]
• ADT
SLQueue SLNode
38
List-based Queue [3]
• Create an empty queue
• Set front = rear = null
• isEmpty()
• If the queue is empty, then front==rear==null
• enqueue(newNode)
• Considering two case:
• If queue is empty
• front=rear=newNode
• If queue is not empty
• Add newNode to the last position of the list 39
• Update rear reference
List-based Queue [4]
• enqueue(newNode)
newNode.setNext(null);
if (isEmpty())
{
front=rear=newNode;
}
else
{
rear.setNext(newNode);
rear=newNode;
}
40
List-based Queue [5]
• dequeue()
• Considering two cases:
• If the queue has one item
• front=rear=null
• If the queue hasmore
than one item
• Get item at front
and return
• Update front reference
41
Tutorial & next topic
• Preparing for the tutorial:
• Practice with examples and exercises in
Tutorial 7
• Preparing for next topic:
• Read textbook chapter 3 (3.6 & 3.7) Stack and
Queue.
42