6
6
*
7
7
* Implement a simple LinkedList
8
8
* 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
9
10
* Required functionality...
10
11
* - head(): get head of list
11
12
* - tail(): get tail of list
@@ -28,6 +29,7 @@ public LinkedList() {
28
29
29
30
/**
30
31
* constructor with first value
32
+ * @param value first element in the linked list
31
33
*/
32
34
public LinkedList (int value ) {
33
35
// create first node
@@ -36,15 +38,15 @@ public LinkedList(int value) {
36
38
37
39
/**
38
40
* get head of Linked List
39
- * @return
41
+ * @return first element of the linked list
40
42
*/
41
43
public LinkedListNode head () {
42
44
return this .head ;
43
45
}
44
46
45
47
/**
46
48
* traverse and get tail of linked list
47
- * @return
49
+ * @return last element of the linked list
48
50
*/
49
51
public LinkedListNode tail () {
50
52
LinkedListNode current = head ;
@@ -59,6 +61,7 @@ public LinkedListNode tail() {
59
61
60
62
/**
61
63
* add new element (node) to linked list
64
+ * @param value element to add to the end of the linked list
62
65
*/
63
66
public void add (int value ) {
64
67
LinkedListNode tail = tail ();
@@ -70,17 +73,16 @@ public void add(int value) {
70
73
}
71
74
72
75
/**
73
- * push (add to head of linkedlist)
74
- * @return
76
+ * push (add element to head of linkedlist)
75
77
*/
76
78
public void push (int value ) {
77
79
LinkedListNode newHead = new LinkedListNode (value , head );
78
80
head = newHead ;
79
81
}
80
82
81
83
/**
82
- * pop (remove head of linkedlist)
83
- * @return
84
+ * pop (remove and return head of linkedlist)
85
+ * @return the node that was removed
84
86
*/
85
87
public LinkedListNode pop () {
86
88
LinkedListNode popped = head ;
@@ -89,8 +91,8 @@ public LinkedListNode pop() {
89
91
}
90
92
91
93
/**
92
- * to String
93
- * @return
94
+ * Returns a String version of the LinkedList
95
+ * @return a String version of the LinkedList
94
96
*/
95
97
@ Override
96
98
public String toString () {
@@ -112,10 +114,18 @@ class Node {
112
114
113
115
private int value ;
114
116
117
+ /**
118
+ * Constructs a list node with the given value
119
+ * @param value the value stored in this Node
120
+ */
115
121
public Node (int value ) {
116
122
this .value = value ;
117
123
}
118
124
125
+ /**
126
+ * Returns the value of this Node
127
+ * @return the value of this Node
128
+ */
119
129
public int value () {
120
130
return this .value ;
121
131
}
@@ -125,15 +135,28 @@ class LinkedListNode extends Node {
125
135
126
136
private LinkedListNode next ;
127
137
138
+ /**
139
+ * Constructs a LinkedListNode
140
+ * @param value the value stored in this node
141
+ * @param next the next node
142
+ */
128
143
public LinkedListNode (int value , LinkedListNode next ) {
129
144
super (value );
130
145
this .next = next ;
131
146
}
132
147
148
+ /**
149
+ * Returns the next node
150
+ * @return the next node
151
+ */
133
152
public LinkedListNode next () {
134
153
return this .next ;
135
154
}
136
155
156
+ /**
157
+ * Sets the next node
158
+ * @param next the next node
159
+ */
137
160
public void setNext (LinkedListNode next ) {
138
161
this .next = next ;
139
162
return ;
0 commit comments