Repository navigation
Expand file tree
/
Copy pathMp3Player.java
More file actions
238 lines (208 loc) · 7.17 KB
/
Copy pathMp3Player.java
File metadata and controls
238 lines (208 loc) · 7.17 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
import java.util.NoSuchElementException;
public class Mp3Player {
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> prev;
private Node<T> next;
public Node(T data, Node<T> prev, Node<T> next) {
this.data = data;
this.prev = prev;
this.next = next;
}
public T getData() {
return data;
}
public Node<T> getNext() {
return next;
}
public Node<T> getPrev() {
return prev;
}
public void setNext(Node<T> next) {
this.next = next;
}
public void setPrev(Node<T> prev) {
this.prev = prev;
}
public String toString() {
return data.toString();
}
}
class DoublyCircularLinkedList<T> implements IDoublyCircularLinkedList<T> {
private Node<T> head;
private Node<T> tail;
private Node<T> current;
private int size;
public DoublyCircularLinkedList() {
this.head = null;
this.tail = null;
this.current = null;
this.size = 0;
}
public void addFirst(T data) {
Node<T> newNode = new Node<>(data, null, null);
if (isEmpty()) {
head = tail = newNode;
head.setNext(head);
head.setPrev(head);
} else {
newNode.setNext(head);
newNode.setPrev(tail);
head.setPrev(newNode);
tail.setNext(newNode);
head = newNode;
}
size++;
}
public void addLast(T data) {
Node<T> newNode = new Node<>(data, null, null);
if (isEmpty()) {
head = tail = newNode;
head.setNext(head);
head.setPrev(head);
} else {
newNode.setNext(head);
newNode.setPrev(tail);
tail.setNext(newNode);
head.setPrev(newNode);
tail = newNode;
}
size++;
}
public T removeFirst() {
if (isEmpty()) throw new NoSuchElementException();
T data = head.getData();
if (size == 1) {
head = tail = null;
} else {
tail.setNext(head.getNext());
head.getNext().setPrev(tail);
head = head.getNext();
}
size--;
return data;
}
public T removeLast() {
if (isEmpty()) throw new NoSuchElementException();
T data = tail.getData();
if (size == 1) {
head = tail = null;
} else {
head.setPrev(tail.getPrev());
tail.getPrev().setNext(head);
tail = tail.getPrev();
}
size--;
return data;
}
public T get(int index) {
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
Node<T> currentNode = head;
for (int i = 0; i < index; i++) {
currentNode = currentNode.getNext();
}
return currentNode.getData();
}
public T first() {
if (isEmpty()) throw new NoSuchElementException();
current = head;
return current.getData();
}
public T last() {
if (isEmpty()) throw new NoSuchElementException();
current = tail;
return current.getData();
}
public boolean remove(T data) {
Node<T> currentNode = head;
for (int i = 0; i < size; i++) {
if (currentNode.getData().equals(data)) {
if (currentNode == head) {
removeFirst();
} else if (currentNode == tail) {
removeLast();
} else {
currentNode.getPrev().setNext(currentNode.getNext());
currentNode.getNext().setPrev(currentNode.getPrev());
size--;
}
return true;
}
currentNode = currentNode.getNext();
}
return false;
}
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public T next() {
if (isEmpty()) throw new NoSuchElementException();
if (current == null) {
current = head;
} else {
current = current.getNext();
}
return current.getData();
}
public T previous() {
if (isEmpty()) throw new NoSuchElementException();
if (current == null) {
current = tail;
} else {
current = current.getPrev();
}
return current.getData();
}
public T getCurrent() {
if (current == null) return head.getData();
return current.getData();
}
public Node<T> getHead() {
return head;
}
}