Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.  * creating LinkedList that use private variables of Node
  3.  * Single LinkedList
  4.  *
  5.  * @author MUHAMMAD AZRI BIN JASNI @ ABDUL RANI
  6.  * @version 10 OCTOBER 2012
  7.  */
  8.  
  9. public class LinkedList
  10. {
  11.     public Node head;//head should always point to the first nod
  12.     public Node tail;
  13.     public Node current;
  14.     private int counter=0;
  15.    
  16.     public LinkedList() {}
  17.    
  18.     public int size()
  19.     {
  20.         return counter;
  21.     }
  22.    
  23.     public boolean isEmpty()
  24.     {
  25.         return (head==null);
  26.         //return (size()==0);
  27.     }
  28.    
  29.     public Node getFirst()
  30.     {
  31.         return head;
  32.     }
  33.    
  34.     public Node getNext()
  35.     {
  36.         //return current.link;
  37.         return current.getLink();
  38.     }
  39.    
  40.     public void insertAtFront(int data)
  41.     {
  42.         Node newNode = new Node(data);
  43.         if (isEmpty())
  44.         {
  45.             //head = new Node();
  46.             //tail = new Node();
  47.             head = newNode;
  48.             tail = newNode;
  49.         }
  50.         else
  51.         {
  52.            newNode.setLink(head); //link of newNode was set to be the same as address stored in head
  53.            head = newNode; //head store the address of newNode
  54.         }
  55.         newNode = null;
  56.         counter++;
  57.     }
  58.    
  59.     public void insertAtBack(int data)
  60.     {
  61.         Node newNode = new Node(data);
  62.         if (isEmpty())
  63.         {
  64.             head = newNode;
  65.             tail = newNode;
  66.         }
  67.         else
  68.         {
  69.             tail.setLink(newNode);
  70.             tail = tail.getLink();
  71.         }
  72.         newNode = null;//remove reference at the end of operation
  73.         counter++;
  74.     }
  75.    
  76.     public void removeFromFront()
  77.     {
  78.         if (isEmpty())
  79.         {
  80.             System.err.println("List is Empty, can't remove element from front.");
  81.         }
  82.         else
  83.         {
  84.             current = head;
  85.             head = head.getLink();
  86.             current.setLink(null);
  87.             counter--;
  88.             current = null;//remove reference at the end of operation
  89.         }
  90.     }
  91.    
  92.     public void removeFromBack()
  93.     {
  94.         if (isEmpty())
  95.         {
  96.             System.err.println("List is Empty, can't remove element from back.");
  97.         }
  98.         else
  99.         {
  100.             current = head;
  101.  
  102.             while (current.getLink()!=tail)
  103.             {
  104.                 current = current.getLink();
  105.             }
  106.             tail = current;
  107.             current.setLink(null);
  108.             counter--;
  109.             current = null;//remove reference at the end of operation
  110.         }
  111.     }
  112.    
  113.     public void display()
  114.     {
  115.         //current = new Node();
  116.         String output = "[";
  117.         if (! isEmpty())
  118.         {
  119.             current = head;//take address so it point to first node
  120.             while (current != null)
  121.             {
  122.                 output += current.getData();
  123.                 if (current.getLink() != null)
  124.                 {
  125.                     output += ", ";
  126.                 }
  127.                 current = getNext();//current = current.link;
  128.             }
  129.         }
  130.         output += "]";
  131.         System.out.println(output);
  132.         current = null;//remove reference
  133.     }
  134. }