Advertisement
Aldin-SXR

addToRear()

Mar 5th, 2020
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.43 KB | None | 0 0
  1. /* Add a new item to the end of the list */
  2. public void addToRear(Item item) {
  3.     Node<Item> newNode = new Node<Item>();      // 1
  4.     newNode.data = item;                        // 1
  5.        
  6.     if (head == null) {                         // 2
  7.         head = newNode;                         // 2
  8.     } else {           
  9.         Node<Item> current = head;              // 3
  10.         while (current.next != null) {          // 4
  11.             current = current.next;             // 4
  12.         }                                       // 4
  13.         current.next = newNode;                 // 5
  14.     }
  15.     size++;                                     // 6
  16. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement