Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions