Public class stack { private Listing[] data; private int top; private int siza;
ID: 3811536 • Letter: P
Question
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
Explanation / Answer
/*Stack.java*/
public class Stack
{ private Listing[] data;
private int top;
private int size;
public Stack() // default constructor
{ top=-1;
size=100;
data=new Listing[100];
}
public Stack(int n) // parameterised constructor
{
top=-1;
size=n;
data=new Listing[n];
}
//Pushes a new element into the stack, if the stack is not full
public boolean push(Listing newNode)
{
if(isStackFull())
{
System.out.println("Stack is overflow");
return false;
}
else
{
top=top+1;
data[top]=newNode.deepCopy();
return true;
}
}
//Pops an element that is pushed recently, if the stack is not empty
public Listing pop()
{
int topLocation;
if(isStackEmpty())
{
System.out.println("Stack is underflow");
return null;
}
else
{
topLocation=top;
top=top-1;
return data[topLocation];
}
}
//Displays all the elements in the stack
public void showAll()
{
for(int i=top;i>=0;i--)
System.out.println(data[i].toString());
}
//Reinitializes the stack or makes the stack empty
public void reInitStack()
{
top=-1;
}
//Returns true, if the stack is empty
//otherwise, returns false.
public boolean isStackEmpty()
{
if(top==-1)
return true;
else
return false;
}
//Returns true, if the stack is full
//otherwise, returns false.
public boolean isStackFull()
{
if(top==size-1)
return true;
else
return false;
}
//Returns the top element of the stack
//with out removing the top element.
public Listing peek()
{
if(isStackEmpty())
{
System.out.println("Stack is underflow");
return null;
}
else
{
return data[top];
}
}
//Expands the size of the stack
public void expandStack()
{
Listing[] buff;
buff=new Listing[size*2];
for(int i=0;i<size;i++)
{
buff[i]=data[i];
}
size=size*2;
data=buff;
}
}
/*DriverStack.java*/
public class DriverStack
{
public static void main(String args[])
{
Stack s=new Stack(2);//Stack of size only 2
Listing l1=new Listing("Philp","1st Avenue","123 4567");
Listing l2=new Listing("John","2nd Avenue","456 3232");
Listing l3=new Listing("Joy","3rd Avenue","444 4444");
Listing l4=new Listing("David","4th Avenue","765 4321");
s.push(l1);
s.push(l2);
System.out.println("Top element : "+s.peek());//Display top element
s.push(l3);//overflow error
System.out.println("Current stack elements :");
s.showAll();//Display l1 and l2
s.expandStack();//Size becomes double, that is 4
s.push(l3);//Now,push l3
System.out.println("Current stack elements :");
s.showAll();//Display l1,l2 and l3
s.pop();//pop l3
s.pop();//pop l2
System.out.println("Current stack elements :");
s.showAll();//Display l1
s.pop();//Pop l1
s.pop();//Underflow error
s.reInitStack();//Make the stack empty
s.push(l4);//push l4
System.out.println("Current stack elements :");
s.showAll();//Display l4
}
}
/*Listing.java*/
public class Listing
{
private String name;
private String address;
private String number;
public Listing()
{
name=" ";
address=" ";
number =" ";
}
public Listing(String n,String a, String num)
{
name=n;
address=a;
number =num;
}
public String toString()
{
return ("Name is "+ name + " " +
"Address is " + address + " " +
"Number is " + number + " ");
}
public Listing deepCopy()
{
Listing clone=newListing(name,address,number);
return clone;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.