-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSinglyLinkedList.java
More file actions
285 lines (260 loc) · 8.48 KB
/
Copy pathSinglyLinkedList.java
File metadata and controls
285 lines (260 loc) · 8.48 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import java.lang.NullPointerException;
/**
* A basic singly-linked list whose nodes are composed of integers.
* Implements the algorithms required by IB Syllabus.
*
* @author (Sachith Ranaweera)
* @version (2016-10-26)
* @updated (2015-10-29)
*/
public class SinglyLinkedList extends Object
{
/* instance fields */
private Node head;
/* constructors */
/**
* Constructs an empty singly-linked list.
*/
public SinglyLinkedList()
{
head = null;
} // end of constructor SinglyLinkedList()
/* methods */
/**
* Searches for the specified integer, and returns the first
* Node containing same, if it exists; otherwise null.
*
* @param integer the integer value sought
* @return the Node containing the integer value sought, if
* it exists; otherwise null
*/
public Node contains(int integer)
{
Node temp = head;
// is the node not null?
while (temp != null)
{
// if (temp.getData() == integer) return temp
if (temp.getData() == integer) return temp;
// goes to the next node.
temp = temp.getNext();
} // end of while (temp != null)
return null;
} // end of method contains(int integer)
/**
* Deletes the specified node.<dr>(pre-condition: target is not null)
*
* @param target the Node to be deleted
*/
public void delete(Node target)
throws NullPointerException
{
// if (target == null) throw new NullPointerException();
if (target == null) throw new NullPointerException();
Node before = head;
Node after;
// if (before == target) head = before.getNext();
if (before == target) head = before.getNext();
// is the next node null?
do
{
// is the next node equal to the target?
if (before.getNext() == target)
{
after = before.getNext().getNext();
before.setNext(after);
}
else
{
before = before.getNext();
} // end of if (before.getNext() == target)
} while (before != null);
} // end of method delete(Node target)
/**
* Returns a reference to the Node which is the head of this
* list.
*
* @return a reference to a Node, or null if this list is
* empty
*/
public Node getHead()
{
return head;
} // end of method getHead()
/**
* Returns a reference to the Node which is the tail of this
* list.
*
* @return a reference to a Node, or null if this list is empty
*/
public Node getTail()
{
// if (head == null) return null;
if (head == null) return head;
Node temp = head;
// is the pointer null?
while (temp.getNext() != null) temp = temp.getNext();
return temp;
} // end of method getTail()
/**
* Returns a reference to the Node which is the tail of this list.
*
* @returns a reference to a Node, or null if this list is empty
*/
public Node getTailWithoutUsingTailReference()
{
// if (head == null) return null;
if (head == null) return head;
Node temp = head;
// is the pointer null?
while (temp.getNext() != null) temp = temp.getNext();
return temp;
} // end of method getTailWithoutUsingTailReference()
/**
* Inserts the specified value into a new node following the
* insertion point.
* <br>(pre-condition: the insertion point is not null)
*
* @param integer the int value to be inserted
* @insertionPoint the Node after which the new node will be
* inserted
*/
public void insert(int integer, Node insertionPoint)
throws NullPointerException
{
// is the insertionPoint not null?
if (insertionPoint != null)
{
Node temp = head;
Node newNode = new Node(integer, null);
do
{
// is the node equal to the insertionpoint
if (temp.equals(insertionPoint))
temp.setNext(newNode);
temp = temp.getNext();
} while (temp != null);
}
else
{
throw new NullPointerException();
} // end of if (insertionPoint != null)
} // end of method insert()
/**
* Inserts the specified node into this list following the
* insertionpoint.
* (pre-conditions: the entrant and the insertion point is not null)
*
* @param entrant the Node to be inserted
* @param insertionPoint the Node after the new node will be
* inserted
*/
public void insert(Node entrant, Node insertionPoint)
throws NullPointerException
{
// is the insertionpoint and entrant null?
if (insertionPoint != null || entrant != null)
{
Node temp = head;
do
{
// the node equal to the insertionPoint?
if (temp.equals(insertionPoint))
temp.setNext(entrant);
temp = temp.getNext();
} while (temp != null);
}
else
{
throw new NullPointerException();
} // end of if (insertionPoint != null)
} // end of method insert(Node entrant, Node insertionPoint)
/**
* Inserts the specified value into a node at the head of this
* list.
*
* @param integer the int value to be inserted
*/
public void insertAtHead(int integer)
{
Node newNode = new Node(integer, null);
newNode.setNext(head);
head = newNode;
} // end of method insertAtHead(int integer)
/**
* Inserts the specified node at the head of this list.
*
* @param entrant the Node to be inserted
*/
public void insertAtHead(Node entrant)
{
entrant.setNext(head);
head = entrant;
} // end of method insertAtHead(Node entrant)
/**
* Inserts the specified value into a node at the tail of this
* list.
*
* @param integer value to be inserted
*/
public void insertAtTail(int integer)
{
Node newNode = new Node(integer, null);
getTailWithoutUsingTailReference().setNext(newNode);
}// end of method insertAtTail(int integer)
/**
* Uses an insertion-sort algorithm with in-place sorting:
* <dr> • establish the upper bound of the sorted portion of this list
* <dr> • the sorted portion begins and ends with the head, which is—by itself—sorted
* <dr> • start at lower bound of the unsorted portion
* <dr> • move each unsorted node into the correct location in the sorted portion
*
* @return the number of times an inserion was made
*/
public int sort()
{
return 0;
} // end of method sort()
/**
* Reveals whether this list is empty.
*
* @return true if this list is composed of 0 nodes; otherwise
* false;
*/
public boolean isEmpty()
{
// if (head == null) return true
if (head == null) return true;
return false;
} // end of method isEmpty()
/**
* Reutrns a terse string reprecentation of this list.
*
* @return a terse string reprecentation of this list
*/
public String terseString()
{
// if (head == null) return null;
if (head == null) return null;
String nodeData = "";
Node temp = head;
while (temp != null)
{
nodeData += temp.getData() + " ";
temp = temp.getNext();
}// end of while (temp.getNext() != null)
return nodeData;
} // end of method terseString()
/**
* Returns a terse string representation of this list.
*
* @return a string reprecentaion of this list
*/
public String toString()
{
return
getClass().getName()
+ "[head: " + head
+ "]";
} // end of method toString()
} // end of class class SinglyLinkedList extends Object