Answer to Question #23756 in C++ for Rohan Saha

Question #23756
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;

if (stackLength < 100)
{
stack[stackLength] = tmp;
stackLength++;
}
else
cout << "Stack is full." << endl;
cout << endl;
break;

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;
}

cout << endl;
cout << "Stack length: " << stackLength << endl;
for (int c = stackLength - 1; c >= 0; c--)
cout << "& quot; << stack[c] << endl;
cout << endl;
break;

case '4':
return;

default:
cout << "Wrong menu item number." << endl;
}
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS