Question: Write a program to create a stack data structure ( do not use the inbuilt stack class ) using arrays. It should support Push, Pop

Write a program to create a stack data structure (do not use the inbuilt stack class) using arrays. It should support
Push, Pop and Peek operations.
To-do:
Create a stack using arrays with maximum array size 10.
Hint: Define an empty array of type integer with size 10
Push integers 20,40,60 into the stack.
Hint: Create a class Stack with variable defined to track topmost element of stack (top) and define a method
push which accepts an integer as an argument and inserts the argument into the array and increments the top by
1.
Print the items in stack.
Hint: Loop through the array till top and print the items.
Pop the item from the stack and print the popped item.
Hint: Add a method pop in Stack class and decrement the variable top.
Peek the stack and print the top-most element of the stack (do not remove it)
Hint: Return the topmost element of the array without decrementing top.
Print the stack.
Hint: Loop through the array till top and print the items.
CSCI 11517
Coding Guidelines
Start coding by creating a class Stack which has a variable top and an
array of size 10. Add 4 methods to Push, Pop, Peek and Print.
Inside the Push method, check if the top is greater than size of the
array. If yes, it represents Stack Overflow. If not, perform
insertion.
Inside the Pop & Peek method, check if top is less than 0. If yes, it
is Stack Underflow. If not, perform pop & peek, respectively.
Inside Print method, loop through all elements in the array till top and
print them.
In main function, create an object of Stack class and call the required
methods.CSCI 11517
Coding Guidelines
Start coding by creating a class Stack which has a variable top and an
array of size 10. Add 4 methods to Push, Pop, Peek and Print.
Inside the Push method, check if the top is greater than size of the
array. If yes, it represents Stack Overflow. If not, perform
insertion.
Inside the Pop & Peek method, check if top is less than 0. If yes, it
is Stack Underflow. If not, perform pop & peek, respectively.
Inside Print method, loop through all elements in the array till top and
print them.
In main function, create an object of Stack class and call the required
methods.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!