-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedQueue.java
63 lines (57 loc) · 1.26 KB
/
LinkedQueue.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* Class implementing Queue with Doubly Linked List.
*
* @param <E> type of the stored elements.
*/
class LinkedQueue<E> implements Queue<E> {
private LinkedList<E> list;
/**
* Constructs empty linked queue.
*/
LinkedQueue() {
list = new LinkedList<>();
}
/**
* Return size of the queue.
*
* @return size
*/
public int size() {
return this.list.size();
}
/**
* Check whether the queue is empty.
*
* @return empty or not
*/
public boolean isEmpty() {
return this.list.size() == 0;
}
/**
* Get the element from the front.
*
* @return top element
* @throws IndexOutOfBoundsException
* if the queue is empty
*/
public E first() throws IndexOutOfBoundsException {
return this.list.get(0);
}
/**
* Insert an element at the rear of the queue.
*
* @param e element to be inserted
*/
public void enqueue(E e) {
this.list.add(this.list.size(), e);
}
/**
* Remove the element from the front.
*
* @throws IndexOutOfBoundsException
* if the queue is empty
*/
public E dequeue() throws IndexOutOfBoundsException {
return this.list.remove(0);
}
}