File tree Expand file tree Collapse file tree 1 file changed +71
-0
lines changed
contents/stacks_and_queues/code/java Expand file tree Collapse file tree 1 file changed +71
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments