Unit III Ppt
Unit III Ppt
Unit-3
Stack and Queue
By
Siddhasen R Patil
Course Outcomes
CO3:
• On completion of the course, learner will be able to
develop applications of stack and queue using array.
Syllabus
• Stack:
– Concept
– Basic Stack operations
– Array representation of stack
– Stack as ADT
– Stack Applications:
• Reversing data,
• Arithmetic expressions conversion and evaluation.
• Queue:
– Concept
– Queue operations
– Array representation of queue
– Queue as ADT
– Circular queue
– Priority Queue
– Applications of queue:
• Categorizing data
• Simulation of queue.
Stack
• Concept:
Stack
• Basic Stack Operations:
– Push: Add an element to the top of a stack.
– Peek: Get the value of the top element without removing it.
• Prefix notation: In this notation the operator is placed before its two
operands.
– i.e. +AB
• Postfix notation: In this notation the operator is placed after its two
operands.
– i.e. AB+
Arithmetic Expressions Conversion and
Evaluation
• There are 4 levels of precedence for binary operators as given
below:
– 4. Parenthesis { }, ( ), [ ] Highest
– 3. Exponentiation (^)
– 2. Multiplication (*) and division (/)
– 1. Addition (+) and Subtraction (-) Lowest
Infix Postfix
A+B
A+B*C
(A+B)*C
C/(D+E)+F
A+B*C+D
(A + B) * (C + D)
A*B+C*D
A+B+C+D
(A-B)*[C/(D+E)+F]
Arithmetic Expressions Conversion and
Evaluation
• Infix to Postfix Notation:
Infix Postfix
A+B AB+
A+B*C ABC*+
(A+B)*C AB+C*
C/(D+E)+F CDE+/F+
A+B*C+D ABC*+D+
(A + B) * (C + D) AB+CD+*
A*B+C*D AB*CD*+
A+B+C+D AB+C+D+
(A-B)*[C/(D+E)+F] AB- CDE +/F +*
Arithmetic Expressions Conversion and
Evaluation
• Infix to Prefix Notation:
Infix Prefix
A+B
A+B*C
(A+B)*C
C/(D+E)+F
A+B*C+D
(A + B) * (C + D)
A*B+C*D
A+B+C+D
(A-B)*[C/(D+E)+F]
Arithmetic Expressions Conversion and
Evaluation
• Infix to Prefix Notation:
Infix Postfix
A+B +AB
A+B*C +A*BC
(A+B)*C *+ABC
C/(D+E)+F +/C+DEF
A+B*C+D ++A*BCD
(A + B) * (C + D) *+AB+CD
A*B+C*D +*AB*CD
A+B+C+D +++ABCD
(A-B)*[C/(D+E)+F] *-AB+/C+DEF
Algorithm for Infix to Postfix Conversion
using Stack
• Step 1 : Push “(“onto Stack, and add “)” to the end of X.
#include<stdio.h>
#include<conio.h>
main()
{
char in[20], po[20], st[20];
int top = -1, i, n, j=0;
printf(“\nEnter total No. of symbols in Infix: ”);
scanf(“%d”, &n);
in[i] = ‘)’;
st[++top]=‘(‘;
C Program for Infix to Postfix Conversion
for (i=0; top != -1; i++)
{
if (in[i]== ‘(‘)
st[++top]=‘(‘;
else if (in [i] == ‘+’ || in[i] == ‘-’)
{
while (st[top] != ‘(‘)
{
po[j] = st[top]; top--; j++;
}
st[++top] = in[i];
}
else if (in [i] == ‘*’ || in[i] == ‘/’)
{
while (( st[top] != ‘(‘ ) && ( st[top] != ‘+‘ ) && ( st[top] != ‘-‘ ))
{
po[j] = st[top--]; j++;
}
st[++top] = in[j];
}
C Program for Infix to Postfix Conversion
else if (in[i] == ‘)’)
{
while (st[top] != ‘(‘)
{
po[j]= st[top--]; j++;
}
top --;
}
else
{
po[j] = in [i]; j++;
}
}
for (i=0; i<j; i++)
printf (“%c”, po[i]);
getch( );
return(0);
}
Algorithm for Infix to Prefix Conversion
using Stack
• Step 1 : Reverse the Infix expression also change the ‘(‘ by ‘)’ and ‘)’
by ‘(‘.
– And Push “(“onto Stack, and add “)” to the end of X.
• Step 3 : If the scanned character is the operator and the Stack is empty
Or contains the '(' symbol,
– Push the operator into the Stack.
• Step 5 : If the scanned operator has lower precedence than the existing
operator in the Stack,
– Pop all the Stack operators and append them to Prefix string.
– After that, push the scanned operator into the Stack.
Algorithm for Infix to Prefix Conversion
using Stack
• Step 6 : If the scanned character is a left bracket ‘)',
– Push it into the Stack.
top=-1;
for (i=0;i<j;i++)
{
st[++top]=po[i];
}
for (i=0;top!=-1;i++)
{
po[i]=st[top--];
}
printf("\nThe Prefix Equation of the given Infix equation is: ");
for (i=0;i<j;i++)
printf ("%c", po[i]);
getch ();
return (0);
}
Evaluation of Postfix Expression
Postfix Expression: 5 4 * 10 + 15 – 6 +
• Step 5 : Repeat step 2 to 4 for the next element of the postfix array.
• Step 6 : If the postfix string ends then Print the result popped from
the stack and Display the result.
C Program to Evaluate Postfix Expression
main()
{
int n,st[30],top = -1, val, a, b, result, i=0;
char A[30];
clrscr();
printf(“Enter the string of Postfix Expression”);
scanf(“%s”,&A);
while ( A[i] != ‘\0’)
{
if (A[i] >= ‘0’ && A[i] <=‘9’)
{
val = A[i] – 48;
st[++top] = val;
}
C Program to Evaluate Postfix Expression
• Step 5 : Repeat step 2 to 4 for the next element of the prefix array.
• Step 6 : Once the prefix string ends then Print the result popped
from the stack.
C Program to Evaluate Prefix Expression
#include<stdio.h>
#include<conio.h>
main()
{
int n, st[30], top = -1, val, a, b, result, i=0;
char A[30];
clrscr();
printf("Enter the string of Postfix Expression");
scanf("%s",&A);
for(i=0;A[i]!='\0';i++)
st[++top]=A[i];
for (i=0;top!=-1;i++)
A[i]=st[top--];
A[i]='\0';
C Program to Evaluate Postfix Expression
i=0;
while ( A[i] != '\0')
{
if (A[i] >= '0' && A[i] <= '9')
{
val = A[i] - 48;
st[++top] = val;
}
else if (A[i] == '+' || A[i] == '-' || A[i] == '*' || A[i] == '/')
{
a = st[top--];
b = st[top--];
switch (A[i])
{
case '+': result = a+b; break;
case '-': result = a-b; break;
case '*': result = a*b; break;
case '/': result = a/b; break;
}
st[++top]=result;
} i++;
}
C Program to Evaluate Postfix Expression
result = st[top--];
printf("\nThe result of evaluation is %d",result);
getch();
return(0);
}
Queue
• Concept:
– A queue in C is basically a linear data structure to store and
manipulate the data elements.
– In queues, the first element entered into the array is the first
element to be removed from the array.
Queue
• Operations :
– isEmpty(): To check if the queue is empty
– dequeue(): Removes the element from the front side of the queue
• Pointers
– Front: Pointer element used to locate the location of first element of
the queue
– Rear: Pointer element used to locate the location of the last element of
the queue.
Array Representation of Queue
• Algorithm
1. Initialize Queue
2. Display Menu: 1. Insert, 2. Remove, 3.Display, 4.Exit
3. Read Choice
4. If Choice=1
Call Insert Function
If Choice=2
Call Remove Function
If Choice=3
Call Display Function
If Choice=4
Exit from Program
Default
Display invalid Choice
4. Read Choice again
5. If Choice in between 1 to 3
Repeat step 3
Else
Stop
Algorithm for Insertion Function
Step 1:If (rear = max) then
a. Print Q is full
Else
a. if (front = -1)
i. front = 0
ii. rear = 0
b. Store new element at Q[rear];
c. Rear = rear+1
Algorithm for Delete Function
Step 1:
If front = rear
Print Q is empty
Else
Print the deleted element Q[front];
Front = front + 1
Algorithm for Read Queue Function
Step 1:
If front = rear
Print Q is empty
Else
a. Repeat step b until (i = Front; i <= Rear; i++)
b. Print Q[i];
Array Representation of Queue
#include<stdio.h>
#include<conio.h>
int main()
{
int queue[10], ch, front = -1, rear = -1, i;
clrscr();
printf("\n1.Insert \n2.Delet \n3.Display \n4.Exit");
printf("\nEnter the Choice:");
scanf("%d",&ch);
Array Representation of Queue
do
{
switch(ch)
{
case 1:
if(rear == 10)
printf("\n Queue is Full");
else
{
if (front == -1)
{
front++; rear++;
}
printf("\n Enter no ");
scanf("%d", &queue[rear++]);
} break;
Array Representation of Queue
case 2:
if(front == rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d", queue[front++]);
} break;
Array Representation of Queue
case 3:
if(front == rear)
printf("\n Queue is Empty");
else
{
printf("\nQueue Elements are:\n ");
for(i=front; i<rear; i++)
printf("%d\n",queue[i]);
} break;
case 4:
return(0);
default:
printf("Wrong Choice: please enter correct option");
}
Array Representation of Queue
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
printf("\nEnter the Choice:");
scanf("%d",&ch);
} while(ch!=4);
return 0;
}
Circular Queue
F=0
Repeat following step till (f<=rear)
Print Q[f++]
C-Program for Circular Queue
#include<stdio.h>
#include<conio.h>
# define MAX 5
int cqueue_arr[MAX];
void insert(void);
void deletion (void);
void display (void);
int front = -1;
int rear = -1;
int main()
{
int choice;
clrscr();
printf("\n1.Insert \n2.Delete \n3.Display \n4.Quit");
printf("Enter your choice : ");
scanf("%d",&choice);
C-Program for Circular Queue
do
{
switch(choice)
{
case 1 :
insert();break;
case 2 :
deletion(); break;
case 3:
display();break;
case 4:
return (0); break;
default:
printf("Wrong choicen");
}
printf("\n1.Insertn \n2.Deleten \n3.Displayn \n4.Quit");
printf("\nEnter your choice : ");
scanf("%d",&choice);
}while(choice!=4);
return (0);
}
C-Program for Circular Queue
void insert()
{
if((front == 0 && rear == MAX-1) || (front == rear+1))
printf("\nQueue Overflow");
else
{
if(front == -1)
{
front = 0;
rear = 0;
}
else
{
if(rear == MAX-1)
rear = 0;
else
rear = rear+1;
}
printf("\nInput the element for insertion in queue : ");
scanf("%d", & cqueue_arr[rear]);
}
}
C-Program for Circular Queue
void deletion()
{
if(front == -1)
printf("Queue Underflown");
else
{
printf("Element deleted from queue is : %d",cqueue_arr[front]);
if(front == rear)
{
front = -1;
rear = -1;
}
else
{
if(front == MAX-1)
front = 0;
else
front = front+1;
}
}
}
C-Program for Circular Queue
void display()
{
int f = front, r = rear;
if(front == -1)
{
printf("\nQueue is empty");
}
else
{
printf("\nQueue elements :");
if( f <= r )
{
while(f <= r)
{
printf("%d ",cqueue_arr[f]);
f++;
}
}
else
{
C-Program for Circular Queue
else
{
while(f <= MAX-1)
{
printf(“ %d ",cqueue_arr[f]);
f++;
}
f = 0;
while(f <= r)
{
printf(“ %d ",cqueue_arr[front_pos]);
f++;
}
}
}
}
Priority Queue
• In priority Queue each element has its own priority.
• In Priority Queue data who has highest priority remove from the Queue
first or processed first and second highest priority element after it and so
on.
• If priority is same for two elements then data remove on the basis of first
come first serve.
Priority Queue
Application of Queue
•Categorizing data
– Categorization is the process of dividing the things into groups whose
members are in some way similar to each other.