Write an interactive menu driven c++ program to implement stack using linked list. 1.Push an element in stack 2.Pop an element in stack. 3.Show the contents of the stack. 4.Exit
1
Expert's answer
2013-02-05T11:10:42-0500
#include <iostream>
using namespace std;
void main() { char choice; int tmp;
int stack[100]; int stackLength = 0;
cout << endl; cout << " 1. Push an element into the stack" << endl; cout << " 2. Pop an element out of the stack" << endl; cout << " 3. Show the contents of the stack" << endl; cout << " 4. Exit" << endl << endl;
while (true) { cout << "Choose the menu item: "; cin >> choice;
switch (choice) { case '1': cout << "Enter and integer value: "; cin >> tmp;
case '2': if (stackLength > 0) { stackLength--; cout << "The following element has been popped: " << stack[stackLength] << endl << endl; } else cout << "The stack is empty." << endl << endl; break;
case '3': if (stackLength == 0) { cout << "Stack is empty." << endl << endl; break; }
Comments
Leave a comment