This repository was archived by the owner on Dec 29, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
242 lines (213 loc) · 6.35 KB
/
LinkedList.java
File metadata and controls
242 lines (213 loc) · 6.35 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
/**
* Doubly Linked List for Song objects.
* Supports insertion/removal at any position, circular toggling, and node-level access.
*/
public class LinkedList {
/** A node in the doubly linked list */
public static class Node {
private final Song data;
private Node next;
private Node prev;
Node(Song data) {
this.data = data;
}
public Song getData() { return data; }
public Node getNext() { return next; }
public Node getPrev() { return prev; }
}
private Node head;
private Node tail;
private int size;
// ======= Basic Accessors =======
public Node getHead() { return head; }
public Node getTail() { return tail; }
public int size() { return size; }
public boolean isEmpty() { return size == 0; }
// ======= Core Operations =======
/**
* Adds a song to the end of the list.
* @param song Song to add
* @return Node created for the song
*/
public Node add(Song song) {
Node newNode = new Node(song);
if (isEmpty()) {
head = tail = newNode;
} else {
tail.next = newNode;
newNode.prev = tail;
tail = newNode;
}
size++;
return newNode;
}
/**
* Inserts a song at a given index (inserts before that index).
* @param index Position to insert at
* @param song Song to insert
* @return Node created
*/
public Node addAt(int index, Song song) {
if (index <= 0 || isEmpty()) {
Node newNode = new Node(song);
newNode.next = head;
if (head != null) head.prev = newNode;
head = newNode;
if (tail == null) tail = newNode;
size++;
return newNode;
}
Node current = getNode(Math.min(index - 1, size - 1));
Node newNode = new Node(song);
newNode.next = current.next;
if (current.next != null) current.next.prev = newNode;
current.next = newNode;
newNode.prev = current;
if (newNode.next == null) tail = newNode;
size++;
return newNode;
}
/**
* Removes and returns the song at the specified index.
* @param index Position to remove from
* @return Removed Song, or null if list is empty
*/
public Song remove(int index) {
if (isEmpty()) return null;
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
Node toRemove = getNode(index);
if (toRemove == head) {
head = head.next;
if (head != null) head.prev = null;
if (toRemove == tail) tail = null;
} else {
Node before = toRemove.prev;
Node after = toRemove.next;
if (before != null) before.next = after;
if (after != null) after.prev = before;
if (toRemove == tail) tail = before;
}
size--;
return toRemove.data;
}
/**
* Retrieves the song at a given index.
* @param index Index to retrieve
* @return Song at the index
*/
public Song get(int index) {
if (isEmpty()) return null;
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
return getNode(index).data;
}
/**
* Returns the Node object at the specified index.
* @param index Index of node
*/
public Node getNodeAt(int index) {
return getNode(index);
}
/**
* Finds the index of the given song.
* @param song Song to find
* @return Index or -1 if not found
*/
public int indexOf(Song song) {
Node current = head;
int index = 0;
while (current != null) {
if (current.data.equals(song)) return index;
current = current.next;
index++;
}
return -1;
}
/**
* Checks if the list contains the specified song.
* @param song Song to check
*/
public boolean contains(Song song) {
return indexOf(song) != -1;
}
/**
* Finds the node containing the given song.
* @param song Song to search for
* @return Node or null if not found
*/
public Node findNode(Song song) {
Node current = head;
while (current != null) {
if (current.data.equals(song)) return current;
current = current.next;
}
return null;
}
public Node findNodeByData(Song song) {
return findNode(song);
}
// ======= Circular List Support =======
/**
* Converts the list to circular.
* Tail links to head and vice versa.
*/
public void makeCircular() {
if (!isEmpty() && head != tail) {
tail.next = head;
head.prev = tail;
}
}
/**
* Breaks circular linkage, making list linear.
*/
public void makeLinear() {
if (!isEmpty()) {
if (tail != null) tail.next = null;
if (head != null) head.prev = null;
}
}
/**
* Checks whether the list is circular.
* @return true if circular, false otherwise
*/
public boolean isCircular() {
return !isEmpty() &&
head != null && tail != null &&
tail.next == head && head.prev == tail;
}
/**
* Toggles circular/linear mode.
* @param circular true to make circular, false to make linear
*/
public void makeCircular(boolean circular) {
if (circular) makeCircular();
else makeLinear();
}
// ======= Utility =======
/**
* Clears the list completely.
*/
public void clear() {
head = tail = null;
size = 0;
}
/**
* Internal optimized node traversal.
* @param index Index of node
* @return Node at the index
*/
private Node getNode(int index) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
Node current;
if (index < size / 2) {
current = head;
for (int i = 0; i < index; i++) current = current.next;
} else {
current = tail;
for (int i = size - 1; i > index; i--) current = current.prev;
}
return current;
}
}