Download
Download
1. Array Implementation
}
int IsEmpty(Stack S)
{
if(Top==-1)
return 1;
}
ROUTINE TO RETURN TOP ELEMENT
int TopElement(Stack S)
{
if(!IsEmpty(S))
return S[Top];
else
Error(“Empty Stack”);
return 0;
}
LINKED LIST IMPLEMENTATION
PUSH operation is performed by inserting an element at
the front of the list.
100
ROUTINE TO POP AN ELEMENT FROM A STACK
void Pop (Stack S)
{
Struct Node *Tempcell;
If (IsEmpty(S))
Error (“Empty Stack”);
else
{
Tempcell=SNext;
SNext=SNextNext;
Free (Tempcell);
}
}
EXAMPLE
Towers of Hanoi
Function Calls
8 Queen Problem
NOTATIONS TO REPRESENT ARITHMETIC EXPRESSION
Priority Order
Examples
People moving on an escalator. The people who got on
the escalator first will be the first one to step out of it.
People standing outside the ticketing window of a
cinema hall. The first person in the line will get the ticket
first and thus will be the first one to move out of it.
OPERATIONS ON QUEUE
Enqueue - The process of inserting an element in the
queue. For every Enqueue operation in the Queue REAR
pointer is incremented by 1.
OPERATIONS ON QUEUE
Dequeue - The process of deleting an element from the
queue. For every Dequeue operation in the Queue
FRONT pointer is incremented by 1.
EXCEPTIONAL CONDITIONS
Overflow: Attempt to insert an element into the Queue
when Queue is full is called Overflow.
Array Implementation
Enqueue
Dequeue
POSITION CALCULATION
Insertion: To perform the insertion of an element to the
queue, the position of the REAR pointer is calculated by the
relation as
REAR = (REAR+1) % Maxsize.
CQueue[REAR]=value.
The new node is inserted before the node with the lower
priority.
After Insertion:
EXAMPLE
Before Insertion:
After Insertion:
DEQUEUE OPERATION
Deletion is a very simple process in this case.
The first node of the list will be deleted and the data of
that node will be processed first.
ARRAY REPRESENTATION OF A PRIORITY QUEUE
Every individual queue will have its own FRONT and REAR
pointers.
Before Insertion:
After Insertion:
DELETION OPERATION IN PRIORITY QUEUE
To delete an element, we find the first non-empty queue
and then process the FRONT element of the first non-
empty queue.
In our priority queue, the first non-empty queue is the
One with priority number 1 and the FRONT element is
A, so A will be deleted and processed first.
In technical terms, find the element with the smallest K,
such that FRONT [K]! = NULL.
APPLICATIONS OF QUEUE
Batch Processing in Operating System.
Simulation.
Computer Networks.
STACK VS QUEUE
S. No Terms Stack Queue
1. Principle First In First Out – LIFO Last In First Out - FIFO
2. Pointer Top Front and Rear
3. Operation Push and Pop Enqueue and Dequeue
Both Push and Pop Enqueue – Rear
4. Operation End Operations are performed Dequeue – Front
at Top
Exceptional Overflow and Underflow Overflow and Underflow
5.
Conditions
Top = -1. Rear = 0, Front = -1.
6. Initial Condition
Front=Rear=NULL
Push = Top+1 Enqueue = Rear+1
7.. Pointer Changing
Pop= Top-1 Dequeue = Front+1
1. Array 1. Array
8. Implementation 2. Linked List 2. Linked List