DS Worksheet 2.3
DS Worksheet 2.3
Experiment 2.3
Student Name: Amit Kumar Singh UID: 21BCS11618
Branch: CSE Section/Group: 808 B
Semester: 3rd Subject Name: Data Structures
Subject Code: 21CSH-211 Date of Performance:
2. Algorithm:
3. Source Code:
#include<iostream> using
namespace std;char
stack[50]; inttop=-1;
voidpush(char p)
{ stack[++top]=p;
} char pop()
{ if(top==-1) return
-1; else return
stack[top--];
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
} int pr (char
p)
{ if(p=='^')
return 3;
if(p=='/' ||
p=='*')
return 2;
if(p=='+' || p=='-')
return 1;
return -1;
} int main()
{ char exp[100]; char *a,p;
cout<<"Enter the
expression:"<<endl; cin>>exp;
a=exp; while(*a!='\0')
{ if(isalnum(*a))
{ cout<<*a;
}else
if(*a=='(')
{ push(*a);
}else
if(*a==')')
{ while((p=pop()) != '(')
cout<<p;
}
else
{ while(pr(stack[top])>=pr(*a))
cout<<pop(); push(*a); } a++;
} while(top!=-1)
{ cout<<pop();
} return 0;
}
4. Result/Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Learning Outcomes:
• Understood the concepts of stacks.
• Implemented push and pop commands.
• Learnt the concepts of infix and postfix.