Unit 3 Stacks and Queue
Unit 3 Stacks and Queue
Unit 3
Outline
Introduction of stacks
Stack Operations –PUSH, POP
Stack Operations conditions- Stack Full/Stack Overflow, Stack Empty/Stack Underflow
Stack representation in Memory using Array and Link List.
Stack as ADT
Applications of stacks
Reversing a list
Polish Notations
Queue
Queue as an ADT.
Representation of Queue.
Types of Queue.
Applications of Linear Queue
Introduction of stacks
Stack Full/Stack Overflow: this is the situation when the stack is full and no
more elements can be PUSHED onto stack. This is required for the PUSH
operation because we cannot push in full stack.
Stack Empty/Stack Underflow: Checks whether the stack is empty. This
operation returns TRUE if the stack is empty and false otherwise. This is
required for the POP operation because we cannot pop from an empty stack.
Representation of stack
Static representation of stack using Array.
It is useful for fixed sized stack
Easy and convenient.
In this representation every new element is inserted at TOP.
Stack can be represented using a one dimensional array. Allocate a block of
memory required to store the full capacity of stack, and the items of stack are
stored in a sequential manner from the first location of memory block.
Representation of stack
Dynamic representation of stack uses linked list.
Singly Linked list is used for this representation.
In linked list the first element inserted into the stack is pointed out by the
second element, the second element by the third and so on. The (n-1)th element
is pointed out by nth element.
The only possible element that can be removed or popped is top element and
node can be inserted by becoming the new head node.
The linked list implementation of a stack requires the following:
Structure declaration containing some n fields, and
Pointer to the previous node or the next node.
Difference
Less space is used for each data element as no pointer is used. More space is used for each node.
The memory space is wasted if we store less elements than the maximum Memory space is not wasted as space is allocated only when user wants.
size of array.
Insertion and deletion involve more data movement. Insertion and deletion involve less data movement.
It does not provide flexibility because the size of stack is fixed. It provides flexibility as the size can grow as per requirement.
) )
F ) F
* )* F
E )* FE
( EMPTY FE*
/ / FE*
) /) FE*
D /) FE*D
- /)- FE*D
C /)- FE*DC
* /)-* FE*DC
B /)-* FE*DCB
+ /)-+ FE*DCB*
A /)-+ FE*DCB*A
( / FE*DCB*A+-
FE*DCB*A+-/
Evaluation of postfix expression
Algorithm
Scan the arithmetic operation from left to right and repeat the steps until ‘\o’ is
encountered.
If an operand is encountered put it on the stack.
If an operator is encountered then
Remove the top 2 elements of stack. (top element and next to top element)
Evaluate the operation on operant 1 and operand 2
Place the result back on to the stack
Set the value to the top of stack to get final outcome.
Exit.
Evaluation of postfix expression
evaluate the expression 234+*6-
Evaluation of postfix expression
Reversing a String
Recursion
When a function definition includes a call to itself, it is referred to as recursive
function.
For storing all values in a function a stack is maintained(current values of
parameters, local variables and the return address) .
When a function calls itself space is set aside in memory to execute. For second
function again the space is set aside and so on.
These memory areas for each function call are arranged in the stack. When
the execution of call is complete the place at the top is removed.
It should satisfy the properties
The arguments must have certain base values means it should have statement for
termination.
Each time the function calls itself it should get closer to the base value.
Tower of Hanoi
The tower of Hanoi is puzzle invented by French mathematician.
This is mathematical puzzle which has 3 towers and more than one disk.
There are disk are of different size stacked upon each other in ascending order.
The number of disk can increase but the number of tower remain same.
Tower of Hanoi puzzle with n disk can be solved in minimum 2^n-1 steps.
Rules:
You have to move the disk from on tower to other without disturbing the sequences
of arrangements.
Only one disk can be moved at one time.
Only ‘TOP’ disk can be removed.
No large disk can sit on small disk.
Tower of Hanoi puzzle example for 3 number of disk.
This presentation shows that a puzzle with 3 disks has taken
2^3-1=7 steps.
Queue
Queue is non primitive data structure. Queue is a ADT
Queue is an ordered and homogenous collection of elements where elements
are deleted at front end and inserter at rear end.
It is called FIFO..
Types of Queue
Linear Queue: Linear DS where insertion is at one end and deletion at other end..
It is FIFO. In linear queue elements are arranged in sequential mode such that
front position is always be less than or equal to the rear position.
Circular Queue: all the nodes are treated as circular. Last node is connected back to
first node. Circular queue is also called as Ring Buffer.
Dequeue (double ended queue): in double ended queue insert and delete operation
can be at both ends that is front and rear.
Priority Queue: It contains items which have some preset priority. While deleting
an element in the queue the data item with the highest
priority is removed first.
Queue as an ADT
The entities are added to rear terminal position called as enqueue and removal of entities
from the front terminal position called as dequeue. This is FIFO(first in first out) structure.
The queue operations are given below:
enqueue() add a new item to rear.
dequeue() remove the front item.
Queue() create a new queue.
Front() returns front element from queue.
Rear() returns rear element from queue.
isEmpty() to check whether the queue is empty.
size() to know the number of items in the queue.
C implementation for a queue: array is used to store the elements and variables front and rear
hold the position of first and last elements of the queue.
struct queue
{
int item[10], front, rear;
} q;
Representation of Queue
Stack Queue
Insertion and deletion take place at only one end called as TOP Insertion takes place at REAR and deletion take place at FRONT.
Stack has pointer variable TOP for operations Queue has two pointer variables front and rear for operations.
Operations on stack are PUSH and POP Operations on queue are ENQUEUE and DEQUEUE.
It is used for procedure calls or interrupt handling It is used for time sharing system.
Eg: Eg:
Stack of dishes etc People waiting at bus stop.
Linear Queue
Queue is the ordered list in which all insertions take place at one end called
rear and deletions take place at the other end called front.
Basic features of queue are:
This is FIFO.
Queue is also an ordered list of elements of similar data types.
Two pointers FRONT and REAR are used for insertion and deletion.
When queue is empty Front and Rear are -1.
When Front =0 and Rear=max-1 queue is full.
Operations on Queue.
Algorithm for enqueue() and dequeue() in
linear queue.
ENQUEUE() Algorithm:
Queue[max] be the array for queue. Front and Rear are initialize to -1. Item be the
element to be inserted.
If rear= max-1 then
print queue is overflow
Else
if front=-1 set front to 0
set rear=rear+1
set Queue[rear]= Item. // item inserted at rear position.
Algorithm for enqueue() and dequeue() in
linear queue.
DEQUEUE Algorithm:
Queue[max] be the array for queue. Front and Rear are initialize to -1.
If front=-1
Print queue is underflow
Else
Set value=queue[front] //delete the element which is at the front of queue
Set front=front+1
if front>rear then set both front and rear to -1
Need of Circular Queue.
Insertion is not possible even if the queue is empty in linear queue.
Circular Queue
In this queue all nodes are circular so that the first node follows last node. It is
also known as Ring Buffer.
Operations on circular queue
Algorithm for enqueue() and dequeue() in circular
queue.
ENQUEUE() Algorithm:
Let Queue[0-max-1] is an array having max capacity. Both front and rear are
initialize to -1.
If ((front==0) and rear==max-1) or (rear==front-1)
print overflow.
if (front==-1)
front=rear=0
else
rear=(rear+1)%max
Queue[rear]= element
Algorithm for enqueue() and dequeue() in
circular queue.
DEQUEUE() Algorithm:
Let Queue[0-max-1] is an array having max capacity. Both front and rear are
initialize to -1.
If(front==-1)
print underflow
Element= Queue[front]
If(front==rear)
front=rear=-1
Else
front=(front+1)%max
Priority Queue
Priority queue is the special kind of queue data structure in which insertion
and deletion operations are performed according to some special rule rather
than just FIFO rule.
In case of priority queue, a priority number is associated with each element.
The elements are inserted and deleted according to this priority number.
The following two rules are applied to process the elements in the priority
queue:
1. The elements with higher priority are processed before the elements with lower
priority.
2. In case of elements with same priority, elements are processed according to the
First In First Out (FIFO) rule.
Eg: queue in an hospital.
Priority Queue
In linked list representation of priority queue, each node of the linked list is
divided into three parts as shown below: