Skip to content

Commit b418778

Browse files
committed
updated code to use ArrayDeque
1 parent cb50286 commit b418778

File tree

13 files changed

+57
-55
lines changed

13 files changed

+57
-55
lines changed

src/chapter02/TwoPoint7.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package chapter02;
22

3-
import java.util.Stack;
3+
import java.util.ArrayDeque;
44

55
/* Check if Linked List is a palindrome
66
*
@@ -14,9 +14,9 @@ public class TwoPoint7 {
1414
public static boolean palindrome_2(Node head) {
1515
Node slow = head;
1616
Node fast = head;
17-
Stack<Integer> stack = new Stack<>();
17+
ArrayDeque<Integer> deque = new ArrayDeque<>(); // use deque as a stack
1818
while (fast != null && fast.next != null) {
19-
stack.push(slow.data);
19+
deque.push(slow.data);
2020
slow = slow.next;
2121
fast = fast.next.next;
2222
}
@@ -25,7 +25,7 @@ public static boolean palindrome_2(Node head) {
2525
slow = slow.next;
2626
}
2727
while (slow != null) {
28-
if (stack.pop() != slow.data) {
28+
if (deque.pop() != slow.data) {
2929
return false;
3030
}
3131
slow = slow.next;

src/chapter03/MyQueue.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
/* This is problem 3.5 */
66
public class MyQueue<T> { // class is parameterized!
7-
private Stack<T> stack1 = new Stack<T>();
8-
private Stack<T> stack2 = new Stack<T>();
7+
private Stack<T> stack1 = new Stack<>();
8+
private Stack<T> stack2 = new Stack<>();
99

1010
public int size() {
1111
return stack1.size() + stack2.size();

src/chapter03/ThreePoint2.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.Stack;
44

55
public class ThreePoint2 {
6+
/* Can alternatively use ArrayDeque (it's faster) */
67
Stack<Integer> stack = new Stack<>();
78
Stack<Integer> minStack = new Stack<>(); // keeps track of minimums
89

src/chapter03/ThreePoint3.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package chapter03;
22

33
import java.util.ArrayList;
4-
import java.util.Stack;
4+
import java.util.Stack; // can alternatively use ArrayDeque (it's faster)
55

66
public class ThreePoint3 {
77
private ArrayList<Stack<Integer>> stacks = new ArrayList<Stack<Integer>>();

src/chapter03/ThreePoint4.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package chapter03;
22

3-
import java.util.Stack;
3+
import java.util.ArrayDeque;
44

55
public class ThreePoint4 {
66
public static void moveDisks(int n, Tower origin, Tower destination, Tower buffer) {
@@ -21,12 +21,12 @@ public static void moveTop(Tower origin, Tower destination) {
2121

2222
/* It's just a stack, including 1) Error Checking 2) tower number */
2323
class Tower {
24-
private Stack<Integer> disks;
24+
private ArrayDeque<Integer> disks; // use deque as a stack
2525
public int towerNum;
2626

2727
/* Constructor */
2828
public Tower(int towerNum) {
29-
disks = new Stack<>();
29+
disks = new ArrayDeque<>();
3030
this.towerNum = towerNum;
3131
}
3232

src/chapter03/ThreePoint6.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
public class ThreePoint6 {
99
/* Clever Algorithm */
1010
public static Stack<Integer> sort(Stack<Integer> stack) {
11-
Stack<Integer> helperStack = new Stack<Integer>();
11+
Stack<Integer> helperStack = new Stack<Integer>(); // can alternatively use ArrayDeque (it's faster)
1212
while (!stack.isEmpty()) {
1313
Integer curr = stack.pop(); // saving the top of the stack is one of the main tricks.
1414
while (!helperStack.isEmpty() && curr < helperStack.peek()) {

src/chapter04/FourPoint2.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package chapter04;
22

3-
import java.util.Queue;
4-
import java.util.LinkedList;
3+
import java.util.ArrayDeque;
54

65
public class FourPoint2 {
76
/* We do BFS from start node to see if we arrive at end node */
@@ -10,19 +9,19 @@ public static boolean routeExists(GraphNode start, GraphNode end){
109
return true;
1110
}
1211

13-
Queue<GraphNode> queue = new LinkedList<>();
12+
ArrayDeque<GraphNode> deque = new ArrayDeque<>(); // use deque as a queue
1413
start.visit();
15-
queue.add(start);
14+
deque.add(start);
1615

17-
while ( ! queue.isEmpty()) {
18-
GraphNode curr = queue.remove();
16+
while (!deque.isEmpty()) {
17+
GraphNode curr = deque.remove();
1918
if (curr == end) {
2019
return true;
2120
}
2221
for (GraphNode neighbor : curr.getNeighbors()) {
2322
if ( ! neighbor.visited) {
2423
neighbor.visit();
25-
queue.add(neighbor);
24+
deque.add(neighbor);
2625
}
2726
}
2827
}

src/chapter04/GraphFunctions.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package chapter04;
22

3-
import java.util.Queue;
4-
import java.util.LinkedList;
3+
import java.util.ArrayDeque;
54

65
public class GraphFunctions {
76

@@ -10,7 +9,7 @@ public static void connectNodes(GraphNode node1, GraphNode node2) {
109
node2.addNeighbor(node1);
1110
}
1211

13-
/* I do this recursively. Can alternatively do it like BFS (below) but using a Stack instead of a Queue. */
12+
/* I do this recursively. Can alternatively do it like BFS (below) by using a Stack instead of a Queue. */
1413
public static void DFS(GraphNode node, int data) {
1514
if (node == null) {
1615
return;
@@ -40,12 +39,12 @@ public static void BFS(GraphNode node, int data) {
4039
return;
4140
}
4241

43-
Queue<GraphNode> queue = new LinkedList<>();
42+
ArrayDeque<GraphNode> deque = new ArrayDeque<>(); // use deque as a queue
4443
node.visit();
45-
queue.add(node);
44+
deque.add(node);
4645

47-
while ( ! queue.isEmpty()) {
48-
GraphNode curr = queue.remove();
46+
while (!deque.isEmpty()) {
47+
GraphNode curr = deque.remove();
4948

5049
if (curr.data == data) {
5150
System.out.println("BFS found the GraphNode with desired data: " + curr.data);
@@ -55,7 +54,7 @@ public static void BFS(GraphNode node, int data) {
5554
for (GraphNode neighbor : curr.getNeighbors()) {
5655
if ( ! neighbor.visited) {
5756
neighbor.visit(); // make sure to visit node BEFORE we put it on queue instead of when we take it off (to avoid duplicates on queue)
58-
queue.add(neighbor);
57+
deque.add(neighbor);
5958
}
6059
}
6160
}

src/chapter04/TreeFunctions.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package chapter04;
22

3-
import java.util.Queue;
4-
import java.util.LinkedList;
3+
import java.util.ArrayDeque;
54

65
/* I thought it'd be useful to make this class */
76
public class TreeFunctions {
@@ -65,16 +64,16 @@ public static void printPostOrder(TreeNode node) {
6564

6665
/* Prints tree level 0, 1, ... n */
6766
public static void printLevelOrder(TreeNode root) {
68-
Queue<TreeNode> queue = new LinkedList<>(); // Queue is an interface, so we use a LinkedList
69-
queue.add(root);
70-
while ( ! queue.isEmpty()) {
71-
TreeNode n = queue.remove();
67+
ArrayDeque<TreeNode> deque = new ArrayDeque<>(); // use deque as a queue
68+
deque.add(root);
69+
while (!deque.isEmpty()) {
70+
TreeNode n = deque.remove();
7271
System.out.print(n + " ");
7372
if (n.left != null) {
74-
queue.add(n.left);
73+
deque.add(n.left);
7574
}
7675
if (n.right != null) {
77-
queue.add(n.right);
76+
deque.add(n.right);
7877
}
7978
}
8079
}

src/chapter07/SevenPoint7.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package chapter07;
22

3-
import java.util.Queue;
4-
import java.util.LinkedList;
3+
import java.util.ArrayDeque;
54

65
/* Finds kth magic number
76
*
@@ -25,13 +24,15 @@ public static int getKthMagicNumber(int k) {
2524
if (k < 0) {
2625
return -1;
2726
}
28-
Queue<Integer> q3 = new LinkedList<Integer>();
29-
Queue<Integer> q5 = new LinkedList<Integer>();
30-
Queue<Integer> q7 = new LinkedList<Integer>();
31-
27+
28+
ArrayDeque<Integer> q3 = new ArrayDeque<>(); // use deque as a queue
29+
ArrayDeque<Integer> q5 = new ArrayDeque<>(); // use deque as a queue
30+
ArrayDeque<Integer> q7 = new ArrayDeque<>(); // use deque as a queue
31+
3232
q3.add(3);
3333
q5.add(5);
3434
q7.add(7);
35+
3536
int val = 0;
3637
for (int i = 1; i <= k; i++) {
3738
int v3 = q3.peek();

src/chapter08/EightPoint1/Deck.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package chapter08.EightPoint1;
22

3-
import java.util.Queue;
3+
import java.util.ArrayDeque;
44
import java.util.LinkedList;
55

66
public class Deck <T extends Card> {
7-
private Queue<T> deck; // IMPORTANT: can't use a fixed size array[] since we are using generics.
7+
8+
private ArrayDeque<T> deck; // can't use a fixed size array[] since we are using generics.
9+
810
public Deck() {
9-
deck = new LinkedList<T>();
11+
deck = new ArrayDeque<T>();
1012
initializeCards();
1113
}
1214

src/chapter18/Chapter18.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package chapter18;
22

33
import java.util.Arrays;
4-
import java.util.LinkedList;
4+
import java.util.ArrayList;
55

66
public class Chapter18 {
77
public static void main (String [] args) {
@@ -101,7 +101,7 @@ public static void test_EighteenPoint9() {
101101
public static void test_EighteenPoint10() {
102102
System.out.println("\n\n*** Test 18.10: Transform 1 word into another (changing 1 letter at a time)");
103103
EighteenPoint10.setUpDict();
104-
LinkedList<String> solution = EighteenPoint10.convert("Damp", "Like");
104+
ArrayList<String> solution = EighteenPoint10.convert("Damp", "Like");
105105
System.out.println(solution);
106106
}
107107

src/chapter18/EighteenPoint10.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import java.util.HashMap;
44
import java.util.HashSet;
5-
import java.util.LinkedList;
6-
import java.util.Queue;
5+
import java.util.ArrayDeque;
6+
import java.util.ArrayList;
7+
78

89
/* Tricks:
910
* - Use BFS
@@ -25,23 +26,23 @@ public static void setUpDict() {
2526
}
2627

2728
/* Uses BFS */
28-
public static LinkedList<String> convert(String start, String end) {
29+
public static ArrayList<String> convert(String start, String end) {
2930
if (start == null || end == null || start.length() != end.length()) {
3031
return null;
3132
}
3233

3334
start = start.toUpperCase();
3435
end = end.toUpperCase();
3536

36-
Queue<String> queue = new LinkedList<>();
37+
ArrayDeque<String> deque = new ArrayDeque<>(); // use deque as a queue
3738
HashSet<String> visited = new HashSet<>();
3839
HashMap<String, String> backtrackMap = new HashMap<>();
3940

40-
queue.add(start);
41+
deque.add(start);
4142
visited.add(start);
4243

43-
while (! queue.isEmpty()) {
44-
String currWord = queue.remove();
44+
while (! deque.isEmpty()) {
45+
String currWord = deque.remove();
4546
if (currWord.equals(end)) {
4647
return buildSolution(backtrackMap, currWord);
4748
}
@@ -50,7 +51,7 @@ public static LinkedList<String> convert(String start, String end) {
5051
if ( ! visited.contains(neighbor)) {
5152
visited.add(neighbor);
5253
backtrackMap.put(neighbor, currWord);
53-
queue.add(neighbor);
54+
deque.add(neighbor);
5455
}
5556
}
5657
}
@@ -75,8 +76,8 @@ private static HashSet<String> getNeighbors(String currString) {
7576
return validWords;
7677
}
7778

78-
private static LinkedList<String> buildSolution(HashMap<String, String> backtrackMap, String currWord) {
79-
LinkedList<String> solution = new LinkedList<>();
79+
private static ArrayList<String> buildSolution(HashMap<String, String> backtrackMap, String currWord) {
80+
ArrayList<String> solution = new ArrayList<>();
8081
solution.add(currWord);
8182
while (backtrackMap.containsKey(currWord)) {
8283
currWord = backtrackMap.get(currWord);

0 commit comments

Comments
 (0)