1212 * The elements that are added first are the first to be removed.
1313 * New elements are added to the back/rear of the queue.
1414 *
15- * @author sahilb2
15+ * @author sahilb2 (https://www.github.com/sahilb2)
1616 *
1717 */
1818class QueueWithStack {
@@ -58,9 +58,9 @@ public Object remove() {
5858 /**
5959 * Peek at the element from the front of the queue
6060 *
61- * @return the new front of the queue
61+ * @return the front element of the queue
6262 */
63- public Object peek () {
63+ public Object peekFront () {
6464 if (this .outStack .isEmpty ()) {
6565 // Move all elements from inStack to outStack (preserving the order)
6666 while (!this .inStack .isEmpty ()) {
@@ -70,6 +70,15 @@ public Object peek() {
7070 return this .outStack .peek ();
7171 }
7272
73+ /**
74+ * Peek at the element from the back of the queue
75+ *
76+ * @return the back element of the queue
77+ */
78+ public Object peekBack () {
79+ return this .inStack .peek ();
80+ }
81+
7382 /**
7483 * Returns true if the queue is empty
7584 *
@@ -84,7 +93,7 @@ public boolean isEmpty() {
8493/**
8594 * This class is the example for the Queue class
8695 *
87- * @author sahilb2
96+ * @author sahilb2 (https://www.github.com/sahilb2)
8897 *
8998 */
9099public class QueueUsingTwoStacks {
@@ -97,41 +106,46 @@ public class QueueUsingTwoStacks {
97106 public static void main (String args []){
98107 QueueWithStack myQueue = new QueueWithStack ();
99108 myQueue .insert (1 );
109+ System .out .println (myQueue .peekBack ()); //Will print 1
100110 // instack: [(top) 1]
101111 // outStack: []
102112 myQueue .insert (2 );
113+ System .out .println (myQueue .peekBack ()); //Will print 2
103114 // instack: [(top) 2, 1]
104115 // outStack: []
105116 myQueue .insert (3 );
117+ System .out .println (myQueue .peekBack ()); //Will print 3
106118 // instack: [(top) 3, 2, 1]
107119 // outStack: []
108120 myQueue .insert (4 );
121+ System .out .println (myQueue .peekBack ()); //Will print 4
109122 // instack: [(top) 4, 3, 2, 1]
110123 // outStack: []
111124
112125 System .out .println (myQueue .isEmpty ()); //Will print false
113126
114127 System .out .println (myQueue .remove ()); //Will print 1
128+ System .out .println (myQueue .peekBack ()); //Will print NULL
115129 // instack: []
116130 // outStack: [(top) 2, 3, 4]
117131
118132 myQueue .insert (5 );
119- System .out .println (myQueue .peek ()); //Will print 2
133+ System .out .println (myQueue .peekFront ()); //Will print 2
120134 // instack: [(top) 5]
121135 // outStack: [(top) 2, 3, 4]
122136
123137 myQueue .remove ();
124- System .out .println (myQueue .peek ()); //Will print 3
138+ System .out .println (myQueue .peekFront ()); //Will print 3
125139 // instack: [(top) 5]
126140 // outStack: [(top) 3, 4]
127141 myQueue .remove ();
128- System .out .println (myQueue .peek ()); //Will print 4
142+ System .out .println (myQueue .peekFront ()); //Will print 4
129143 // instack: [(top) 5]
130144 // outStack: [(top) 4]
131145 myQueue .remove ();
132146 // instack: [(top) 5]
133147 // outStack: []
134- System .out .println (myQueue .peek ()); //Will print 5
148+ System .out .println (myQueue .peekFront ()); //Will print 5
135149 // instack: []
136150 // outStack: [(top) 5]
137151 myQueue .remove ();
0 commit comments