Advertisement
Aldin-SXR

LinkedList.java

Mar 5th, 2020
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1. package ds.linked.singly;
  2.  
  3. import java.util.Iterator;
  4.  
  5. public class LinkedList<Item> implements Iterable<Item>{
  6.    
  7.     Node<Item> head;
  8.     int size = 0;
  9.    
  10.     /* Add a new item to the beginning of the list */
  11.     public void addToFront(Item item) {
  12.         Node<Item> newNode = new Node<Item>();  // 1
  13.         newNode.data = item;                    // 1
  14.         newNode.next = head;                    // 2
  15.         head = newNode;                         // 3
  16.         size++;                                 // 4
  17.     }
  18.    
  19.     /* Remove an item from the beginning of the list */
  20.     public void removeFromFront() {
  21.         if (head == null) {                                                     // 1
  22.             throw new IndexOutOfBoundsException("The linked list is empty.");   // 1
  23.         }                                                                       // 1
  24.         head = head.next;                                                       // 2
  25.         size--;                                                                 // 3
  26.     }
  27.    
  28.     /* Add a new item to the end of the list */
  29.     public void addToRear(Item item) {
  30.         Node<Item> newNode = new Node<Item>();      // 1
  31.         newNode.data = item;                        // 1
  32.        
  33.         if (head == null) {                         // 2
  34.             head = newNode;                         // 2
  35.         } else {           
  36.             Node<Item> current = head;              // 3
  37.             while (current.next != null) {          // 4
  38.                 current = current.next;             // 4
  39.             }                                       // 4
  40.             current.next = newNode;                 // 5
  41.         }
  42.         size++;                                     // 6
  43.     }
  44.    
  45.     /* Remove an item from the end of the list */
  46.     public void removeFromRear() {
  47.         if (head == null) {                                                     // 1
  48.             throw new IndexOutOfBoundsException("The linked list is empty.");   // 1
  49.         } else if (size == 1) {                                                 // 2
  50.             head = null;                                                        // 2
  51.         } else {                                                                // 3
  52.             Node<Item> current = head;                                          // 3
  53.             while (current.next.next != null) {                                 // 4
  54.                 current = current.next;                                         // 4
  55.             }                                                                   // 4
  56.             current.next = null;                                                // 5
  57.         }
  58.         size--;                                                                 // 6
  59.     }
  60.    
  61.     /* Return the size of the linked list */
  62.     public int count() {
  63.         return size;
  64.     }
  65.    
  66.     /* Return an Iterator Object */
  67.     public Iterator<Item> iterator() {
  68.         return new LinkedListIterator();
  69.     }
  70.    
  71.     /* Define the Iterator class, and hasNext() and next() methods */
  72.     private class LinkedListIterator implements Iterator<Item> {        // 1
  73.         Node<Item> current = head;                                      // 2
  74.        
  75.         public boolean hasNext() {                                      // 3
  76.             return current != null;                                     // 3
  77.         }                                                               // 3
  78.        
  79.         public Item next() {                                            // 4
  80.             Item item = current.data;                                   // 4
  81.             current = current.next;                                     // 4
  82.             return item;                                                // 4
  83.         }
  84.     }
  85.    
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement