public class LinkedStack{
private int size;
private Node head;
public LinkedStack(){
head=new Node(null);
size=0;
}
public void push(Object data){
Node temp=new Node(data);
temp.next=head.next;
head.next=temp;
size++;
}
public void pop(){
if(size==0){
System.out.println("Stack Under Flow");
}
else{
size--;
Node temp= head;
temp.next=temp.next.next;
}
}
public Object top(){
return head.next.data;
}
public boolean isEmpty(){
return (size==0);
}
public boolean isFull(){
return false;
}
public void display(){
if(isEmpty()){
System.out.println("(Empty)");
}
else{
System.out.println("xxxxxx");
Node temp=head.next;
while(temp!=null){
System.out.printf("|%4d",temp.data);
System.out.println("|");
temp=temp.next;
}
System.out.println("xxxxxx");
}
}
private class Node
{
protected Node next;
protected Object data;
public Node(Object _data)
{
next = null;
data = _data;
}
public Node(Object _data, Node _next)
{
next = _next;
data = _data;
}
}
}