Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class LinkedStack{
  2.     private int size;
  3.     private Node head;
  4.    
  5.     public LinkedStack(){        
  6.         head=new Node(null);
  7.         size=0;
  8.     }
  9.    
  10.     public void push(Object data){
  11.         Node temp=new Node(data);
  12.         temp.next=head.next;
  13.         head.next=temp;
  14.         size++;
  15.        
  16.     }
  17.    
  18.     public void pop(){
  19.         if(size==0){
  20.             System.out.println("Stack Under Flow");
  21.         }
  22.         else{
  23.             size--;
  24.             Node temp= head;
  25.             temp.next=temp.next.next;
  26.         }
  27.     }
  28.    
  29.     public Object top(){
  30.         return head.next.data;
  31.     }
  32.    
  33.     public boolean isEmpty(){
  34.         return (size==0);
  35.     }
  36.    
  37.     public boolean isFull(){
  38.         return false;
  39.     }
  40.    
  41.     public void display(){
  42.         if(isEmpty()){
  43.             System.out.println("(Empty)");
  44.         }
  45.         else{
  46.             System.out.println("xxxxxx");
  47.             Node temp=head.next;
  48.             while(temp!=null){
  49.                 System.out.printf("|%4d",temp.data);
  50.                 System.out.println("|");
  51.                
  52.                 temp=temp.next;
  53.             }
  54.             System.out.println("xxxxxx");
  55.         }        
  56.     }
  57.    
  58.     private class Node
  59.     {
  60.         protected Node next;
  61.         protected Object data;
  62.        
  63.         public Node(Object _data)
  64.         {
  65.             next = null;
  66.             data = _data;
  67.         }
  68.  
  69.         public Node(Object _data, Node _next)
  70.         {
  71.             next = _next;
  72.             data = _data;
  73.         }
  74.  
  75.     }
  76. }