DS Stack and its Applications (1) (1)
DS Stack and its Applications (1) (1)
Size=5
4
0
top = -1
b. push(s, x): It is used to push the data element ‘x’ into the stack ‘s’. Whenever we
are pushing the data element into the stack first we check the condition. if
(top==size-1), if it is true indicate stack is full or overflow. Otherwise, Increment
the top and insert the data element in that location. This is shown in below.
4 4 4 4 4
10
8 8
3 3 3 3 3
6 6 6
2 2 2 2 2
4 4 4 4
1 1 1 1 1
2 2 2 2 2
0 0 0 0 0
top = 0 top = 1 top = 2 top = 3 top = 4
top top+1
After inserting 5 elements the top reaches the size-1. Therefore no more insertion is possible
because and the stack is full.
c. pop(s): It is used to delete the data element from the stack. Here also we check
the condition if(top==-1). If it is true the stack is empty or underflow. Otherwise
retrieve the data element from the top of the stack and store it in temporary
variable ‘x’ and then top will be decremented by 1. This is shown in below.
4 4 4 4 4
8
3 3 3 3 3
6 6
2 2 2 2 2
4 4 4
1 1 1 1 1
2 2 2 2
0 0 0 0 0
top = 3 top = 2 top = 1 top = 0 top =-1
top top-1
After deleting the 5 elements the top reaches the ‘-1’ that means that the stack is empty.
d. isfull(): It is used to check whether the stack is full or not while checking the
condition if(top==size-1). If it is true, the stack is full.
e. isempty(): It is used to check whether the stack is empty or not while checking
the condition if(top==-1). If it is true, the stack is empty
Ex: The collection of books or plates placed on one on another is nothing but the stack.
Applications:
1. Evolution of postfix expression.
2. Conversion from Infix to Postfix.
3. Stack is used in Recursion.
Stack is implemented by using two ways:
i. By using Arrays.
ii. By using Linked lists.
/* Implementation of Stack by using arrays*/
#include <stdio.h>
#include<process.h>
#define size 5
struct stack
{
int top;
int item[size];
};
struct stack s;
void push(struct stack *ps,int x);
int pop(struct stack *ps);
void display(struct stack *ps);
void main()
{
int ele,ch;
s.top=-1;
clrscr();
do
{
printf("\n 1: push \n");
printf(" 2: pop \n");
printf(" 3: display \n");
printf(" 4: exit \n");
printf("enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n ele:");
scanf("%d",&ele);
push(&s,ele);
break;
case 2:ele=pop(&s);
if(ele!=0)
printf("deleted ele is %d",ele);
break;
case 3:display(&s);
break;
case 4:exit(0);
}
}while(ch<4);
getch();
}
void push(struct stack *ps,int x)
{
if (ps->top==size-1)
printf("stack is full\n");
else
{
ps->top=ps->top+1;
ps->item[ps->top]=x;
printf("\n ele inserted into stack");
}
}
int pop(struct stack *ps)
{
int y;
if (ps->top==-1)
{
printf("stack under flow\n");
return 0;
}
else
{
y=ps->item[ps->top];
ps->top=ps->top-1;
return y;
}
}
void display(struct stack *ps)
{
int i;
if(ps->top==-1)
printf("stack is empty\n");
else
{
printf("\nthe elements in the stack are:");
for(i=ps->top;i>=0;i--)
printf("%4d",ps->item[i]);
}
}
OUTPUT:
Applications of Stack:
Generally, the Mathematical Expression is represented in the following Expressions.
1. Infix Expression
2. Prefix Expression
3. Postfix Expression
Infix Expression: The operator is placed between the two operands. Such an expression
is called as Infix Expression.
Ex: A+B, A*B.
Prefix Expression: The operator is placed before two operands. Such an expression is
called as Prefix Expression.
Ex: +AB,*AB
Postfix Expression: The operator is placed after the two operands. Such an expression is
called as Postfix Expression.
Ex: AB+, AB*
Generally, the Mathematical Equation consists of four operators.
Multiplication (*)
Division (/)
Addition (+)
Subtraction (-)
And also consists of fifth operator i.,e Exponential ($ or ^) Ex: 3$2=9
The preceding of operators from highest to lowest:
Exponential ($ or ^)
Multiplication (*) or Division (/)
Addition (+) or Subtraction (-)
To avoid the confusion we use the parenthesis i.,e ( ).
Table 1.1 Conversions from Infix Expression to Prefix and Postfix Expression
I. Evaluation of Postfix Expression:
Ex: Evaluate the given Postfix Expression.
345*+
Sol: The given expression is evaluated from left to right one symbol at a time if it is the
operand directly push it into the stack, if it is operator pop top two operands from the
stack and apply operator between operand2 and operand 1 and get the result and then
push the result into the stack. This is continued until given expression is completed. If
input string is completed pop the result from the stack and display it on the screen.
3 3
4 3,4
5 3,4,5
* 5 4 20 3,20
+ 20 3 23 23
9 9
8 9,8
+ 8 9 17 17
3 17,3
8 17,3,8
2 17,3,8,2
/ 2 8 4 17,3,4
* 4 3 12 17,12
2 17,12,2
+ 2 12 14 17,14
- 14 17 3 3
6 6
2 6,2
3 6,2,3
+ 3 2 5 6,5
- 5 6 1 1
3 1,3
8 1,3,8
2 1,3,8,2
/ 2 8 4 1,3,4
+ 4 3 7 1,7
* 7 1 7 7
2 7,2
$ 2 7 49 49
3 49,3
+ 3 49 52
Result:52
Algorithm for Evaluation of Postfix Expression:
OUTPUT:
Ex1: Consider the Infix Expression A+B*C and convert it into Postfix Expression.
Sol: The given Expression is evaluated from left to right. If it is operand directly send to
postfix Expression. If it is operator push it into the stack until the right movement. If the
input string is completed pop the elements from the stack until the stack is empty and
popped elements are send to Postfix Expression.
A A
+ + A
B + AB
* +* AB
C +* ABC
Verification: A+B*C
A+BC*
ABC*+
Ex2: Consider the Infix Expression A*B+C and convert it into Postfix Expression.
Sol: if in stack priority of top is greater than or equal to in coming priority of symbol then
pop the elements from the stack and poped elements are send to postfix expression and
then push the upcoming symbol.
A A
* * A
B * AB
+ + AB*
C + AB*C
Verification: A*B+C
AB*+C
AB*+
Ex3: Consider the Infix Expression (A+B)*C and convert it into Postfix Expression.
Sol: If it is the left parenthesis directly push it into the stack if is the right parenthesis then
pop the elements from the stack until left parenthesis is found and poped elements are send
to postfix expression and then discard the left and right parenthesis.
( (
A A
+ (+ A
B (+ AB
) AB+
* * AB+
C * AB+C
Verification: (A+B)*C
AB+ * C
AB+C*
Algorithm for conversion of Infix Expression to Postfix Expression:
Step1: Create empty stack.
Step2: while(not end of input)
{
Symbol=next input character
if(symbol is operand)
Send to Postfix Expression
else if(symbol = ‘(‘ )
push(stack, symbol)
else if(symbol=’)’)
{
Pop the elements from the stack until left parenthesis is found and
popped elements are send to postfix expression and also delete left
and right parenthesis.
}
else if(symbol is operator)
{
if (isp(top)>=icp(symbol))
Pop the elements from the stack and popped elements are send to
postfix expression.
push (stack,symbol)
}
}
Pop the elements from the stack until stack is empty and popped elements
are
send to postfix Expression.
OUTPUT:
#include<stdio.h>
struct stack
char item[10];
int top;
};
struct stack s;
void main()
char infix[10],postfix[10];
s.top=-1;
gets(infix);
con(infix,postfix);
puts(postfix);
int i,j=0;
char b,c;
push(&s,'#');
for(i=0;infix[i]!='\0';i++)
c=infix[i];
if(c=='(')
push(&s,c);
else if(c>='A'&&c<='Z')
postfix[j++]=c;
else if((c=='+')||(c=='-
')||(c=='*')||(c=='/')||(c=='*')||(c=='$'))
while(isp(s.item[s.top])>=icp(c))
postfix[j++]=pop(&s);
push(&s,c);
}
else if(c==')')
while((b=pop(&s))!='(')
postfix[j++]=b;
}//end of for
b=pop(&s);
while(b!='#')
postfix[j++]=b;
b=pop(&s);
postfix[j]='\0';
int isp(char x)
switch(x)
case '$':return 4;
case '*':return 3;
case '/':return 3;
case '+':return 2;
case '-':return 2;
case '(':return 1;
case '#':return 0;
}
int icp(char x)
switch(x)
case '$':return 4;
case '*':return 3;
case '/':return 3;
case '+':return 2;
case '-':return 2;
ps->top=ps->top+1;
ps->item[ps->top]=x;
char y;
y=ps->item[ps->top];
ps->top=ps->top-1;
return y;
OUTPUT:
III. Recursion: The function calling itself is called as recursion. The recursion is
implemented by using Stack.
Ex: The recursive function for finding the Factorial of a given number.
Algorithm to find the factorial of a given number
Step1:Factorial of given number is calculated by using formula
fact(n)=n*(n-1)*(n-2)*………….1 , if n>0
Step2:if( n==0) ,then
return 1;
/* Recursive function for factorial of a given
number */
#include<stdio.h>
int fact(int);
void main()
{
int n,f;
printf("\n enter n");
scanf("%d",&n);
f=fact(n);
printf("fact of %d is %d",n,f);
}
int fact(int n)
{
int x;
if(n==0)
return 1;
x=n-1;
return (n*fact(x));
}
OUTPUT:
Table 1.4 Factorial Evaluation using Stack
Ex: The recursive function for Multiplication of Two Natural Numbers.
Algorithm for Multiplication of Two Natural Numbers.
Step1: Multiplication of Two Natural Numbers are calculated by using the
formula a*b=a*(b-1) + a, if b>1
Step2:if( b==1) then
return a;
//Recursive function for Multiplication of Two Natural Numbers.
#include<stdio.h>
void main()
{
int a,b,t;
clrscr();
printf("\n a and b");
scanf("%d%d",&a,&b);
t=mul(a,b);
printf("\n mul two natural numbers=%d",t);
}
int mul(int a,int b)
{
int c;
if(b==1)
return a;
c=b-1;
return (mul(a,c)+a);
}
Output:
Ex: The recursive function for Greatest Common Divisor of Two numbers
Algorithm for GCD(m,n)
Step1:if(n>m) then
Return GCD(n,m)
Step2:if( n==0) then
return m;
otherwise
return GCD(n,mod(m,n));
Step1: Enter array size, array elements in sorted order and key element
Step2: Repeat Step 3 until (low<=high)
Step 3:low=0,high=n and find the mid=(low+high)/2
if(key<a[mid])
{
high=mid-1;
Search the key element low to a[mid-1];
}
else if(key>a[mid])
{
low=mid+1;
Search the key element from a[mid+1] to high;
}
else if(key==a[mid])
return (mid+1);
else
return 0;