-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.java
39 lines (31 loc) · 852 Bytes
/
Queue.java
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
package linear;
public class Queue {
LinkedList linkedList = new LinkedList();
int size = 0;
void enqueue(int value){
linkedList.insertLast(value);
size ++;
}
int dequeue(){
if(isEmpty()) throw new EmptyQueueException("Cannot dequeue from an empty queue.");
int deletedNodeValue = linkedList.head.data;
linkedList.deleteFirst();
size--;
return deletedNodeValue;
}
int peek(){
if(isEmpty()) throw new EmptyQueueException("Cannot peek from an empty queue.");
return linkedList.head.data;
}
boolean isEmpty(){
return linkedList.head == null;
}
int size(){
return size;
}
}
class EmptyQueueException extends RuntimeException {
public EmptyQueueException(String message) {
super(message);
}
}