1515 * @author sahilb2
1616 *
1717 */
18- class QueueUsingTwoStacks {
18+ class QueueWithStack {
1919
2020 // Stack to keep track of elements inserted into the queue
2121 private Stack inStack ;
@@ -25,7 +25,7 @@ class QueueUsingTwoStacks {
2525 /**
2626 * Constructor
2727 */
28- public QueueUsingTwoStacks () {
28+ public QueueWithStack () {
2929 this .inStack = new Stack ();
3030 this .outStack = new Stack ();
3131 }
@@ -65,3 +65,59 @@ public boolean isEmpty() {
6565 }
6666
6767}
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