Skip to content

Commit 18e876f

Browse files
committed
Minor bug fixes
1 parent b6678a1 commit 18e876f

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

src/chapter03/MyQueue.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,19 @@ public T dequeue(){
2525
}
2626

2727
public T peek(){
28-
shiftStacks();
28+
if (size() == 0)
29+
return null;
30+
if (stack2.isEmpty())
31+
shiftStacks();
2932
return stack2.peek();
3033
}
3134

32-
/* Great helper function */
3335
private void shiftStacks(){
34-
while (!stack1.isEmpty()){
35-
T temp = stack1.pop();
36-
stack2.push(temp);
36+
if (stack2.isEmpty()){ // shifting items while stack2 has elements in it would mess up our queue's ordering
37+
while (!stack1.isEmpty()){
38+
T temp = stack1.pop();
39+
stack2.push(temp);
40+
}
3741
}
3842
}
3943
}

src/chapter18/EighteenPoint09.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
* maxheap.size()-1 == minheap.size() always holds.
1212
*/
1313
public class EighteenPoint09 { //code from website
14-
private static PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(50, Collections.reverseOrder()); // maxHeap contains all SMALL elements
15-
private static PriorityQueue<Integer> minHeap = new PriorityQueue<Integer>(50); // minHeap contains all LARGE elements
14+
private static PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(Collections.reverseOrder()); // maxHeap contains all SMALL elements
15+
private static PriorityQueue<Integer> minHeap = new PriorityQueue<Integer>(); // minHeap contains all LARGE elements
1616

1717
public static void addNum(int n) {
1818
if (maxHeap.isEmpty())

0 commit comments

Comments
 (0)