You are on page 1of 5

Assignment

Name
Muhammad Hamza
Roll No
F22NSEEN1M01006
Submitted to
Dr. Hina Afreen

Department of Software Engineering


The Islamia university of Bahawalpur
( BWN CAMPUS)
1. Write a program that push items on stack, pop items from stack and also
perform peek operation collectively.

#include <iostream>
#include <stack>
using namespace std;
class MyStack {
private:
stack<int> data;
public:
// Function to push an item onto the stack
void pushItem(int value) {
data.push(value);
cout << "Pushed: " << value << endl;
}

// Function to pop an item from the stack


void popItem() {
if (!data.empty()) {
int topValue = data.top(); // Get the top element
data.pop(); // Remove the top element
cout << "Popped: " << topValue << endl;
} else {
cout << "Stack is empty. Cannot pop." << endl;
}
}

// Function to peek at the top item without removing it


void peek() {
if (!data.empty()) {
int topValue = data.top(); // Get the top element
cout << "Peeked: " << topValue << endl;
} else {
cout << "Stack is empty. Cannot peek." << endl;
}
}
};
int main() {
MyStack myStack; // Creating an instance of the MyStack class

// Pushing items onto the stack


myStack.pushItem(7);
myStack.pushItem(16);
myStack.pushItem(27);
myStack.pushItem(37);
myStack.pushItem(47);

// Performing peek operation


myStack.peek();

// Popping items from the stack


myStack.popItem();
myStack.popItem();
myStack.popItem();
myStack.popItem();
myStack.popItem(); // Attempting to pop from an empty stack

// Performing peek operation on an empty stack


myStack.peek();

return 0;
}

Output:

Pushed: 7
Pushed: 16
Pushed: 27
Pushed: 37
Pushed: 47
Peeked: 47
Popped: 47
Popped: 37
Popped: 27
Popped: 16
Popped: 7
Stack is empty. Cannot peek.
2. Write a program that convert infix expression to postfix expression.

#include <iostream>
#include <stack>

using namespace std;

// Function to check if a character is an operator


bool isOperator(char c) {
return c == '+' || c == '-' || c == '*' || c == '/' || c == '^';
}

// Function to get the precedence of an operator


int getPrecedence(char op) {
if (op == '+' || op == '-')
return 1;
if (op == '*' || op == '/')
return 2;
if (op == '^')
return 3;
return 0; // Default precedence for non-operators
}

// Function to convert infix expression to postfix expression


string infixToPostfix(const string& infixExpression) {
stack<char> operators;
string postfixExpression = "";

for (char c : infixExpression) {


if (isalnum(c)) {
postfixExpression += c;
} else if (c == '(') {
operators.push(c);
} else if (c == ')') {
while (!operators.empty() && operators.top() != '(') {
postfixExpression += operators.top();
operators.pop();
}
operators.pop();
} else if (isOperator(c)) {
while (!operators.empty() && getPrecedence(operators.top()) >=
getPrecedence(c)) {
postfixExpression += operators.top();
operators.pop();
}
operators.push(c); }
}

while (!operators.empty()) {
postfixExpression += operators.top();
operators.pop();
}

return postfixExpression;
}

int main() {
string infixExpression;

cout << "Enter an infix expression: ";


getline(cin, infixExpression);

string postfixExpression = infixToPostfix(infixExpression);

cout << "Infix Expression: " << infixExpression << endl;


cout << "Postfix Expression: " << postfixExpression << endl;

return 0;
}

Output:

Enter an infix expression: 8*(5^4+2)-6^2/(9*3)


Infix Expression: 8*(5^4+2)-6^2/(9*3)
Postfix Expression: 854^2+*62^93*/-

You might also like