Question: Expand the implementation of the class Stack presented in Figure 3.13 to include methods to: reinitialize the stack to empty, test for an empty stack
Expand the implementation of the class Stack presented in Figure 3.13 to include methods to: reinitialize the stack to empty, test for an empty stack (underflow condition), test for a full stack (overflow condition), and to perform a Peek operation. Include a progressively developed driver program to demonstrate the functionality of each new method. Here is Figure 3.13
Public class stack
{
private Listing[] data;
private int top;
private int siza;
public stack()
{
top = -1;
size = 100;
data = new Listing [100];
}
public stack(int n)
{
top = -1;
size = n;
data = new Listing[n];
}
public boolean push(Listing newNode)
{
if(top== size - 1)
return false;
else
{
top =top +1;
data[top] = newNode.deepCopy();
return true;
}
}
public Listing pop ()
{
int topLocation;
if(top== -1)
return null; // underflow error
else
{
topLocation = top;
top = top - 1;
return data[topLocation];
}
}
public void showAll()
{
for (int i = top; i >=0; i- -)
System.out.println(data[i].toString());
} // end of show all method
}// end of class stack
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
