Answered step by step
Verified Expert Solution
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 =
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started