STACKS

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 39

NAME – TANVEER KAUR

ROLL NO – 2019PSC1009
SEM – 5

STACKS USING ARRAY


CODE:
#include<iostream>
#define MAX 10

using namespace std;


class Stack
{
int tos; //top of stack
int num;
int stack_array[100]; //array for the stack
bool isEmpty();
bool isFull();
public:
Stack();
void push(int x);
void pop();
void peep();
void display();
void clear();
};

/* constructor */
Stack::Stack(){
tos = -1;
}

/* function to check if stack is empty */


bool Stack::isEmpty(){
return tos == -1;
}

/* function to check if stack is full */


bool Stack::isFull(){
return tos == MAX-1;
}
/* function to add an element to the top of the stack */
void Stack::push(int x){
if(!isFull()){
stack_array[++tos] = x;
}
else
cout << "Stack is Full!" << endl;
}
/* function to remove n element from top of the stack
*/
void Stack:: pop(){
if(!isEmpty()){
cout<<"Popped element is: "<<stack_array[tos];
tos--;
}
else
cout << "Stack is Empty!" << endl;
}

/* function to display the topmost element of the stack


*/
void Stack::peep(){
if(!isEmpty()){
cout<<"Topmost element is: "<<stack_array[tos];
}
else
cout << "Stack is Empty!" << endl;
}
/*function to display the entire stack --> does not make
sense */
void Stack:: display(){
if(!isEmpty()){
cout<<"\n Displaying the stack contents: ";
for(int i=0;i<=tos;i++)
cout << stack_array[i] << " ";
cout << endl;
}
else
cout << "Stack is Empty!" << endl;
}
/* function to clear the stack */
void Stack :: clear()
{

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;
}

string InfixToPostfix(stack<char> s, string


infix)
{
string postfix;
for(int i=0;i<infix.length();i++)
{
if((infix[i] >= 'a' && infix[i] <= 'z')
||(infix[i] >= 'A' && infix[i] <= 'Z'))
{
postfix+=infix[i];
}
else if(infix[i] == '(')
{
s.push(infix[i]);
}
else if(infix[i] == ')')
{
while((s.top()!='(') && (!
s.empty()))
{
char temp=s.top();
postfix+=temp;
s.pop();
}
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]=='^'))
{
s.push(infix[i]);
}
else
{
while((!
s.empty())&&( precedence(infix[i])<=prece
dence(s.top())))
{
postfix+=s.top();
s.pop();
}
s.push(infix[i]);
}
}
}
}
while(!s.empty())
{
postfix+=s.top();
s.pop();
}

return postfix;
}
int main()
{

string infix_exp, postfix_exp;


cout<<"Enter a Infix
Expression :"<<endl;
cin>>infix_exp;
stack <char> stack;
cout<<"INFIX EXPRESSION:
"<<infix_exp<<endl;
postfix_exp = InfixToPostfix(stack,
infix_exp);
cout<<endl<<"POSTFIX EXPRESSION:
"<<postfix_exp;

return 0;
}
OUTPUT:
INFIX TO PREFIX
CODE:
#include <iostream>
#include <stack>
#include <algorithm>

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;
}
string InfixToPrefix(stack<char> s, string
infix)
{
string prefix;
reverse(infix.begin(), infix.end());

for (int i = 0; i < infix.length(); i++) {


if (infix[i] == '(') {
infix[i] = ')';
}
else if (infix[i] == ')') {
infix[i] = '(';
}
}
for (int i = 0; i < infix.length(); i++) {
if ((infix[i] >= 'a' && infix[i] <= 'z') ||
(infix[i] >= 'A' && infix[i] <= 'Z')) {
prefix += infix[i];
}
else if (infix[i] == '(') {
s.push(infix[i]);
}
else if (infix[i] == ')') {
while ((s.top() != '(') && (!
s.empty())) {
prefix += s.top();
s.pop();
}

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()
{

string infix, prefix;


cout << "Enter a Infix Expression :" <<
endl;
cin >> infix;
stack<char> stack;
cout << "INFIX EXPRESSION: " << infix <<
endl;
prefix = InfixToPrefix(stack, infix);
cout << endl
<< "PREFIX EXPRESSION: " << prefix;

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:

You might also like