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

#include <iostream>

#include <cstring>

using namespace std;

class Node {
public:
string data;
Node* next;

Node(string value) : data(value), next(nullptr) {}


};

class Stack {
private:
Node* top;

public:
Stack() : top(nullptr) {}

~Stack() {
while (!isEmpty()) {
pop();
}
}

bool isEmpty() {
return top == nullptr;
}

void push(string value) {


Node* newNode = new Node(value);
newNode->next = top;
top = newNode;
cout << "Pushed: " << value << endl;
}

void pop() {
if (isEmpty()) {
cout << "Stack underflow! Cannot pop element.\n";
return;
}

Node* temp = top;


top = top->next;
cout << "Popped: " << temp->data << endl;
delete temp;
}

string getTop() {
if (isEmpty()) {
return "Stack is empty";
}
return top->data;
}

void printStack() {
if (isEmpty()) {
cout << "Stack is empty.\n";
return;
}

cout << "Stack content:\n";


Node* current = top;
while (current != nullptr) {
cout << current->data << endl;
current = current->next;
}
}
};

int main() {
Stack stack;

int choice;
do {
cout << "\nStack Menu:\n"
<< "1. Push\n"
<< "2. Pop\n"
<< "3. Get Top\n"
<< "4. Print Stack\n"
<< "5. Check if Stack is Empty\n"
<< "6. Quit\n"
<< "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1: {
string value;
cout << "Enter the element to push: ";
cin >> value;
stack.push(value);
break;
}
case 2:
stack.pop();
break;
case 3:
cout << "Top of the stack: " << stack.getTop() << endl;
break;
case 4:
stack.printStack();
break;
case 5:
cout << (stack.isEmpty() ? "Stack is empty" : "Stack is not empty")
<< endl;
break;
case 6:
cout << "Quitting the program.\n";
break;
default:
cout << "Invalid choice. Please try again.\n";
}
} while (choice != 6);

return 0;
}
#include <iostream>
#include <cstring>

using namespace std;

const int MAX_SIZE = 100;

class Stack {
private:
string arr[MAX_SIZE];
int top;

public:
Stack() : top(-1) {}

bool isEmpty() {
return top == -1;
}

int getSize() {
return top + 1;
}

void push(string value) {


if (top == MAX_SIZE - 1) {
cout << "Stack overflow! Cannot push element.\n";
return;
}
arr[++top] = value;
cout << "Pushed: " << value << endl;
}

void pop() {
if (isEmpty()) {
cout << "Stack underflow! Cannot pop element.\n";
return;
}
cout << "Popped: " << arr[top--] << endl;
}

string getTop() {
if (isEmpty()) {
return "Stack is empty";
}
return arr[top];
}

void printStack() {
if (isEmpty()) {
cout << "Stack is empty.\n";
return;
}
cout << "Stack content:\n";
for (int i = top; i >= 0; --i) {
cout << arr[i] << endl;
}
}
};

int main() {
Stack stack;

int choice;
do {
cout << "\nStack Menu:\n"
<< "1. Push\n"
<< "2. Pop\n"
<< "3. Get Top\n"
<< "4. Print Stack\n"
<< "5. Check if Stack is Empty\n"
<< "6. Get Stack Size\n"
<< "7. Quit\n"
<< "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1: {
string value;
cout << "Enter the element to push: ";
cin >> value;
stack.push(value);
break;
}
case 2:
stack.pop();
break;
case 3:
cout << "Top of the stack: " << stack.getTop() << endl;
break;
case 4:
stack.printStack();
break;
case 5:
cout << (stack.isEmpty() ? "Stack is empty" : "Stack is not empty")
<< endl;
break;
case 6:
cout << "Stack size: " << stack.getSize() << endl;
break;
case 7:
cout << "Quitting the program.\n";
break;
default:
cout << "Invalid choice. Please try again.\n";
}
} while (choice != 7);

return 0;
}

#include <iostream>
#include <string>
#include <cctype>

using namespace std;

class Stack {
private:
static const int MAX_SIZE = 100;
string arr[MAX_SIZE];
int top;

public:
Stack() : top(-1) {}

bool isEmpty() {
return top == -1;
}

int getSize() {
return top + 1;
}

void push(string value) {


if (top == MAX_SIZE - 1) {
cout << "Stack overflow! Cannot push element.\n";
return;
}
arr[++top] = value;
cout << "Pushed: " << value << endl;
}

void pop() {
if (isEmpty()) {
cout << "Stack underflow! Cannot pop element.\n";
return;
}
cout << "Popped: " << arr[top--] << endl;
}

string getTop() {
if (isEmpty()) {
return "Stack is empty";
}
return arr[top];
}

void printStack() {
if (isEmpty()) {
cout << "Stack is empty.\n";
return;
}
cout << "Stack content:\n";
for (int i = top; i >= 0; --i) {
cout << arr[i] << endl;
}
}
};

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

int precedence(char op) {


if (op == '+' || op == '-') {
return 1;
} else if (op == '*' || op == '/') {
return 2;
}
return 0;
}

string infixToPostfix(string infix) {


Stack stack;
string postfix;

for (char c : infix) {


if (isalnum(c)) {
postfix += c;
} else if (c == '(') {
stack.push(string(1, c));
} else if (c == ')') {
while (!stack.isEmpty() && stack.getTop() != "(") {
postfix += stack.getTop();
stack.pop();
}
stack.pop(); // Pop '('
} else { // Operator
while (!stack.isEmpty() && precedence(c) <= precedence(stack.getTop()
[0])) {
postfix += stack.getTop();
stack.pop();
}
stack.push(string(1, c));
}
}

while (!stack.isEmpty()) {
postfix += stack.getTop();
stack.pop();
}

return postfix;
}

float evaluatePostfix(string postfix) {


Stack stack;

for (char c : postfix) {


if (isdigit(c)) {
stack.push(string(1, c));
} else {
float operand2 = stof(stack.getTop());
stack.pop();
float operand1 = stof(stack.getTop());
stack.pop();

switch (c) {
case '+':
stack.push(to_string(operand1 + operand2));
break;
case '-':
stack.push(to_string(operand1 - operand2));
break;
case '*':
stack.push(to_string(operand1 * operand2));
break;
case '/':
stack.push(to_string(operand1 / operand2));
break;
}
}
}

return stof(stack.getTop());
}

int main() {
string infix, postfix;
cout << "Enter an infix expression: ";
getline(cin, infix);

postfix = infixToPostfix(infix);
cout << "Postfix expression: " << postfix << endl;

float result = evaluatePostfix(postfix);


cout << "Result: " << result << endl;

return 0;
}

You might also like