Name-Shailendra Mukati Reg. no.
- 21MCA0218
Subject-Problem solving with data structure and algorithms Subject code- ITA5002
//Compiler- dev c++
//1. Implementation in stack using array
#include<stdio.h> void pop()
#include<conio.h> {
#include<process.h> if(top==-1)
void push(); printf("\nStack is Underflow!!\a\a\a");
void pop(); else
void display(); {
int stk[5],top=-1,opt,ch=1,ele,i,max=4; printf(" \nDeleted Element is |%d|",stk[top]);
top--;
void main() printf("\nRemaining element in stack\n");
{ display();
printf("\nStack operation menu=\n"); }//else
while(ch==1) }//pop
{
printf("\n1.Push\n2.Pop\n3.Display\n4.Exit"); void display()
printf("\nEnter your choice="); {
scanf("%d",&opt); if(top==-1)
switch(opt) printf("\n stack is Empty!!\a\a\a");
{ else
case 1: push(); {
break; printf("\nStack Elements=\n");
for(i=top;i>=0;i--)
case 2: pop(); printf("|%d|\n",stk[i]);
break; getch();
}//else
case 3: display(); }//display
break;
case 4: exit(0);
default: printf("\n Wrong choice!!\a\a\a");
}//switch
}//while
}//main
void push()
{
if(top==max)
printf("\nStack is overflow!!\a\a");
else
{
top++;
printf("\nEnter Element:");
scanf("%d",&stk[top]);
}//else
printf("Element stored in array is=%d\n",stk[top]);
}//push
Name-Shailendra Mukati Reg. no.- 21MCA0218
Subject-Problem solving with data structure and algorithms Subject code- ITA5002
//Compiler- dev c++
Example:
1.Push
Stack operation menu=
2.Pop
1.Push
3.Display
2.Pop
4.Exit
3.Display
4.Exit
Enter your choice=2
Enter your choice=3
Deleted Element is |20|Remaining element in stack
stack is Empty!!
Stack Elements=
1.Push
|10|
2.Pop
3.Display
1.Push
4.Exit
2.Pop
Enter your choice=1
3.Display
4.Exit
Enter Element:10
Enter your choice=
Element stored in array is=10
2
1.Push
Deleted Element is |10|Remaining element in stack
2.Pop
3.Display
stack is Empty!!
4.Exit
Enter your choice=1
1.Push
Enter Element:20
2.Pop
Element stored in array is=20
3.Display
4.Exit
1.Push
Enter your choice=2
2.Pop
3.Display
Stack is Underflow!!
4.Exit
Enter your choice=3
1.Push
Stack Elements=
2.Pop
3.Display
|20|
4.Exit
|10|
Enter your choice=4
--------------------------------
Process exited after 38.75 seconds with return value 0
Press any key to continue . . .
Name-Shailendra Mukati Reg. no.- 21MCA0218
Subject-Problem solving with data structure and algorithms Subject code- ITA5002
//Compiler- dev c++
//2.program for convert infix to postfix notation
#include<stdio.h> int precedence(char symbol)
#include<conio.h> {
#include<stdlib.h> if(symbol=='^')
#define size 50 return 3;
char stack[size]; else if(symbol=='*'||symbol=='/')
int top=-1; return 2;
void push (char item) else if(symbol=='+'||symbol=='-')
{ return 1;
if(top>=size-1)
printf("\nStack Overflow!!\a\a\a"); else
else return 0;
{ } //precedence
top++;
stack[top]=item; int main()
}//else {
}//push char infix[size],postfix[size],item, temp;
int i=0,j=0;
char pop()
{ printf("\nEnter Infix Notation::");
char item; gets(infix);
item= stack[top];
top--; while(infix[i]!='\0')
return(item); {
} item=infix[i];
int is_operator(char symbol) if (item=='(')
{ push(item);
if(symbol=='^'||symbol=='*'||symbol=='/'||symbol=='+ else if(item>='A'&&item<='Z'||item>='a'&&item<='z')
'||symbol=='-') {
return 1; postfix[j]=item;
j++;
else }
return 0; else if(is_operator(item)==1)
}//is_operator {
temp=pop();
while((is_operator(temp)==1)&&(precedence(temp)>=
precedence(item)))
{
postfix[j]=temp;
j++;
temp=pop();
}//while
Name-Shailendra Mukati Reg. no.- 21MCA0218
Subject-Problem solving with data structure and algorithms Subject code- ITA5002
//Compiler- dev c++
push(temp);
push(item);
Example:
}//else if
else if(item==')') Enter Infix Notation::A*B+C*D
{
temp=pop();
Arithmetic expression In Postfix Notation:)
while(temp!='(')
{
postfix[j]=temp; AB*CD*+
j++;
temp=pop(); --------------------------------
}//while Process exited after 61.54 seconds with return
}//else if value 0
else Press any key to continue . . .
{
printf("\nInvalid aritmetic expression");
getch();
exit(0);
}//else Enter Infix Notation::a*b+e
i++;
}//first while Arithmetic expression In Postfix Notation:)
while(top>-1) ab*e+
{
postfix[j]=pop(); --------------------------------
j++; Process exited after 13.18 seconds with return
} value 0
postfix[j]='\0';
Press any key to continue . . .
printf("\nArithmetic expression In Postfix
Notation:)\n\n");
puts(postfix);
getch();
return 0;
}//main
Name-Shailendra Mukati Reg. no.- 21MCA0218
Subject-Problem solving with data structure and algorithms Subject code- ITA5002
//Compiler- dev c++
//3.program for convert infix to prefix notation
int precedence(char symbol)
{
#include<stdio.h> if(symbol=='^')
#include<conio.h> return 3;
#include<stdlib.h>
#include<string.h> else if(symbol=='*'||symbol=='/')
#define size 50 return 2;
char stack[size]; else if(symbol=='+'||symbol=='-')
int top=-1; return 1;
void push (char item) else
{ return 0;
if(top>=size-1) }//precedence
printf("\nStack Overflow!!\a\a\a"); int main()
else {
{ char a[size],infix[size],postfix[size],item,temp;
top++; int i=0,j=0;
stack[top]=item;
}//else printf("\nEnter Infix Notation::");
}//push gets(infix);
strrev(infix);
char pop()
{ while(infix[i]!='\0')
char item; {
item= stack[top]; item=infix[i];
top--; if (item==')')
return(item); {
} push(item);
}
int is_operator(char symbol) else if(item>='A'&&item<='Z'||item>='a'&&item<='z')
{ {
postfix[j]=item;
if(symbol=='^'||symbol=='*'||symbol=='/'||symbol=='+ j++;
'||symbol=='-') }
return 1;
else if(is_operator(item)==1)
else {
return 0; temp=pop();
}//is_operator
while((is_operator(temp)==1)&&(precedence(temp)>=p
recedence(item)))
{
postfix[j]=temp;
j++;
temp=pop();
}//while
Name-Shailendra Mukati Reg. no.- 21MCA0218
Subject-Problem solving with data structure and algorithms Subject code- ITA5002
//Compiler- dev c++
push(temp);
push(item);
Example:
}//else if
Enter Infix Notation::a*b+c*d
else if(item=='(')
{
Arithmetic expressions In Prefix Notation):+*ab*cd
temp=pop();
while(temp!=')')
--------------------------------
{
Process exited after 11.97 seconds with return value 0
postfix[j]=temp;
Press any key to continue . . .
j++;
temp=pop();
}//while
}//else if
else
Enter Infix Notation::A*B+C*D
{
printf("\n Invalid arithmetic expression");
Arithmetic expression In Prefix Notation :)
getch();
exit(0);
+*AB*CD
}//else
i++;
--------------------------------
Process exited after 61.54 seconds with return value 0
}//first while
Press any key to continue . . .
while(top>-1)
{
postfix[j]=pop();
j++;
}
postfix[j]='\0';
strrev(postfix);
printf("\n Arithmetic expression In Prefix
Notation):\n");
puts(postfix);
getch();
return 0;
}//main
Name-Shailendra Mukati Reg. no.- 21MCA0218
Subject-Problem solving with data structure and algorithms Subject code- ITA5002
//Compiler- dev c++
//4.Evalution of postfix notation
case '+':
#include<stdio.h>
{
#include<conio.h>
n3= n2+n1;
#include<ctype.h>
break;
#include<math.h>
}
int stk[5];
case '-':
int top=-1;
{
n3= n2-n1;
void push(int x)
break;
{
}
stk[++top]=x;
}
case '*':
{
int pop()
n3= n2*n1;
{
break;
return stk[top--];
}
}
case '/':
void main()
{
{
n3= n2/n1;
char post[10],*e;
break;
int n1,n2,n3,num;
}
}
printf("\nEnter the Postfix Notation");
push(n3);
gets(post);
}
e=post;
e++;
}
while(*e!='\0')
{
printf("\nThe Evaluation of Postfix=%d",pop());
if(isdigit(*e))
getch();
{
}
num=*e-48;
push(num);
}
Example:
else
{ Enter the Postfix Notation= 245+*
n1=pop(); The Evaluation of Postfix expression 245+*=18
n2=pop(); --------------------------------
Process exited after 6.784 seconds with return value
switch(*e) 13
{ Press any key to continue . . .
case '^':
{ Enter the Postfix Notation= 2542^+*
n3= pow(n2,n1); The Evaluation of Postfix expression 2542^+*=42
break; --------------------------------
} Process exited after 22.29 seconds with return value
224
Press any key to continue . . .
Name-Shailendra Mukati Reg. no.- 21MCA0218
Subject-Problem solving with data structure and algorithms Subject code- ITA5002
//Compiler- dev c++
//5.Evalution of Prefix notation
case '-':
{
#include<stdio.h>
n3= n1-n2;
#include<conio.h>
break;
#include<ctype.h>
}
#include<string.h>
#include<math.h>
case '*':
{
void push(int);
n3= n1*n2;
int pop(void);
break;
int stk[5];
}
int top=-1;
case '/':
void main()
{
{
n3= n1/n2;
char pre[10],*e;
break;
int n1,n2,n3,num;
}
}
printf("\nEnter the Prefix Notation");
push(n3);
gets(pre);
}
strrev(pre);
e++;
e=pre;
}
while(*e!='\0')
Strrev(pre);
{
printf("\nThe Evaluation of Prefix expression
if(isdigit(*e))
%s=%d",pre,pop());
{
getch();
num=*e-48;
}
push(num);
}
void push(int x)
{
else
stk[++top]=x;
{
}
n1=pop();
n2=pop();
int pop()
{
switch(*e)
return stk[top--];
{
}
case '^':
{
n3= pow(n1,n2); Example:
break;
} Enter the Prefix Notation=*+^2452
case '+': The Evaluation of Prefix expression *+^2452=42
{ --------------------------------
n3= n1+n2; Process exited after 27.01 seconds with return value
break; 13
} Press any key to continue . . .