Advertisement
Aldin_SXR

LinkedList

Mar 15th, 2022 (edited)
789
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.29 KB | None | 0 0
  1. package week1;
  2.  
  3. import java.util.Iterator;
  4.  
  5. public class LinkedList<Data> implements Iterable<Data> {
  6.     private Node<Data> head;
  7.     private int size = 0;
  8.  
  9.     /* Add a new item to the beginning of the list */
  10.     public void addToFront(Data data) {
  11.         Node<Data> newNode = new Node<>();      // 1
  12.         newNode.data = data;                    // 1
  13.         newNode.next = head;                    // 2
  14.         head = newNode;                         // 3
  15.         size++;                                 // 4
  16.     }
  17.  
  18.     /* Remove an item from the beginning of the list */
  19.     public void removeFromFront() {
  20.         if (head == null) {                                                     // 1
  21.             throw new IndexOutOfBoundsException("The linked list is empty.");   // 1
  22.         }                                                                       // 1
  23.         head = head.next;                                                       // 2
  24.         size--;                                                                 // 3
  25.     }
  26.  
  27.     /* Add a new item to the end of the list */
  28.     public void addToRear(Data data) {
  29.         Node<Data> newNode = new Node<>();          // 1
  30.         newNode.data = data;                        // 1
  31.  
  32.         if (head == null) {                         // 2
  33.             head = newNode;                         // 2
  34.         } else {
  35.             Node<Data> current = head;              // 3
  36.             while (current.next != null) {          // 4
  37.                 current = current.next;             // 4
  38.             }                                       // 4
  39.             current.next = newNode;                 // 5
  40.         }
  41.         size++;                                     // 6
  42.     }
  43.  
  44.     /* Remove an item from the end of the list */
  45.     public void removeFromRear() {
  46.         if (head == null) {                                                     // 1
  47.             throw new IndexOutOfBoundsException("The linked list is empty.");   // 1
  48.         } else if (size == 1) {                                                 // 2
  49.             head = null;                                                        // 2
  50.         } else {                                                                // 3
  51.             Node<Data> current = head;                                          // 3
  52.             while (current.next.next != null) {                                 // 4
  53.                 current = current.next;                                         // 4
  54.             }                                                                   // 4
  55.             current.next = null;                                                // 5
  56.         }
  57.         size--;                                                                 // 6
  58.     }
  59.  
  60.     /* Get a linked list node by index (0-indexed) */
  61.     public Data get(int index) {
  62.         if (index < 0 || index >= size) {                                       // 1
  63.             throw new IndexOutOfBoundsException("Invalid linked list node.");   // 1
  64.         }
  65.  
  66.         Node<Data> current = head;                                              // 2
  67.         int i = 0;                                                              // 3
  68.         while (i < index) {                                                     // 4
  69.             current = current.next;                                             // 4
  70.             i++;                                                                // 4
  71.         }
  72.  
  73.         return current.data;                                                    // 5
  74.     }
  75.  
  76.     /* Add an element to a linked list by index (0-index) */
  77.     public void add(int index, Data data) {
  78.         // your code
  79.     }
  80.  
  81.     /* Delete an element from a linked list by index (0-index) */
  82.     public void remove(int index) {
  83.         // your code
  84.     }
  85.  
  86.     public void reverse() {
  87.         Node<Data> current = head;                                              // 1
  88.         Node<Data> previous = null;                                             // 1
  89.  
  90.         while (current != null) {                                               // 2
  91.             Node<Data> next = current.next;                                     // 3
  92.             current.next = previous;                                            // 4
  93.             previous = current;                                                 // 5
  94.             current = next;                                                     // 6
  95.         }
  96.         head = previous;                                                        // 7
  97.     }
  98.  
  99.     /* Return the size of the linked list */
  100.     public int count() {
  101.         return size;
  102.     }
  103.  
  104.     /* Define the Iterator class, and hasNext() and next() methods */
  105.     private class LinkedListIterator implements Iterator<Data> {        // 1
  106.         Node<Data> current = head;                                      // 2
  107.  
  108.         public boolean hasNext() {                                      // 3
  109.             return current != null;                                     // 3
  110.         }                                                               // 3
  111.  
  112.         public Data next() {                                            // 4
  113.             Data data = current.data;                                   // 4
  114.             current = current.next;                                     // 4
  115.             return data;                                                // 4
  116.         }
  117.     }
  118.  
  119.     /* Return an Iterator Object */
  120.     public Iterator<Data> iterator() {
  121.         return new LinkedListIterator();
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement