66 *
77 * Implement a simple LinkedList
88 * You will need to create a LinkedListNode class, which represents each item/node in the linked list
9+ * Assume that the elements in the linked list are all of type int
910 * Required functionality...
1011 * - head(): get head of list
1112 * - tail(): get tail of list
@@ -28,6 +29,7 @@ public LinkedList() {
2829
2930 /**
3031 * constructor with first value
32+ * @param value first element in the linked list
3133 */
3234 public LinkedList (int value ) {
3335 // create first node
@@ -36,15 +38,15 @@ public LinkedList(int value) {
3638
3739 /**
3840 * get head of Linked List
39- * @return
41+ * @return first element of the linked list
4042 */
4143 public LinkedListNode head () {
4244 return this .head ;
4345 }
4446
4547 /**
4648 * traverse and get tail of linked list
47- * @return
49+ * @return last element of the linked list
4850 */
4951 public LinkedListNode tail () {
5052 LinkedListNode current = head ;
@@ -59,6 +61,7 @@ public LinkedListNode tail() {
5961
6062 /**
6163 * add new element (node) to linked list
64+ * @param value element to add to the end of the linked list
6265 */
6366 public void add (int value ) {
6467 LinkedListNode tail = tail ();
@@ -70,17 +73,16 @@ public void add(int value) {
7073 }
7174
7275 /**
73- * push (add to head of linkedlist)
74- * @return
76+ * push (add element to head of linkedlist)
7577 */
7678 public void push (int value ) {
7779 LinkedListNode newHead = new LinkedListNode (value , head );
7880 head = newHead ;
7981 }
8082
8183 /**
82- * pop (remove head of linkedlist)
83- * @return
84+ * pop (remove and return head of linkedlist)
85+ * @return the node that was removed
8486 */
8587 public LinkedListNode pop () {
8688 LinkedListNode popped = head ;
@@ -89,8 +91,8 @@ public LinkedListNode pop() {
8991 }
9092
9193 /**
92- * to String
93- * @return
94+ * Returns a String version of the LinkedList
95+ * @return a String version of the LinkedList
9496 */
9597 @ Override
9698 public String toString () {
@@ -112,10 +114,18 @@ class Node {
112114
113115 private int value ;
114116
117+ /**
118+ * Constructs a list node with the given value
119+ * @param value the value stored in this Node
120+ */
115121 public Node (int value ) {
116122 this .value = value ;
117123 }
118124
125+ /**
126+ * Returns the value of this Node
127+ * @return the value of this Node
128+ */
119129 public int value () {
120130 return this .value ;
121131 }
@@ -125,15 +135,28 @@ class LinkedListNode extends Node {
125135
126136 private LinkedListNode next ;
127137
138+ /**
139+ * Constructs a LinkedListNode
140+ * @param value the value stored in this node
141+ * @param next the next node
142+ */
128143 public LinkedListNode (int value , LinkedListNode next ) {
129144 super (value );
130145 this .next = next ;
131146 }
132147
148+ /**
149+ * Returns the next node
150+ * @return the next node
151+ */
133152 public LinkedListNode next () {
134153 return this .next ;
135154 }
136155
156+ /**
157+ * Sets the next node
158+ * @param next the next node
159+ */
137160 public void setNext (LinkedListNode next ) {
138161 this .next = next ;
139162 return ;
0 commit comments