File tree 3 files changed +147
-0
lines changed
contents/stacks_and_queues
3 files changed +147
-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 .front ());
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
+ * 'front' returns the first element of the queue without removing it
41
+ */
42
+ T front ();
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 front () {
68
+ return this .list .get (0 );
69
+ }
70
+
71
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .List ;
2
+ import java .util .ArrayList ;
3
+
4
+
5
+ public class StackTest {
6
+
7
+ public static void main (String [] args ) {
8
+ IStack <Integer > intStack = new Stack <>();
9
+
10
+ intStack .push (4 );
11
+ intStack .push (5 );
12
+ intStack .push (9 );
13
+
14
+ System .out .println (intStack .pop ());
15
+ System .out .println (intStack .size ());
16
+ System .out .println (intStack .top ());
17
+ }
18
+
19
+ }
20
+
21
+
22
+ interface IStack <T > {
23
+ /*
24
+ * 'pop' removed the last element from the stack and returns it
25
+ */
26
+ T pop ();
27
+
28
+ /*
29
+ * 'push' adds an element to at the end of the stack and returns the new size
30
+ */
31
+ int push (T element );
32
+
33
+ /*
34
+ * 'size' returns the length of the stack
35
+ */
36
+ int size ();
37
+
38
+ /*
39
+ * 'top' returns the first element of the stack
40
+ */
41
+ T top ();
42
+ }
43
+
44
+
45
+ public class Stack <T > implements IStack <T > {
46
+
47
+ private List <T > list ;
48
+
49
+ public Stack () {
50
+ this .list = new ArrayList <>();
51
+ }
52
+
53
+ public T pop () {
54
+ return this .list .remove (this .size () - 1 );
55
+ }
56
+
57
+ public int push (T element ) {
58
+ this .list .add (element );
59
+ return this .size ();
60
+ }
61
+
62
+ public int size () {
63
+ return this .list .size ();
64
+ }
65
+
66
+ public T top () {
67
+ return this .list .get (this .size () - 1 );
68
+ }
69
+
70
+ }
71
+
72
+
Original file line number Diff line number Diff line change @@ -18,12 +18,16 @@ Here is a simple implementation of a stack:
18
18
{% method %}
19
19
{% sample lang="ts" %}
20
20
[ import, lang:"typescript"] ( code/typescript/stack.ts )
21
+ {% sample lang="java" %}
22
+ [ import, lang:"java"] ( code/java/Stack.java )
21
23
{% endmethod %}
22
24
23
25
Here is a simple implementation of a queue:
24
26
{% method %}
25
27
{% sample lang="ts" %}
26
28
[ import, lang:"typescript"] ( code/typescript/queue.ts )
29
+ {% sample lang="java" %}
30
+ [ import, lang:"java" ] ( code/java/Queue.java )
27
31
{% endmethod %}
28
32
29
33
You can’t perform that action at this time.
0 commit comments