Dsa Lab Assignment 2 Hira[1]
Dsa Lab Assignment 2 Hira[1]
ASSIGNMENT NO 2
bool isPalindrome()
{
if (head == nullptr || head->next == nullptr)
{
return true;
}
class node
{
private:
int data;
node *next;
public:
node(int value)
{
data = value;
next = nullptr;
}
};
class stack
{
private:
node *top;
public:
stack()
{
top = nullptr;
}
void Pop() {
if (top == nullptr) {
cout << "The stack is empty!" << endl;
return;
}
Node* temp = top;
cout << top->data << " popped from the stack." << endl;
top = top->next;
delete temp;
}
int Top() {
if (top == nullptr) {
cout << "The stack is empty!" << endl;
return -1;
}
return top->data;
}
bool Empty() {
return top == nullptr;
}
};
int main() {
Stack s1;
while (true) {
cout << "\nMenu:" << endl;
cout << "1. Push" << endl;
cout << "2. Pop" << endl;
cout << "3. View Top Element" << endl;
cout << "4. Check if Stack is Empty" << endl;
cout << "5. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter the value to push: ";
cin >> value;
s1.Push(value);
break;
case 2:
s1.Pop();
break;
case 3:
value = s1.Top();
if (value != -1) {
cout << "Top element: " << value << endl;
}
break;
case 4:
if (s1.Empty()) {
cout << "The stack is empty." << endl;
} else {
cout << "The stack is not empty." << endl;
}
break;
case 5:
cout << "Exiting..." << endl;
return 0;
default:
cout << "Invalid choice! Please try again." << endl;
}
}
return 0;
}
};