12
12
* The elements that are added first are the first to be removed.
13
13
* New elements are added to the back/rear of the queue.
14
14
*
15
- * @author sahilb2
15
+ * @author sahilb2 (https://www.github.com/sahilb2)
16
16
*
17
17
*/
18
18
class QueueWithStack {
@@ -58,9 +58,9 @@ public Object remove() {
58
58
/**
59
59
* Peek at the element from the front of the queue
60
60
*
61
- * @return the new front of the queue
61
+ * @return the front element of the queue
62
62
*/
63
- public Object peek () {
63
+ public Object peekFront () {
64
64
if (this .outStack .isEmpty ()) {
65
65
// Move all elements from inStack to outStack (preserving the order)
66
66
while (!this .inStack .isEmpty ()) {
@@ -70,6 +70,15 @@ public Object peek() {
70
70
return this .outStack .peek ();
71
71
}
72
72
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
+
73
82
/**
74
83
* Returns true if the queue is empty
75
84
*
@@ -84,7 +93,7 @@ public boolean isEmpty() {
84
93
/**
85
94
* This class is the example for the Queue class
86
95
*
87
- * @author sahilb2
96
+ * @author sahilb2 (https://www.github.com/sahilb2)
88
97
*
89
98
*/
90
99
public class QueueUsingTwoStacks {
@@ -97,41 +106,46 @@ public class QueueUsingTwoStacks {
97
106
public static void main (String args []){
98
107
QueueWithStack myQueue = new QueueWithStack ();
99
108
myQueue .insert (1 );
109
+ System .out .println (myQueue .peekBack ()); //Will print 1
100
110
// instack: [(top) 1]
101
111
// outStack: []
102
112
myQueue .insert (2 );
113
+ System .out .println (myQueue .peekBack ()); //Will print 2
103
114
// instack: [(top) 2, 1]
104
115
// outStack: []
105
116
myQueue .insert (3 );
117
+ System .out .println (myQueue .peekBack ()); //Will print 3
106
118
// instack: [(top) 3, 2, 1]
107
119
// outStack: []
108
120
myQueue .insert (4 );
121
+ System .out .println (myQueue .peekBack ()); //Will print 4
109
122
// instack: [(top) 4, 3, 2, 1]
110
123
// outStack: []
111
124
112
125
System .out .println (myQueue .isEmpty ()); //Will print false
113
126
114
127
System .out .println (myQueue .remove ()); //Will print 1
128
+ System .out .println (myQueue .peekBack ()); //Will print NULL
115
129
// instack: []
116
130
// outStack: [(top) 2, 3, 4]
117
131
118
132
myQueue .insert (5 );
119
- System .out .println (myQueue .peek ()); //Will print 2
133
+ System .out .println (myQueue .peekFront ()); //Will print 2
120
134
// instack: [(top) 5]
121
135
// outStack: [(top) 2, 3, 4]
122
136
123
137
myQueue .remove ();
124
- System .out .println (myQueue .peek ()); //Will print 3
138
+ System .out .println (myQueue .peekFront ()); //Will print 3
125
139
// instack: [(top) 5]
126
140
// outStack: [(top) 3, 4]
127
141
myQueue .remove ();
128
- System .out .println (myQueue .peek ()); //Will print 4
142
+ System .out .println (myQueue .peekFront ()); //Will print 4
129
143
// instack: [(top) 5]
130
144
// outStack: [(top) 4]
131
145
myQueue .remove ();
132
146
// instack: [(top) 5]
133
147
// outStack: []
134
- System .out .println (myQueue .peek ()); //Will print 5
148
+ System .out .println (myQueue .peekFront ()); //Will print 5
135
149
// instack: []
136
150
// outStack: [(top) 5]
137
151
myQueue .remove ();
0 commit comments