Question: Implementing the size() and toString() method public interface MyStackInterface { /** Gets the current number of data in this stack. @return the integer number of
Implementing the size() and toString() method
public interface MyStackInterface
/** Gets the current number of data in this stack. @return the integer number of entries currently in the stack*/ public int size();
/** Adds a new data to the top of this stack. @param aData an object to be added to the stack */ public void push(T aData); /** Removes and returns this stack's top data. @return either the object at the top of the stack or, if the stack is empty before the operation, null */ public T pop(); /** Retrieves this stack's top data. @return either the data at the top of the stack or null if the stack is empty */ public T peek(); /** Detects whether this stack is empty. @return true if the stack is empty */ public boolean empty(); /** Removes all data from this stack */ public void clear(); } // end MyStackInterface
--------------------------------------------------------------------------------------------
public class MyLinkedStack
{
// Data fields
private StackNode top; // references the first node in the chain
private int count; // number of data in this stack
public MyLinkedStack()
{
// add stataments
} // end default constructor
public void push(T newData)
{
// add stataments
} // end push
public T peek()
{
// add stataments
return null;
} // end peek
public T pop()
{
// add stataments
return null;
} // end pop
public int size()
{
// add stataments
return 0;
} // end size
public boolean empty()
{
// add stataments
return false;
} // end empty
public void clear()
{
// add stataments
} // end clear
public String toString()
{
// add stataments
// note: data class in stack must implement toString() method
// return a list of data in Stack, separate them with ','
return null;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
