-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHW02.java
More file actions
306 lines (278 loc) · 8.75 KB
/
Copy pathHW02.java
File metadata and controls
306 lines (278 loc) · 8.75 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import java.util.NoSuchElementException;
public class HW02 {
public static void main(String[] args) {
MusicPlayer mp = new MusicPlayer("./Musics");
}
}
interface INode <T> { // storage unit
// Constructor (T data, Node<T> prev, Node<T> next)
T getData(); // returns the data
Node<T> getNext(); // returns the next of this storage unit
Node<T> getPrev(); // returns the previous storage unit of this unit
void setNext(Node<T> next); // sets next pointer of this node
void setPrev(Node<T> prev); // sets the prev pointer of this node
String toString(); // string representation
}
interface IDoublyCircularLinkedList <T> {
// must have the data field current
// Constructor ()
void addFirst(T data); // adds an element to the head of the list. If first element in list, must also be last element
// if only element in the list its next and prev should point to itself
void addLast(T data); // adds an element to the tail of the list. If first element in list, must also be last element
// if only element in the list its next and prev should point to itself
T removeFirst() throws NoSuchElementException; // removes the first element in the list,
// throw exception if list is empty, if only element remaining it should be first and last and its next and prev,
// should be itself
T removeLast() throws NoSuchElementException; // removes the last element in the list,
// throw exception if list is empty, if only element remaining it should be first and last and its next and prev,
// should be itself
T get(int index) throws IndexOutOfBoundsException; // gets the ith element in the list,
// should throw exception if out of bounds
T first() throws NoSuchElementException; // should set current, returns the first data
T last() throws NoSuchElementException; // should set current, returns the last data
boolean remove(T data); // should return false if data doesnt exists, returns true and removes if exists
boolean isEmpty();
int size();
T next() throws NoSuchElementException; // if empty, throws exception, should change current correctly
// if current is null should return head and set it to head
T previous() throws NoSuchElementException; // if empty, throws exception, should change current correctly
// if current is null should return tail data and set it
T getCurrent() throws NoSuchElementException; // Retruns the current pointer, if no element exits throws exception
// if current is null returns heads data
Node<T> getHead(); // returns the head of the list, if is empty returns null
// any other method needed
}
class Node<T> implements INode<T> {
private T data;
private Node<T> next;
private Node<T> prev;
public Node(T data, Node<T> prev, Node<T> next) {
this.data = data;
this.prev = prev;
this.next = next;
}
@Override
public T getData() {
return data;
}
@Override
public Node<T> getNext() {
return next;
}
@Override
public Node<T> getPrev() {
return prev;
}
@Override
public void setNext(Node<T> next) {
this.next = next;
}
@Override
public void setPrev(Node<T> prev) {
this.prev = prev;
}
@Override
public String toString() {
return "Data şudur: " + data;
}
}
class DoublyCircularLinkedList<T> implements IDoublyCircularLinkedList<T> {
private int size;
private Node<T> head;
private Node<T> tail;
private Node<T> current;
public DoublyCircularLinkedList(){
size = 0;
head = null;
tail = null;
current = null;
}
@Override
public void addFirst(T data) {
Node<T> element = new Node<>(data,null,null);
if(size == 0){
head = element;
tail = element;
head.setNext(head);
head.setPrev(head);
}
else{
tail.setNext(element);
element.setPrev(tail);
element.setNext(head);
head.setPrev(element);
head = element;
}
size++;
}
@Override
public void addLast(T data) {
Node<T> element = new Node<>(data, null, null);
if(size == 0){
head = element;
tail = element;
head.setPrev(head);
head.setNext(head);
}
else{
tail.setNext(element);
element.setPrev(tail);
element.setNext(head);
head.setPrev(element);
tail = element;
}
size++;
}
@Override
public T removeFirst() throws NoSuchElementException {
if(isEmpty()){
throw new NoSuchElementException("Liste boş, başta eleman yok.");
}
else if(size() == 1){
Node<T> temp = head;
head = null;
tail = null;
size--;
return temp.getData();
}
else{
Node<T> temp = head;
T tempData = temp.getData();
head = head.getNext();
head.setPrev(tail);
tail.setNext(head);
size--;
return tempData;
}
}
@Override
public T removeLast() throws NoSuchElementException {
if(isEmpty()){
throw new NoSuchElementException("Liste boş, sonda eleman yok.");
}
else if(size() == 1){
Node<T> temp = head;
head = null;
tail = null;
size--;
return temp.getData();
}
else{
Node<T> temp = tail;
T tempData = tail.getData();
tail = tail.getPrev();
tail.setNext(head);
head.setPrev(tail);
size--;
return tempData;
}
}
@Override
public T get(int index) throws IndexOutOfBoundsException {
if(index >= size || index < 0){
throw new IndexOutOfBoundsException("Geçersiz index girdiniz.");
}
else{
Node<T> temp = head;
for(int i = 0; i < index; i++){
temp = temp.getNext();
}
return temp.getData();
}
}
@Override
public T first() throws NoSuchElementException {
if(isEmpty()){
throw new NoSuchElementException("Liste boş, ilk eleman yok.");
}
current = head;
return head.getData();
}
@Override
public T last() throws NoSuchElementException {
if (isEmpty()) {
throw new NoSuchElementException("Liste boş, ilk eleman yok.");
}
current = tail;
return tail.getData();
}
@Override
public boolean remove(T data) {
Node<T> temp = head;
for (int i = 0; i < size; i++) {
if (temp.getData().equals(data)) {
if (temp == head) {
head = head.getNext();
tail.setNext(head);
head.setPrev(tail);
} else if (temp == tail) {
tail = tail.getPrev();
tail.setNext(head);
head.setPrev(tail);
} else {
temp.getPrev().setNext(temp.getNext());
temp.getNext().setPrev(temp.getPrev());
}
size--;
return true;
}
temp = temp.getNext();
}
return false;
}
@Override
public boolean isEmpty() {
if(size == 0){
return true;
}
return false;
}
@Override
public int size() {
return size;
}
@Override
public T next() throws NoSuchElementException {
if (head == null) {
throw new NoSuchElementException("Liste boş.");
}
if (current == null) {
current = head;
return head.getData();
}
current = current.getNext();
return current.getData();
}
@Override
public T previous() throws NoSuchElementException {
if (head == null) {
throw new NoSuchElementException("Liste boş.");
}
if (current == null) {
current = tail;
return tail.getData();
}
current = current.getPrev();
return current.getData();
}
@Override
public T getCurrent() throws NoSuchElementException {
if (head == null) {
throw new NoSuchElementException("Liste boş.");
}
if (current == null) {
current = head;
return head.getData();
}
return current.getData();
}
@Override
public Node<T> getHead() {
if(size == 0){
return null;
}
else{
return head;
}
}
}