-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLinkedList.java
238 lines (212 loc) · 5.29 KB
/
LinkedList.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
public class LinkedList<T> {
/**
* This class linked the nodes together to create a list
*
* Ching2 Huang
*/
private LinkedListNode<T> head;
/**
* Constructor creates a empty list
*/
public LinkedList() {
this.head = null;
}
/**
* Constructor creates a list and pass in head node
*/
public LinkedList(LinkedListNode<T> head) {
this.head = head;
}
/**
* Get data stored in head node of list.
*/
public T getFirst() {
if (isEmpty()) {
return null;
} else {
return head.getData();
}
}
/**
* Get the head node of the list.
*/
public LinkedListNode<T> getFirstNode() {
return head;
}
/**
* Get data stored in last node of list.
*/
public T getLast() {
// if the list doens't exist, return null
if (isEmpty()) {
return null;
} else { // if the list exits
// node is the head
LinkedListNode<T> node = head;
// while the nextNode is not the last value
while (node.getNext() != null) {
// keep looking for the next node until nextNode is last
// node
node = node.getNext();
}
// if nextNode's next node is null, nextNode is the last node
// so return the value of last node
return node.getData();
}
}
/**
* Get the tail node of the list.
*/
public LinkedListNode<T> getLastNode() {
// if the list doens't exist, return null
if (isEmpty()) {
return null;
} else {// if the list exits
// node is the head
LinkedListNode<T> node = head;
// while the node is not the last value
while (node.getNext() != null) {
// keep looking for the next node until nextNode is last
// node
node = node.getNext();
}
// if nextNode's next node is null, nextNode is the last node
// so return the last node
return node;
}
}
/**
* Insert a new node with data at the head of the list.
*/
public void insertFirst(T data) {
// create a new node to insert
LinkedListNode<T> newNode = new LinkedListNode<T>(data);
// set value for the new node
newNode.setData(data);
// if the list exists
if (isEmpty() == false) {
// set the newNode to connect with the head
newNode.setNext(head);
// newNode replace the head
head = newNode;
} else {
// if the link doens't exist, set the newNode as the head of the
// list
head = newNode;
}
}
/**
* Insert a new node with data after currentNode
*/
public void insertAfter(LinkedListNode<T> currentNode, T data) {
// if the list has at lease one node
if (isEmpty() == false) {
// create a new node to insert
LinkedListNode<T> newNode = new LinkedListNode<T>(data);
// get the original next node of the current node
LinkedListNode<T> nextNode = currentNode.getNext();
// set newNode as the next node of currentNode
currentNode.setNext(newNode);
// set the original next node as the next node of newNode
newNode.setNext(nextNode);
}
}
/**
* Insert a new node with data at the end of the list.
*/
public void insertLast(T data) {
// if the list exits
if (isEmpty() == false) {
// create a new node to insert
LinkedListNode<T> newNode = new LinkedListNode<T>(data);
// get the last node of the list
LinkedListNode<T> lastNode = getLastNode();
// set newNode as the last node of the list
lastNode.setNext(newNode);
}
}
/**
* Remove the first node
*/
public void deleteFirst() {
// if the list exists
if (isEmpty() == false) {
// set head to the next node of the original head
head = head.getNext();
}
}
/**
* Remove the last node
*/
public void deleteLast() {
// if the list exists
if (isEmpty() == false) {
// node is the head
LinkedListNode<T> node = head;
// lastNode is the last node of the list
LinkedListNode<T> lastNode = getLastNode();
// while the next node of the node is not the lastNode
while (node.getNext() != lastNode) {
// keep looking for the next node
node = node.getNext();
}
// if the next node is the lastNode, set the node as the last
// node of the list
node.setNext(null);
}
}
/**
* Remove node following currentNode If no node exists (i.e., currentNode is
* the tail), do nothing
*/
public void deleteNext(LinkedListNode<T> currentNode) {
// check if currentNode and the node next to currentNode exist
if (currentNode != null && currentNode.getNext() != null) {
// delete the middle node
currentNode.setNext(currentNode.getNext().getNext());
}
}
/**
* Return the number of nodes in this list.
*/
public int size() {
// the size of the list
int size = 0;
// node is the head
LinkedListNode<T> node = head;
while (node != null) {
// increase one more node
size++;
// update the node
node = node.getNext();
}
// return the total size of the list
return size;
}
/**
* Check if list is empty.
*
* @return true if list contains no items.
*/
public boolean isEmpty() {
// if the head doesn't exist, this is an empty list
return head == null;
}
/**
* Return a String representation of the list.
*/
public String toString() {
if (isEmpty() == false) {
String arrow = " -> ";
LinkedListNode<T> nextNode = head;
String listString = head.toString();
while (nextNode.getNext() != null) {
nextNode = nextNode.getNext();
listString = listString + arrow + nextNode.toString();
}
return listString;
} else {
return "empty";
}
}
}