Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
3 views10 pages

Lab 6

Uploaded by

mehrabmahi83
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

Lab 6

Uploaded by

mehrabmahi83
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Problem name: 1.

Stack push, pop, display operation

Source Code:
#include<iostream>

using namespace std;

void menu();

class Stack {

int arr[100];

int top;

public:

Stack() {

top = -1;

void push(int value) {

if (top >= 99) {

cout<<"Error! Stack overflow!!"<<endl;

} else {

arr[++top] = value;

cout<<value<<" pushed to stack."<<endl;

void pop() {

if (top < 0) {

cout<<"Invalid!! Stack Underflow!!"<<endl;

} else {

cout<<arr[top--]<<" popped from stack."<<endl;


}

void display() {

if (top < 0) {

cout<<"Stack is empty!!"<<endl;

} else {

cout<<"Stack elements: ";

for (int i=top; i>=0; i--) {

cout<<arr[i]<<" ";

cout<<endl;

};

Stack s;

int main() {

menu();

return 0;

void menu(){

int opt,value;

cout<<endl;

cout<<" *Menu*"<<endl;

cout<<"1.Push\n2.Pop\n3.Display\n4.Exit"<<endl;

cout<<"Enter an option: ";

cin>>opt;
switch (opt) {

case 1:

cout<<"Enter value to push: ";

cin>>value;

s.push(value);

break;

case 2:

s.pop();

break;

case 3:

s.display();

break;

case 4:

exit(1);

default:

cout<<"Invalid!!"<<endl;

menu();

Input/Output:

*Menu*

1.Push

2.Pop
3.Display

4.Exit

Enter an option: 1

Enter value to push: 11

11 pushed to stack.

*Menu*

1.Push

2.Pop

3.Display

4.Exit

Enter an option: 1

Enter value to push: 12

12 pushed to stack.

*Menu*

1.Push

2.Pop

3.Display

4.Exit

Enter an option: 1

Enter value to push: 13

13 pushed to stack.

*Menu*

1.Push

2.Pop

3.Display

4.Exit

Enter an option: 3

Stack elements: 13 12 11
*Menu*

1.Push

2.Pop

3.Display

4.Exit

Enter an option: 2

13 popped from stack.

*Menu*

1.Push

2.Pop

3.Display

4.Exit

Enter an option: 3

Stack elements: 12 11

*Menu*

1.Push

2.Pop

3.Display

4.Exit

Enter an option: 4

Problem name: 2. Infix to Postfix and Prefix Conversion

Source Code:
#include<iostream>

using namespace std;


class Stack {

char arr[100];

int top;

public:

Stack() {

top = -1;

void push(char ch) {

if(top < 99){

arr[++top] = ch;

else{

cout<<"Invalid!!"<<endl;

char pop() {

if (top == -1){

cout<<"Stack is empty!!"<<endl;

return arr[top--];

char peek() {

if (top == -1){

cout<<"Stack is empty!!"<<endl;

return arr[top];

int isEmpty() {

return top == -1;

}
};

int isOperator(char ch) {

return (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^');

int precedence(char ch) {

if (ch == '+' || ch == '-')

return 1;

if (ch == '*' || ch == '/')

return 2;

if (ch == '^')

return 3;

return 0;

string infixToPostfix(string infix) {

Stack st;

string postfix = "";

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

char ch = infix[i];

if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9'))

postfix += ch;

else if (ch == '(')

st.push(ch);

else if (ch == ')') {

while (!st.isEmpty() && st.peek() != '(')

postfix += st.pop();

if (!st.isEmpty())

st.pop();

}
else if (isOperator(ch)) {

while (!st.isEmpty() && precedence(st.peek()) >= precedence(ch))

postfix += st.pop();

st.push(ch);

while (!st.isEmpty())

postfix += st.pop();

return postfix;

string inf_to_prefix(string revinfix) {

Stack st;

string postfix = "";

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

char ch = revinfix[i];

if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9'))

postfix += ch;

else if (ch == '(')

st.push(ch);

else if (ch == ')') {

while (!st.isEmpty() && st.peek() != '(')

postfix += st.pop();

if (!st.isEmpty())

st.pop();

else if (isOperator(ch)) {

while (!st.isEmpty() && precedence(st.peek()) > precedence(ch))

postfix += st.pop();

st.push(ch);

}
}

while (!st.isEmpty())

postfix += st.pop();

return postfix;

string reverseStr(string str) {

string rev = "";

for (int i = str.length() - 1; i >= 0; i--) {

if (str[i] == '(')

rev += ')';

else if (str[i] == ')')

rev += '(';

else

rev += str[i];

return rev;

string infixToPrefix(string infix) {

string reversedInfix = reverseStr(infix);

string reversedPrefix = inf_to_prefix(reversedInfix);

string prefix = reverseStr(reversedPrefix);

return prefix;

int main() {

string infix;

cout<<"Enter infix expression: ";

cin>>infix;
string postfix = infixToPostfix(infix);

string prefix = infixToPrefix(infix);

cout<<"Postfix expression: "<<postfix<<endl;

cout<<"Prefix expression: "<<prefix<<endl;

return 0;

Input/Output:
Enter infix expression: A+B*C

Postfix expression: ABC*+

Prefix expression: +A*BC

You might also like