Programming using Java: Implement the ADT stack by using an array stack to conta
ID: 3564586 • Letter: P
Question
Programming using Java: Implement the ADT stack by using an array stack to contain its entries. Expand the array dynamically, as necessary. Maintain the stacks bottom entry in stack[stack.length - 1]. Use the StackInterface class (provided). Write a program to thoroughly test the ADT stack operations.
StackInterface.java
/**
An interface for the ADT stack.
*/
public interface StackInterface<T>
{
/** Adds a new entry to the top of this stack.
@param newEntry an object to be added to the stack */
public void push(T newEntry);
/** Removes and returns this stack
Explanation / Answer
ntroduce two closely-related data types for manipulating arbitrarily large collections of objects: the stack and the queue. Each is defined by two basic operations: insert a new item, and remove an item. When we insert an item, our intent is clear. But when we remove an item, which one do we choose? The rule used for a queue is to always remove the item that has been in the collection the most amount of time. This policy is known as first-in-first-out or FIFO. The rule used for a stack is to always remove the item that has been in the collection the least amount of time. This policy is known as last-in first-out or LIFO.
Representing stacks with arrays is a natural idea. The first problem that you might encounter is implementing the constructor ArrayStackOfStrings(). An instance variable a[] with an array of strings to hold the stack items is clearly needed, but how big should it be? For the moment, We will finesse this problem by having the client provide an argument for the constructor that gives the maximum stack size. We keep the items in reverse order of their arrival.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.