Ds lb4
Ds lb4
Ds lb4
#include <cstring>
class Node {
public:
string data;
Node* next;
class Stack {
private:
Node* top;
public:
Stack() : top(nullptr) {}
~Stack() {
while (!isEmpty()) {
pop();
}
}
bool isEmpty() {
return top == nullptr;
}
void pop() {
if (isEmpty()) {
cout << "Stack underflow! Cannot pop element.\n";
return;
}
string getTop() {
if (isEmpty()) {
return "Stack is empty";
}
return top->data;
}
void printStack() {
if (isEmpty()) {
cout << "Stack is empty.\n";
return;
}
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>
class Stack {
private:
string arr[MAX_SIZE];
int top;
public:
Stack() : top(-1) {}
bool isEmpty() {
return top == -1;
}
int getSize() {
return top + 1;
}
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>
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 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 == '/');
}
while (!stack.isEmpty()) {
postfix += stack.getTop();
stack.pop();
}
return postfix;
}
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;
return 0;
}