aznishboy

SinglyLinkedList

Mar 29th, 2012
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 5.55 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. /**
  4.  *  Implementation of lists, using singly linked elements.
  5.  *
  6.  * @author     G. Peck
  7.  * @created    April 27, 2002
  8.  */
  9. public class SinglyLinkedList{
  10.     private ListNode first;  // first element
  11.     private ListNode last;
  12.     private int size;
  13.     private ListNode back;
  14.     private ListNode temp;
  15.  
  16.   /**
  17.    *  Constructor for the SinglyLinkedList object
  18.    *  Generates an empty list.
  19.    */
  20.     public SinglyLinkedList(){
  21.         first = null;
  22.         last = null;
  23.     }
  24.   /**
  25.    *  Returns the first element in this list.
  26.    *
  27.    * @return  the first element in the linked list.
  28.    */
  29.     public Object getFirst(){
  30.         if (first == null){
  31.             throw new NoSuchElementException();
  32.         }
  33.         else return first.getValue();
  34.     }
  35.     public Object getLast(){
  36.         if (last == null)
  37.             throw new NoSuchElementException();
  38.         else return last.getValue();
  39.     }
  40.   /**
  41.    *  Inserts the given element at the beginning of this list.
  42.    *
  43.    * @param  value  the element to be inserted at the beginning of this list.
  44.    */
  45.     public void addFirst(Object value){
  46.         if (first == null){
  47.             first = last = new ListNode(value, null);
  48.         }
  49.         else first = new ListNode(value, first);
  50.  
  51.         /*
  52.          *first = new ListNode(value, first);
  53.          *if (last == null)
  54.          *  last = first;
  55.          *
  56.          */
  57.     }
  58.     public void addLast(Object value){
  59.         if (first == null){
  60.             first = last = new ListNode(value, first);
  61.         }
  62.         else{
  63.             last.setNext(new ListNode(value, null));
  64.             last = last.getNext();
  65.         }
  66.     }
  67.       /**
  68.    *  Inserts the specified element at the position in this list
  69.    *  according to the natural ordering of its elements. All elements
  70.    *  in the list must implement the Comparable interface. Shifts
  71.    *  the element currently at that position (if any) and any
  72.    *  subsequent elements to the right.
  73.    *
  74.    * @param  element  element to be inserted
  75.    *create the new node with its link field pointing to temp
  76.    *
  77.     if (node is to be inserted at head of list)     // back == null
  78.        first = new node;
  79.      else
  80.         back.next = new node;
  81.     if (node is to be inserted at end of list)      // temp == null
  82.         last = new node;
  83.    */
  84.   public void insert(Comparable element){
  85.     temp = first;
  86.     back = null;
  87.     while((temp != null) && (element.compareTo(temp.getValue()) > 0)){
  88.         back = temp;
  89.         temp = temp.getNext();
  90.     }
  91.     ListNode newNode = new ListNode(element, null);
  92.     newNode.setNext(temp);
  93.     if(back == null){
  94.         first = newNode;
  95.     }
  96.     else{
  97.         back.setNext(newNode);
  98.     }
  99.     if (temp == null){
  100.         last = newNode;
  101.     }
  102.   }
  103.  
  104.   /**
  105.    *  Returns the first occurrence of the specified element, or null
  106.    *  if the List does not contain this element.
  107.    *
  108.    * @param  element  element to search for.
  109.    * @return        first occurrence of the specified element, or null
  110.    *                if the list doesn not contain the element.
  111.    */
  112.   public Object find(Comparable element){
  113.     ListNode temp = first;
  114.     ListNode back = null;
  115.     while ((temp != null) && (element.compareTo(temp.getValue())) != 0){
  116.         back = temp;
  117.         temp = temp.getNext();
  118.     }
  119.     if (temp == null)
  120.         return null;
  121.     return temp.getValue();
  122.   }
  123.  
  124.   /**
  125.    *  Removes the first occurrence of the specified element in
  126.    *  this list. If the list does not contain the element, it
  127.    *  is unchanged.
  128.    *
  129.    * @param  element  element to be removed from this list, if present.
  130.    * @return          removes first element with matching element, if any.
  131.    */
  132.   public Object remove(Object element){
  133.     ListNode temp = first;
  134.     ListNode back = null;
  135.     while ((temp!= null) && (element.equals(temp.getValue()) == false)){
  136.         back = temp;
  137.         temp = temp.getNext();
  138.     }
  139.     if (temp == null)
  140.         return null;
  141.     if (back == null)
  142.         first = temp.getNext();
  143.     else
  144.         back.setNext(temp.getNext());
  145.     return temp.getValue();
  146.   }
  147.  /**
  148.    *  Prints all the elements of the list in reverse order
  149.    */
  150.   public void printBackwards(){
  151.     printBackwards(first);
  152.   }
  153.  
  154.   /**
  155.    *  Recursive helper method to print all the elements of
  156.    *  the list in reverse order
  157.    */
  158.   private void printBackwards(ListNode temp){  
  159.         if (temp != null){
  160.             printBackwards(temp.getNext());
  161.             System.out.println(temp.getValue());
  162.         }
  163.   }
  164.   /**
  165.    *  Removes all of the elements from this list.
  166.    */
  167.   public void clear(){
  168.     first = null;
  169.     last = null;
  170.     System.out.println("clear");
  171.   }
  172.  
  173.   /**
  174.    *  Print the contents of the entire linked list
  175.    */
  176.     public void printList(){
  177.         ListNode temp = first;// start from the first node
  178.         while (temp != null){
  179.             System.out.println(temp.getValue() + " ");
  180.             temp = temp.getNext();// go to next node
  181.         }
  182.         System.out.println();
  183.     }
  184.  
  185.   /**
  186.    *  Returns a string representation of this list. The string
  187.    *  representation consists of the list's elements in order,
  188.    *  enclosed in square brackets ("[]"). Adjacent elements are
  189.    *  separated by the characters ", " (comma and space).
  190.    *
  191.    * @return    string representation of this list
  192.    */
  193.     public int size(){
  194.         ListNode temp = first;// start from the first node
  195.         size = 0;
  196.         while (temp != null){
  197.             size++;
  198.             temp = temp.getNext();
  199.         }
  200.         return size;
  201.     }
  202.     public String toString(){
  203.         String s = "[";
  204.         ListNode temp = first;  // start from the first node
  205.         while (temp != null){
  206.             s += temp.getValue(); // append the data
  207.             temp = temp.getNext();      // go to next node
  208.             if (temp != null)
  209.                 s += ", ";
  210.         }
  211.         s += "]";
  212.         return s;
  213.     }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment