forked from reeucq/advanced-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedQueue.java
57 lines (50 loc) · 1.57 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
/**
* Queue is a data structure that that follows the First In First Out (FIFO) principle.
* It has restricted insertion and deletion operations, they are allowed at two different ends of the structure, which are called the front and rear.
* The insertion is performed at the rear end and the deletion is performed at the front end.
* This class implements a queue using an linked list.
*/
public class LinkedQueue<T> {
/**
* Data Members
*/
LinkedList queue;
/** Constructor */
public LinkedQueue() {
queue = new LinkedList();
}
/** @return true iff queue is empty */
public boolean isEmpty() { return queue.isEmpty(); }
/** @return the number of elements in the queue */
public int size() { return queue.getSize(); }
/**
* inserts an element at the rear of the queue
*/
public void insert(T element) {
queue.insert(element, queue.getSize());
}
/**
* removes the front element from the queue and returns it
*/
@SuppressWarnings("unchecked")
public T remove() {
return (T) queue.remove(0);
}
/**
* displays the elements of the queue in a nice format
*/
public void display() {
queue.display();
}
public static void main(String[] args) {
LinkedQueue<Integer> queue = new LinkedQueue<Integer>();
queue.insert(1);
queue.insert(2);
queue.insert(3);
queue.insert(4);
queue.insert(5);
queue.display();
System.out.println("Removed: " + queue.remove());
queue.display();
}
}