Unit 2
Unit 2
STACK
Stack is a Linear Data Structure that follows Last In First Out(LIFO) principle. Insertion and deletion can
be done at only one end of the stack called TOP of the stack.Example: - Pile of coins, stack of trays
STACK ADT:
STACK MODEL
TOP pointer
It will always point to the last element inserted in the stack.For empty stack, top will be pointing to -1.
(TOP = -1) Operations on Stack (Stack ADT)
Two fundamental operations performed on the stack are PUSH and POP.
(a) PUSH - It is the process of inserting a new element at the Top of the stack.
For every push operation:
1. Check for Full stack ( overflow ).
2. Increment Top by 1. (Top = Top + 1)
3. Insert the element X in the Top of the stack.
(b) POP - It is the process of deleting the Top element of the stack.
For every pop operation:
1. Check for Empty stack ( underflow ).
2. Delete (pop) the Top element X from the stack
3. Decrement the Top by 1. (Top = Top - 1 )
Exceptional Conditions of stack
1. Stack Overflow
An Attempt to insert an element X when the stack is Full, is said to be stack overflow.
For every Push operation, we need to check this condition.
2. Stack Underflow:
An Attempt to delete an element when the stack is empty, is said to be stackunderflow.
For every Pop operation, we need to check this condition.
Implementation of Stack
Stack can be implemented in 2 ways.
1. Static Implementation (Array implementation of Stack)
2. Dynamic Implementation (Linked List Implementation of Stack)
CS4301-Data Structures and Algorithms 1 Department of CSE 2023-2024
{ if (Top = = Arraysize 1)
return (1);
int TopElement(Stack S)
{
if(Top==-1)
{
return 0;
}
else
return S[Top];
}
Header
30 20 10 NULL
40
newnode
Before Insertion
Push routine /*Inserts element at front of the list
Header
40 30 20 10 NULL
After Insertion
TOP
(ii) Pop Operation
It is the process of deleting the Top element of the stack.
With Linked List implementations, the element at the Front of the List(i.e.) S -> next is always deleted.
It takes only one parameter. Pop(X).The element X to be deleted from the Front of theList.
Before deleting the front element in the list, check for Empty Stack.If the Stack is Empty, deletion is not
possible.
Otherwise, make the front element in the list as Update the next field of header.
Using free ( ) function, Deallocate the memory allocated for temp node.
Header
2 1 N
0 PANIMALA 0 U
40 30 L
L
R
S
TOP
Before Deletion
Pop routine /*Deletes the element at front of list
void Pop( Stack S )
{
Position temp, Top;
Top = S -> next;
if( S -> next = = NULL)
stack! Pop
else
{
Temp = S -> next;
S -> next = temp -> next;
free(temp);
Top = S -> next;
}}
CS4301-Data Structures and Algorithms 1 Department of CSE 2023-2024
Header
40 30 20 10 NULL
HEADER
30 20 10 NULL
After Deletion
(iii) Return Top Element
Pop routine deletes the Front element in the List.
If the user needs to know the last element inserted into the stack, then the user canreturn the Top element of
the stack.
To do this, first check for Empty Stack.
If the stack is empty, then there is no element in the stack.
Otherwise, return the element present in the S -> next -> data in the List.
Routine to Return Top Element
int TopElement(Stack S)
if(S->next==NULL)
is
return 0;
else
return S->next->data;
}
CS4301-Data Structures and Algorithms 1 Department of CSE 2023-2024
Header
40 30 20 10 NULL
TOP
Applications of Stack
POSTFIX:
The arithmetic operator appears directly after the two operands to which it applies.Also
called reverse polish notation.
PREFIX:
The arithmetic operator is placed before the two operands to which it applies. Alsocalled
polish notation.
CS4301-Data Structures and Algorithms 1 Department of CSE 2023-2024
Operand Value
A 2
B 3
C 4
D 4
E 2
OUTPUT = 8
CS4301-Data Structures and Algorithms 1 Department of CSE 2023-2024
Coding for Implementing Infix to Postfix Conversion
#define SIZE 50 /* Size of Stack */
#include <ctype.h>
char s[SIZE];
int top=-1; /* Global declarations */
void push(char elem)
{
s[++top]=elem;
}
char pop()
{
return(s[top--]);
}
int pr(char elem)
{ /* Function for precedence */
switch(elem)
{
case '#': return 0; case '(': return 1; case '+':
case '-': return 2;case '*':
case '/': return 3;
}
return 0;
}
Void main()
{ /* Main Program */
char infx[50],pofx[50],ch,elem;
int i=0,k=0;
printf("\nRead the Infix Expression ? ");
scanf("%s",infx);
push('#');
while( (ch=infx[i++]) != '\0')
{
if( ch == '(')
push(ch);
else if(isalnum(ch))
pofx[k++]=ch;
else if( ch == ')')
{
while( s[top] != '(')
pofx[k++]=pop();
elem=pop(); /* Remove */
}
else
{ /* Operator */
while( pr(s[top]) >= pr(ch) )
pofx[k++]=pop();
push(ch);
}
}
QUEUES:
Queue is a Linear Data Structure that follows First in First out (FIFO) principle.
Insertion of element is done at one end of the Queue called Rear end of the Queue.
Deletion of element is done at other end of the Queue “Front” the Queue.
Example: - Waiting line in the ticket counter.
Front Pointer - It always points to the first element inserted in the Queue.
Rear Pointer - It always points to the last element inserted in the Queue.
For Empty Queue:-
Front (F) = - 1
Rear (R) = - 1
Operations on Queue
Fundamental operations performed on the queue are
1. EnQueue
2. DeQueue
Implementation of Queue
Queue can be implemented in two ways.
1. Implementation using Array (Static Queue)
2. Implementation using Linked List (Dynamic Queue)
Array Declaration of Queue:
#define ArraySize 5
int Q [ ArraySize]; Or int Q [ 5 ];
return ( 1 );
}
CS4301-Data Structures and Algorithms 1 Department of CSE 2023-2024
Queue Full Operation
As we keep inserting the new elements at the Rear end of the Queue, the Queue becomesfull.
When the Queue is Full, Rear reaches its maximum Arraysize.
For every Enqueue Operation, we need to check for full Queue condition.
Routine to check for Full Queue
int IsFull( Queue Q )
if ( Rear = = ArraySize - 1 )
return ( 1 );
Enqueue Operation
It is the process of inserting a new element at the Rear end of the Queue.
It takes two parameters, Enqueue(X, Q). The elements X to be inserted at the Rear end ofthe Queue Q.
Before inserting a new Element into the Queue, check for Full Queue.If the Queue is already Full, Insertion
is not possible.
Otherwise, Increment the Rear pointer by 1 and then insert the element X at the Rear endof the Queue.
If the Queue is Empty, Increment both Front and Rear pointer by 1 and then insert theelement X at the Rear
end of the Queue.
Routine to Insert an Element in a Queue
void EnQueue (int X , Queue Q)
{
if ( Rear = = Arraysize - 1)
print (" Full Queue !!!!. Insertion notpossible");
else if (Rear = = - 1)
{
Front = Front + 1;Rear = Rear + 1; Q [Rear] = X;
}
else
{
Rear = Rear + 1;Q [Rear] = X;
}
}
CS4301-Data Structures and Algorithms 1 Department of CSE 2023-2024
}
}
CS4301-Data Structures and Algorithms 1 Department of CSE 2023-2024
Linked List Implementation of Queue
Queue is implemented using SLL (Singly Linked List ) node.
Enqueue operation is performed at the end of the Linked list and DeQueueoperation is performed at the
front of the Linked list.
With Linked List implementation, for Empty queue
Front = NULL & Rear = NULL
Linked List representation of Queue with 4 elements
Header
10 20 30 40 NULL
Front Rear
struct node;
typedef struct node * Queue;
typedef struct node * position;
int IsEmpty (Queue Q);
Queue CreateQueue (void);
void MakeEmpty (Queue Q);
void Enqueue (int X, Queue Q);
void Dequeue (Queue Q);
struct node
{
int data ;
position next;
}* Front = NULL, *Rear = NULL;
(i) Queue Empty Operation:
Initially Queue is Empty.
With Linked List implementation, Empty Queue is represented as S -> next = NULL.
It is necessary to check for Empty Queue before deleting the front element in the Queue.
ROUTINE TO CHECK WHETHER THE QUEUE IS EMPTY
int IsEmpty (Queue Q
{
If (Q->next == NULL) Q
return (1); Header NULL
}
Empty Queue
CS4301-Data Structures and Algorithms 1 Department of CSE 2023-2024
EnQueue Operation
It is the process of inserting a new element at the Rear end of the Queue.
It takes two parameters, EnQueue ( int X , Queue Q ). The elements X to be inserted intothe Queue Q.
Using malloc ( ) function allocate memory for the newnode to be inserted into the Queue.If the Queue is
Empty, the newnode to be inserted will become first and last node in the list. Hence Front and Rear points
to the newnode.
Otherwise insert the newnode in the Rear -> next and update the Rear pointer.
Empty Queue
Before Insertion
Front Rear
After Insertion
CS4301-Data Structures and Algorithms 1 Department of CSE 2023-2024
Header
20 30 40 NULL
Front Rear
else
{
Rear = ( Rear + 1 ) % Arraysize;
CQ[ Rear ] = X;
}
}
F, R
F= -1,R= --1
Routine To DeQueue An Element In Circular Queue
void DeQueue (CircularQueue CQ)
{
if(Front== - 1)
else if(Front==rear)
{
X=CQ[Front];Front=-1;Rear=-1;
}
else{
X=CQ[Front]; Front=(Front+1)%Arraysize;
}}
CS4301-Data Structures and Algorithms 1 Department of CSE 2023-2024
Operations on DEQUE
Four cases for inserting and deleting the elements in DEQUE are
1. Insertion At Rear End [ same as Linear Queue ]
2. Insertion At Front End
3. Deletion At Front End [ same as Linear Queue ]
4. Deletion At Rear End
Case 1: Routine to insert an element at Rear end
void Insert_Rear (int X, DEQUE DQ)
{
if( Rear = = Arraysize - 1)