15
15
* @author sahilb2
16
16
*
17
17
*/
18
- class QueueUsingTwoStacks {
18
+ class QueueWithStack {
19
19
20
20
// Stack to keep track of elements inserted into the queue
21
21
private Stack inStack ;
@@ -25,7 +25,7 @@ class QueueUsingTwoStacks {
25
25
/**
26
26
* Constructor
27
27
*/
28
- public QueueUsingTwoStacks () {
28
+ public QueueWithStack () {
29
29
this .inStack = new Stack ();
30
30
this .outStack = new Stack ();
31
31
}
@@ -65,3 +65,59 @@ public boolean isEmpty() {
65
65
}
66
66
67
67
}
68
+
69
+ /**
70
+ * This class is the example for the Queue class
71
+ *
72
+ * @author sahilb2
73
+ *
74
+ */
75
+ public class QueueUsingTwoStacks {
76
+
77
+ /**
78
+ * Main method
79
+ *
80
+ * @param args Command line arguments
81
+ */
82
+ public static void main (String args []){
83
+ QueueWithStack myQueue = new QueueWithStack ();
84
+ myQueue .insert (1 );
85
+ // instack: [(top) 1]
86
+ // outStack: []
87
+ myQueue .insert (2 );
88
+ // instack: [(top) 2, 1]
89
+ // outStack: []
90
+ myQueue .insert (3 );
91
+ // instack: [(top) 3, 2, 1]
92
+ // outStack: []
93
+ myQueue .insert (4 );
94
+ // instack: [(top) 4, 3, 2, 1]
95
+ // outStack: []
96
+
97
+ System .out .println (myQueue .isEmpty ()); //Will print false
98
+
99
+ System .out .println (myQueue .remove ()); //Will print 1
100
+ // instack: []
101
+ // outStack: [(top) 2, 3, 4]
102
+
103
+ myQueue .insert (5 );
104
+ // instack: [(top) 5]
105
+ // outStack: [(top) 2, 3, 4]
106
+
107
+ myQueue .remove ();
108
+ // instack: [(top) 5]
109
+ // outStack: [(top) 3, 4]
110
+ myQueue .remove ();
111
+ // instack: [(top) 5]
112
+ // outStack: [(top) 4]
113
+ myQueue .remove ();
114
+ // instack: [(top) 5]
115
+ // outStack: []
116
+ myQueue .remove ();
117
+ // instack: []
118
+ // outStack: []
119
+
120
+ System .out .println (myQueue .isEmpty ()); //Will print true
121
+
122
+ }
123
+ }
0 commit comments