Java Please Help me implement this generic stack interface using generic cicular
ID: 3770827 • Letter: J
Question
Java
Please Help me implement this generic stack interface using generic cicular linked list please
public interface StackADT {
/**
* Pushes an object onto the "top" of the stack
*/
public void push(T new_item);
/**
* Removes and returns the object on the "top" of the stack
*/
public T pop();
/**
* Returns without removing the object on the "top" of the stack
*/
public T peek();
/**
* Returns true if empty, false otherwise
*/
public boolean isEmpty();
/**
* Returns the number of objects in the stack
*/
public int size();
/**
* Returns a string representation of the stack
*/
public String toString();
}
Explanation / Answer
Answer:
Note: method definitions are created as per method declarations provided.
Code:
public interface StackADT<T>
{
private int CountVal;
private NodeLinear<T> TopVal;
public StackADT()
{
CountVal=0;
TopVal=null;
}
public void push(T new_item)
{
NodeLinear<T> tempVar=new NodeLinear<T>(new_item);
tempVar.setNext(TopVal);
TopVal=tempVar;
CountVal++;
}
public T pop()
{
T results=TopVal.getNew_item();
TopVal=TopVal.getNext();
CountVal--;
}
public T peek()
{
return TopVal.getNew_item();
}
public boolean isEmpty()
{
return (CountVal==0);
}
public int size()
{
return CountVal;
}
public String toString()
{
String results=" ";
NodeLinear curr=TopVal;
while(curr!=null)
{
results=result+curr.getNew_item();
curr=curr.getNext();
}
return results;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.