STACKS
STACKS
STACKS
ROLL NO – 2019PSC1009
SEM – 5
/* constructor */
Stack::Stack(){
tos = -1;
}
if (isEmpty())
{
cout<<" stack is empty "<<endl;
}
else
{
while(tos!=1)
{
tos--;
}
cout<<" stack is now clear "<<endl;
}
}
int main()
{
int ch;
char ans = 'y';
Stack s;
cout<<"\n";
do
{
cout<<"------------MENU---------------"<<endl;
cout<<"1.To Push into the Stack Elements"<<endl;
cout<<"2.To Pop an Element"<<endl;
cout<<"3.To Peep the top most Element"<<endl;
cout<<"4.To display the Stack"<<endl;
cout<<"5.To Clear the stack"<<endl;
cout<<"6.Exit"<<endl;
cout<<"Enter your choice:"<<endl;
cin>>ch;
switch(ch)
{
case 1:cout<<"Enter the value to be pushed: ";
cin>>ch;
s.push(ch);
break;
case 2:cout<<" is Popped from Stack";
s.pop();
break;
case 3:cout<<" is the top most element of the Stack";
s.peep();
break;
case 4:s.display();
break;
case 5:s.clear();
break;
case 6:cout<<"EXIT!!"<<endl;
break;
default:cout<<"Please choose correct option "<<endl;
}
cout<<"\nDo you want to continue(y/n): "<<endl;
cin>>ans;
}while(ans=='y');
cout<<"\n\n";
}
OUTPUT:
INFIX TO POSTFIX
CODE:
#include<iostream>
#include<stack>
using namespace std;
bool isOperator(char c)
{
if(c=='+'||c=='-'||c=='*'||c=='/'||
c=='^')
{
return true;
}
else
{
return false;
}
}
int precedence(char c)
{
if(c == '^')
return 3;
else if(c == '*' || c == '/')
return 2;
else if(c == '+' || c == '-')
return 1;
else
return -1;
}
return postfix;
}
int main()
{
return 0;
}
OUTPUT:
INFIX TO PREFIX
CODE:
#include <iostream>
#include <stack>
#include <algorithm>
bool isOperator(char c)
{
if (c == '+' || c == '-' || c == '*' || c == '/'
|| c == '^') {
return true;
}
else {
return false;
}
}
int precedence(char c)
{
if (c == '^')
return 3;
else if (c == '*' || c == '/')
return 2;
else if (c == '+' || c == '-')
return 1;
else
return -1;
}
string InfixToPrefix(stack<char> s, string
infix)
{
string prefix;
reverse(infix.begin(), infix.end());
if (s.top() == '(') {
s.pop();
}
}
else if (isOperator(infix[i])) {
if (s.empty()) {
s.push(infix[i]);
}
else {
if (precedence(infix[i]) >
precedence(s.top())) {
s.push(infix[i]);
}
else if ((precedence(infix[i]) ==
precedence(s.top()))
&& (infix[i] == '^')) {
while ((precedence(infix[i]) ==
precedence(s.top()))
&& (infix[i] == '^')) {
prefix += s.top();
s.pop();
}
s.push(infix[i]);
}
else if (precedence(infix[i]) ==
precedence(s.top())) {
s.push(infix[i]);
}
else {
while ((!s.empty()) &&
(precedence(infix[i]) <
precedence(s.top()))) {
prefix += s.top();
s.pop();
}
s.push(infix[i]);
}
}
}
}
while (!s.empty()) {
prefix += s.top();
s.pop();
}
reverse(prefix.begin(), prefix.end());
return prefix;
}
int main()
{
return 0;
}
OUTPUT:
EVALUATION OF POSTFIX
CODE:
#include<iostream>
#include<math.h>
using namespace std;
class eval
{
public:
int st[100];
int top;
char str[100];
eval()
{
top = -1;
}
void push(int ch)
{
top++;
st[top] = ch;
}
int pop()
{
int val = st[top];
top--;
return val;
}
int operation(int a,int b,char opr)
{
switch(opr)
{
case '+':
return a+b;
case '-':
return a-b;
case '*':
return a*b;
case '/':
return a/b;
case '^':
return pow(b,a);
default:
return 0;
}
}
int calculatePostfix();
};
int eval::calculatePostfix() {
int index = 0;
while(str[index]!='\0') {
if(isdigit(str[index]))
push(str[index]-'0');
}
else
{
int x = pop();
int y = pop();
int res = operation(y,x,str[index]);
push(res);
}
index++;
}
return pop();
}
int main()
{
eval E;
cout << "Enter the postfix: ";
cin >> E.str;
int res = E.calculatePostfix();
cout << "The result is " << res;
return 0;
}
OUTPUT: