Skip to content

Commit b073969

Browse files
committed
Implemented queue in java
1 parent a1019d4 commit b073969

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import java.util.List;
2+
import java.util.ArrayList;
3+
4+
public class QueueTest {
5+
6+
public static void main(String[] args) {
7+
IQueue<Integer> intQueue = new Queue<>();
8+
9+
intQueue.enqueue(4);
10+
intQueue.enqueue(5);
11+
intQueue.enqueue(9);
12+
13+
System.out.println(intQueue.dequeue());
14+
System.out.println(intQueue.size());
15+
System.out.println(intQueue.peek());
16+
}
17+
18+
}
19+
20+
21+
interface IQueue<T> {
22+
23+
/*
24+
* 'dequeue' removes the first element from the queue and returns it
25+
*/
26+
T dequeue();
27+
28+
/*
29+
* 'enqueue' adds an element at the end of the queue and returns the new size
30+
*/
31+
int enqueue(T element);
32+
33+
34+
/*
35+
* 'size' returns the size of the queue
36+
*/
37+
int size();
38+
39+
/*
40+
* 'peek' returns the first element of the queue without removing it
41+
*/
42+
T peek();
43+
}
44+
45+
46+
public class Queue<T> implements IQueue<T> {
47+
48+
private List<T> list;
49+
50+
public Queue() {
51+
this.list = new ArrayList();
52+
}
53+
54+
public T dequeue() {
55+
return this.list.remove(0);
56+
}
57+
58+
public int enqueue(T element) {
59+
this.list.add(element);
60+
return this.size();
61+
}
62+
63+
public int size() {
64+
return this.list.size();
65+
}
66+
67+
public T peek() {
68+
return this.list.get(0);
69+
}
70+
71+
}

0 commit comments

Comments
 (0)