9
9
*
10
10
* Implement a simple LinkedList
11
11
* You will need to create a LinkedListNode class, which represents each item/node in the linked list
12
+ * Assume that the elements in the linked list are all of type int
12
13
* Required functionality...
13
14
* - head(): get head of list
14
15
* - tail(): get tail of list
@@ -31,6 +32,7 @@ public LinkedList() {
31
32
32
33
/**
33
34
* constructor with first value
35
+ * @param value first element in the linked list
34
36
*/
35
37
public LinkedList (int value ) {
36
38
// create first node
@@ -39,15 +41,15 @@ public LinkedList(int value) {
39
41
40
42
/**
41
43
* get head of Linked List
42
- * @return
44
+ * @return first element of the linked list
43
45
*/
44
46
public LinkedListNode head () {
45
47
return this .head ;
46
48
}
47
49
48
50
/**
49
51
* traverse and get tail of linked list
50
- * @return
52
+ * @return last element of the linked list
51
53
*/
52
54
public LinkedListNode tail () {
53
55
LinkedListNode current = head ;
@@ -62,6 +64,7 @@ public LinkedListNode tail() {
62
64
63
65
/**
64
66
* add new element (node) to linked list
67
+ * @param value element to add to the end of the linked list
65
68
*/
66
69
public void add (int value ) {
67
70
LinkedListNode tail = tail ();
@@ -73,17 +76,16 @@ public void add(int value) {
73
76
}
74
77
75
78
/**
76
- * push (add to head of linkedlist)
77
- * @return
79
+ * push (add element to head of linkedlist)
78
80
*/
79
81
public void push (int value ) {
80
82
LinkedListNode newHead = new LinkedListNode (value , head );
81
83
head = newHead ;
82
84
}
83
85
84
86
/**
85
- * pop (remove head of linkedlist)
86
- * @return
87
+ * pop (remove and return head of linkedlist)
88
+ * @return the node that was removed
87
89
*/
88
90
public LinkedListNode pop () {
89
91
LinkedListNode popped = head ;
@@ -92,8 +94,8 @@ public LinkedListNode pop() {
92
94
}
93
95
94
96
/**
95
- * to String
96
- * @return
97
+ * Returns a String version of the LinkedList
98
+ * @return a String version of the LinkedList
97
99
*/
98
100
@ Override
99
101
public String toString () {
@@ -116,11 +118,19 @@ class Node {
116
118
private UUID ID ;
117
119
private int value ;
118
120
121
+ /**
122
+ * Constructs a list node with the given value
123
+ * @param value the value stored in this Node
124
+ */
119
125
public Node (int value ) {
120
126
this .ID = UUID .randomUUID ();
121
127
this .value = value ;
122
128
}
123
129
130
+ /**
131
+ * Returns the value of this Node
132
+ * @return the value of this Node
133
+ */
124
134
public int value () {
125
135
return this .value ;
126
136
}
@@ -130,15 +140,28 @@ class LinkedListNode extends Node {
130
140
131
141
private LinkedListNode next ;
132
142
143
+ /**
144
+ * Constructs a LinkedListNode
145
+ * @param value the value stored in this node
146
+ * @param next the next node
147
+ */
133
148
public LinkedListNode (int value , LinkedListNode next ) {
134
149
super (value );
135
150
this .next = next ;
136
151
}
137
152
153
+ /**
154
+ * Returns the next node
155
+ * @return the next node
156
+ */
138
157
public LinkedListNode next () {
139
158
return this .next ;
140
159
}
141
160
161
+ /**
162
+ * Sets the next node
163
+ * @param next the next node
164
+ */
142
165
public void setNext (LinkedListNode next ) {
143
166
this .next = next ;
144
167
return ;
0 commit comments