Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as Java by adhadh ( 14 years ago )
public class LinkedStack<T> implements StackInterface<T> {
private Node topNode;
public LinkedStack(){
topNode = null;
}
public void push(T newEntry){
Node newNode = new Node (newEntry, topNode);
topNode = newNode;
}
public T peek(){
T top = null;
if(topNode != null)
top = topNode.getData();
return top;
}
public T pop(){
T top = null;
if(topNode != null)
topNode = topNode.getNextNode();
return top;
}
public boolean isEmpty(){
return topNode == null;
}
public void clear(){
topNode = null;
}
private class Node implements java.io.Serializable{
private T data;
private Node next;
private Node(T dataPortion){
data = dataPortion;
next = null;
}
private Node(T dataPortion, Node linkPortion){
data = dataPortion;
next = linkPortion;
}
private T getData(){
return data;
}
private void setData(T newData){
data = newData;
}
private Node getNextNode(){
return next;
}
private void setNextNode(Node nextNode){
next = nextNode;
}
}
}
Revise this Paste