Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

private Object[] theStack private int capacity, private int top: I/Constructor: Creates an empty stack public myStack(int capacity) theStack new Object(capacityl: this.capacity - capacity, top =

image text in transcribed

private Object[] theStack private int capacity, private int top: I/Constructor: Creates an empty stack public myStack(int capacity) theStack new Object(capacityl: this.capacity - capacity, top = 0; ) public boolean empty // Write your code here ) Collections: The Stack class represents a last-in-first-out (LIFO) container of objects. Here is the Stack interface: public boolean push(Object x): Pushes (adds) an item on to the top of the stack. public Object pop(): Removes the object at the top of the stack and returns that object as the return value of this function. public Object peek(): Returns the object at the top of the stack without removing it from the stack. public boolean empty(): Tests if the stack is empty. Returns true it and only if the stack contains no items; false otherwise. public int search(Object x): Returns the array position where the object is located. Returns -1 if the object is not in the stack. Below, you are given the "myStack" class, where a stack is implemented using arrays. The class has 3 data declarations. "theStack" is the Array where elements of the stack is kept. "capacity" denotes the maximum allowed capacity of myStack, and "top" is an integer denoting the first empty space in the stack. Initially, when the stack is empty the value of top is 0, denoting the first empty space in the stack is the Array's oth position. If the stack contains one element, that element is stored in Array's oth position, and the value of the top is 1, denoting the first empty space in the stack is the Array's 1st position. Complete the myStack class by writing Java code into appropriate blanks below. (Hint: The push method for the stack class is already implemented, as a hint for you.) public Object pop // Write your code here ) public boolean push(Object x) theStack(top) = x top; ) public int search(Object) [ // Write your code here public Object peek01 1/ Write your code here

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

Recommended Textbook for

Databases On The Web Designing And Programming For Network Access

Authors: Patricia Ju

1st Edition

1558515100, 978-1558515109

More Books

Students also viewed these Databases questions

Question

LO1.2 Describe the role of economic theory in economics.

Answered: 1 week ago