|
| 1 | +package com.codefortomorrow.advanced.chapter16.solutions; |
| 2 | + |
| 3 | +// using Lombok for Getters and Setters |
| 4 | +import lombok.Setter; |
| 5 | +import lombok.AllArgsConstructor; |
| 6 | +import lombok.Getter; |
| 7 | +import lombok.Setter; |
| 8 | +// UUID to represent each node's unique ID |
| 9 | +import java.util.UUID; |
| 10 | + |
| 11 | +/** |
| 12 | + * @author ArmeetJatyani |
| 13 | + * March 2021 |
| 14 | + * |
| 15 | + * Implement a simple LinkedList |
| 16 | + * You will need to create a LinkedListNode class, which represents each item/node in the linked list |
| 17 | + * Required functionality... |
| 18 | + * - head(): get head of list |
| 19 | + * - tail(): get tail of list |
| 20 | + * - add(): add to tail of list |
| 21 | + * - push(): push to head of list |
| 22 | + * - pop(): remove head of list |
| 23 | + * - toString(): return a String representation of the list |
| 24 | + */ |
| 25 | + |
| 26 | +@Setter |
| 27 | +public class LinkedList { |
| 28 | + private LinkedListNode head; |
| 29 | + |
| 30 | + /** |
| 31 | + * default constructor |
| 32 | + */ |
| 33 | + public LinkedList() { |
| 34 | + head = null; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * constructor with first value |
| 39 | + * @param value |
| 40 | + */ |
| 41 | + public LinkedList(int value) { |
| 42 | + // create first node |
| 43 | + head = new LinkedListNode(value, null); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * get head of Linked List |
| 48 | + * @return |
| 49 | + */ |
| 50 | + public LinkedListNode head() { |
| 51 | + return this.head; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * traverse and get tail of linked list |
| 56 | + * @return |
| 57 | + */ |
| 58 | + public LinkedListNode tail() { |
| 59 | + LinkedListNode current = head; |
| 60 | + if(current == null) return null; |
| 61 | + |
| 62 | + while (current.getNext() != null) { |
| 63 | + current = current.getNext(); |
| 64 | + } |
| 65 | + |
| 66 | + return current; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * add new element (node) to linked list |
| 71 | + * @param value |
| 72 | + */ |
| 73 | + public void add(int value) { |
| 74 | + LinkedListNode tail = tail(); |
| 75 | + if (tail == null) { |
| 76 | + head = new LinkedListNode(value, null); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + tail.setNext(new LinkedListNode(value, null)); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * push (add to head of linkedlist) |
| 85 | + * @return |
| 86 | + */ |
| 87 | + public void push(int value) { |
| 88 | + LinkedListNode newHead = new LinkedListNode(value, head); |
| 89 | + head = newHead; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * pop (remove head of linkedlist) |
| 94 | + * @return |
| 95 | + */ |
| 96 | + public LinkedListNode pop() { |
| 97 | + LinkedListNode popped = head; |
| 98 | + head = head.getNext(); |
| 99 | + return popped; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * to String |
| 104 | + * @return |
| 105 | + */ |
| 106 | + @Override |
| 107 | + public String toString() { |
| 108 | + String list = "["; |
| 109 | + LinkedListNode current = head; |
| 110 | + if(current == null) return null; |
| 111 | + do { |
| 112 | + list += Integer.toString(current.getValue()) + ", "; |
| 113 | + current = current.getNext(); |
| 114 | + } |
| 115 | + while (current != null); |
| 116 | + |
| 117 | + list = list.substring(0, list.length() - 2); |
| 118 | + return list + "]"; |
| 119 | + } |
| 120 | + |
| 121 | +} |
| 122 | + |
| 123 | +@Getter |
| 124 | +@Setter |
| 125 | +class Node |
| 126 | +{ |
| 127 | + private UUID ID; |
| 128 | + private int value; |
| 129 | + |
| 130 | + public Node(int value) |
| 131 | + { |
| 132 | + this.ID = UUID.randomUUID(); |
| 133 | + this.value = value; |
| 134 | + } |
| 135 | + |
| 136 | + public int value() |
| 137 | + { |
| 138 | + return this.value; |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +@Getter |
| 143 | +@Setter |
| 144 | +class LinkedListNode extends Node |
| 145 | +{ |
| 146 | + private LinkedListNode next; |
| 147 | + |
| 148 | + public LinkedListNode(int value, LinkedListNode next) |
| 149 | + { |
| 150 | + super(value); |
| 151 | + this.next = next; |
| 152 | + } |
| 153 | + |
| 154 | + public LinkedListNode next() |
| 155 | + { |
| 156 | + return this.next; |
| 157 | + } |
| 158 | +} |
| 159 | + |
0 commit comments