Create a Stack using a 2D array in C++ I am trying to create a stack that resemb
ID: 3563713 • Letter: C
Question
Create a Stack using a 2D array in C++
I am trying to create a stack that resembles the effect of a linked list with 5 functions. I was wondering if someone could show how to construct a 2D array that has the maximum space of 25
Please add lots of comments.
Then create an empty stack (with capacity=25) and then start push 10 values (1,2,3,4,5,6,7,8,9,10) by Push( ) consecutively. After that, you will have 10 elements in your STACK. Run Display ( ) to print out the STACK. Then perform 3 times of Push( ) of value (20,30,40) on valid positions, run Display( ) to print out the list every time you insert a new element. Then perform 3 times of Top() and Pop( ), again, run Display( ) to print our the list every time you delete an element.
To visualize the requirement, the code should looks like:
Push(1)
Push(2)
Push(3)
Push(4)
Push(5)
Push(6)
Push(7)
Push(8)
Push(9)
Push(10)
Display()
Push(20)
Display()
Push(30)
Display()
Push(40)
Display()
Top()
Pop()
Display()
Top()
Pop()
Display()
Top()
Pop()
Display()
Explanation / Answer
#include int push(char [][15],int); int pop(int); void display(char [][15],int); void main() { char names[][15]={"Ed Brown","Ann Smith","Sue Jones"}; int count=3; puts("The original stack is as follows:"); display(names,count); puts("After popping the top name, the stack is as follows:"); display(names,pop(count)); push(names,count); puts("After pushing on a name, the stack is as follows:"); display(names,count); } int pop(int count) { return --count; } void display(char n[][15],int count) { int i; for(i=(count-1);i>=0;--i) printf("%s ",(n+i)); puts(""); } int push(char n[][15],int count) { puts("Enter a name:"); gets(n[count-1]); puts(""); return count++; }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.