In: Computer Science
sing arrays please write a program to implement the STACK concept
after creating the Array, the user is to be presented with a menu to choose from a number of options such as pop, push, top, etc...
elements to be added on the stack are ints between 0 and 99
include a loop to re display the options (menu)
and an outer loop to restart the program
Write a C++ program to implement the Stacks concept.
the stack implementation is to perform the following operations
create_Stack()
Push (s,i)
Pop(s,i)
Top(s)
Purge(s)
Extra option to display the entire stack()
when the user runs the program he should be presented with the following menu
Stack is empty
----------Menu----------
1: Create a new stack
2: Push a value
3: Pop the value
4: Display the top value
5: Purge the stack
6: Display the whole stack
0: Exit the program
submit:
1) design puse (pseudocode)
2) source code (cpp file)
3) an output of ALLLLL scenarios
Here is the program:
#include <iostream>
using namespace std;
void create_Stack (int s[], int &i) {
i = 0;
}
void Push (int s[], int &i) {
if (i == 100)
cout << "Stack full, cannot push any more onto stack.\n";
else {
cout << "Please enter integer to push on stack: ";
cin >> s [i++];
}
}
void Pop (int s[], int &i) {
if (i == 0)
cout << "No elements in stack to pop.\n";
else
cout << "Popped from stack: " << s [--i] << endl;
}
void Top (int s[], int &i) {
if (i == 0)
cout << "No elements in stack.\n";
else
cout << "Element at top of stack is: " << s [i - 1] << endl;
}
void Purge (int s[], int &i) {
i = 0;
cout << "Stack contents purged, stack now empty.\n";
}
void Display (int s [], int &i) {
cout << "Stack contents: ";
for (int j = 0; j < i; j++)
cout << s[j] << " " << endl;
}
int main()
{
int myStack[100], depth = 0, choice = -1;
char onceMore;
do {
create_Stack (myStack, depth);
while (choice != 0) {
cout<<" (1):Create a new stack\n ";
cout<<"(2):Push a value\n ";
cout<<"(3):Pop the value\n ";
cout<<"(4):Display the top value\n ";
cout<<"(5):Purge the stack\n ";
cout<<"(6):Display the whole stack\n ";
cout<<"(0):Exit\n ";
cout<<"Please enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
create_Stack (myStack, depth);
break;
case 2:
Push (myStack, depth);
break;
case 3:
Pop (myStack, depth);
break;
case 4:
Top (myStack, depth);
break;
case 5:
Purge (myStack, depth);
break;
case 6:
Display (myStack, depth);
break;
}
}
cout << "Restart the stack program? (y/n):";
cin >> onceMore;
choice = -1;
} while (onceMore == 'y');
return 0;
}
Here is sample output:
(1):Create a new stack
(2):Push a value
(3):Pop the value
(4):Display the top value
(5):Purge the stack
(6):Display the whole stack
(0):Exit
Please enter your choice: 1
(1):Create a new stack
(2):Push a value
(3):Pop the value
(4):Display the top value
(5):Purge the stack
(6):Display the whole stack
(0):Exit
Please enter your choice: 2
Please enter integer to push on stack: 23
(1):Create a new stack
(2):Push a value
(3):Pop the value
(4):Display the top value
(5):Purge the stack
(6):Display the whole stack
(0):Exit
Please enter your choice: 2
Please enter integer to push on stack: 45
(1):Create a new stack
(2):Push a value
(3):Pop the value
(4):Display the top value
(5):Purge the stack
(6):Display the whole stack
(0):Exit
Please enter your choice: 4
Element at top of stack is: 45
(1):Create a new stack
(2):Push a value
(3):Pop the value
(4):Display the top value
(5):Purge the stack
(6):Display the whole stack
(0):Exit
Please enter your choice: 6
Stack contents: 23
45
(1):Create a new stack
(2):Push a value
(3):Pop the value
(4):Display the top value
(5):Purge the stack
(6):Display the whole stack
(0):Exit
Please enter your choice: 2
Please enter integer to push on stack: 23
(1):Create a new stack
(2):Push a value
(3):Pop the value
(4):Display the top value
(5):Purge the stack
(6):Display the whole stack
(0):Exit
Please enter your choice: 2
Please enter integer to push on stack: 66
(1):Create a new stack
(2):Push a value
(3):Pop the value
(4):Display the top value
(5):Purge the stack
(6):Display the whole stack
(0):Exit
Please enter your choice: 6
Stack contents: 23
45
23
66
(1):Create a new stack
(2):Push a value
(3):Pop the value
(4):Display the top value
(5):Purge the stack
(6):Display the whole stack
(0):Exit
Please enter your choice: 5
Stack contents purged, stack now empty.
(1):Create a new stack
(2):Push a value
(3):Pop the value
(4):Display the top value
(5):Purge the stack
(6):Display the whole stack
(0):Exit
Please enter your choice: 0
Restart the stack program? (y/n):n
Pseudo code:
Pseudo code:
-----------
while user wants to continue to use:
Create a new stack
while user wants to perform operations on the
stack:
Present menu options to user
Based on menu options, call Pop,
Push, Display, Top, Purge functions
Display output corresponding to the
actions
Exit
Pop:
If stack is not empty:
Remove top element and reduce stack
size
Push:
If stack is not full:
Add new element at top and increase
stack size
Display:
Iterate through stack to show all elements
Top:
Display top element on stack
Purge:
Remove all elements on stack
Reduce size of stack to zero