-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSLL.java
More file actions
261 lines (247 loc) · 9.12 KB
/
SLL.java
File metadata and controls
261 lines (247 loc) · 9.12 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
//************************** SLL.java *********************************
// a generic singly linked list class
public class SLL<T> {
class SLLNode<T> {
T info;
SLLNode<T> next;
public SLLNode() {
this(null,null);
}
public SLLNode(T el) {
this(el,null);
}
public SLLNode(T el, SLLNode<T> ptr) {
info = el; next = ptr;
}
}
protected SLLNode<T> head, tail;
public SLL() {
head = tail = null;
}
public boolean isEmpty() {
return head == null;
}
public void addToHead(T el) {
head = new SLLNode<T>(el,head);
if (tail == null)
tail = head;
}
public void addToTail(T el) {
if (!isEmpty()) {
tail.next = new SLLNode<T>(el);
tail = tail.next;
}
else head = tail = new SLLNode<T>(el);
}
public T deleteFromHead() { // delete the head and return its info;
if (isEmpty())
return null;
T el = head.info;
if (head == tail) // if only one node on the list;
head = tail = null;
else head = head.next;
return el;
}
public T deleteFromTail() { // delete the tail and return its info;
if (isEmpty())
return null;
T el = tail.info;
if (head == tail) // if only one node in the list;
head = tail = null;
else { // if more than one node in the list,
SLLNode<T> tmp; // find the predecessor of tail;
for (tmp = head; tmp.next != tail; tmp = tmp.next);
tail = tmp; // the predecessor of tail becomes tail;
tail.next = null;
}
return el;
}
public void delete(T el) { // delete the node with an element el;
if (!isEmpty())
if (head == tail && el.equals(head.info)) // if only one
head = tail = null; // node on the list;
else if (el.equals(head.info)) // if more than one node on the list;
head = head.next; // and el is in the head node;
else { // if more than one node in the list
SLLNode<T> pred, tmp;// and el is in a nonhead node;
for (pred = head, tmp = head.next;
tmp != null && !tmp.info.equals(el);
pred = pred.next, tmp = tmp.next);
if (tmp != null) { // if el was found;
pred.next = tmp.next;
if (tmp == tail) // if el is in the last node;
tail = pred;
}
}
}
@Override
public String toString() {
if(head == null)
return "[ ]";
String str = "[ ";
SLLNode<T> tmp = head;
while(tmp != null){
str += tmp.info + " ";
tmp = tmp.next;
}
return str+"]";
}
public boolean contains(T el) {
if(head == null)
return false;
SLLNode<T> tmp = head;
while(tmp != null){
if(tmp.info.equals(el))
return true;
tmp = tmp.next;
}
return false;
}
public int size(){
if(head == null)
return 0;
int count = 0;
SLLNode<T> p = head;
while(p != null) {
count++;
p = p.next;
}
return count;
}
// Please write the methods of Task02 here:
public void insertBefore(int index, T newElem) {
try {
// Check if the list is empty
if(isEmpty())
throw new IllegalArgumentException("The list is empty.");
// Check if the index is valid
if(index < 0 || index >= size())
throw new IndexOutOfBoundsException("The index is not valid.");
// Create a new node with the new element
SLLNode<T> newNode = new SLLNode<T>(newElem);
SLLNode<T> currentNode = head;
// Handle the case where the index is 0
if(index == 0) {
newNode.next = head;
head = newNode;
}
else {
// Traverse the list to find the node at index - 1
for(int i = 0; i < index - 1; i++) {
currentNode = currentNode.next;
}
// Insert the new node before the node at index
newNode.next = currentNode.next;
currentNode.next = newNode;
}
}
catch (Exception e) {
// Print the exception message
System.out.println(e.getMessage());
}
}
public T delete(int index) {
T deletedValue = null;
try {
// Check if the list is empty
if (isEmpty())
throw new IllegalArgumentException("The list is empty.");
// Check if the index is valid
if (index < 0 || index >= size())
throw new IndexOutOfBoundsException("The index is not valid.");
// Handle the case where the index is 0
if (index == 0) {
deletedValue = head.info;
if (head == tail) { // if only one node on the list
head = tail = null;
}
else {
head = head.next;
}
}
else {
SLLNode<T> currentNode = head;
// Traverse the list to find the node at index - 1
for (int i = 0; i < index - 1; i++) {
currentNode = currentNode.next;
}
// Check if the node to delete is the tail
if (currentNode.next == tail) {
deletedValue = tail.info;
currentNode.next = null;
tail = currentNode;
}
else {
deletedValue = currentNode.next.info;
currentNode.next = currentNode.next.next;
}
}
}
catch (Exception e) {
// Print the exception message
System.out.println(e.getMessage());
}
return deletedValue;
}
public void insertAfterSecondOccurrence(T targetElement, T newElement) {
try {
// Check if the list is empty
if (head == null) {
throw new IllegalArgumentException("The list is empty.");
}
SLLNode<T> current = head;
int count = 0;
SLLNode<T> secondOccurrence = null;
// Traverse the list to find the second occurrence of the target element
while (current != null) {
if (current.info.equals(targetElement)) {
count++;
if (count == 2) {
secondOccurrence = current;
break;
}
}
current = current.next;
}
// Check if the second occurrence was found
if (secondOccurrence == null) {
throw new IllegalArgumentException("No second occurrence of " + targetElement + " found.");
}
// Create a new node with the new element and insert it after the second occurrence
SLLNode<T> newNode = new SLLNode<T>(newElement);
newNode.next = secondOccurrence.next;
secondOccurrence.next = newNode;
}
catch (IllegalArgumentException e) {
// Print the exception message
System.out.println(e.getMessage());
}
}
public T[] toArray(T[] list) {
// Check if the provided array is smaller than the size of the linked list
if (list.length < size()) {
// Create a new array of the required size using type casting
list = (T[]) new Object[size()];
}
// Initialize a currentNode variable with the head of the linked list
SLLNode<T> currentNode = head;
// Initialize an index variable to keep track of the array index
int index = 0;
// Iterate over the linked list
while (currentNode != null) {
// Assign the current node's info to the corresponding index in the array
list[index] = currentNode.info;
// Move to the next node in the linked list
currentNode = currentNode.next;
// Increment the index
index++;
}
// If there are remaining indices in the array after iterating over the linked list,
// set them to null to indicate the end of the valid elements
if (index < list.length) {
list[index] = null;
}
// Return the modified array
return list;
}
}