From f485b9effb5fef21edce0615bb189577163a9b24 Mon Sep 17 00:00:00 2001 From: ttskym Date: Thu, 4 Jun 2020 00:17:14 +0800 Subject: [PATCH 01/11] add array stack --- .../datastructures/stack/Stack.java | 53 ++-------------- .../datastructures/stack/impl/ArrayStack.java | 60 +++++++++++++++++++ .../stack/{ => impl}/IntStack.java | 25 +++++++- .../datastructures/stack/impl/ListStack.java | 56 +++++++++++++++++ .../datastructures/stack/StackTest.java | 4 +- 5 files changed, 147 insertions(+), 51 deletions(-) create mode 100644 src/main/java/com/williamfiset/algorithms/datastructures/stack/impl/ArrayStack.java rename src/main/java/com/williamfiset/algorithms/datastructures/stack/{ => impl}/IntStack.java (74%) create mode 100644 src/main/java/com/williamfiset/algorithms/datastructures/stack/impl/ListStack.java diff --git a/src/main/java/com/williamfiset/algorithms/datastructures/stack/Stack.java b/src/main/java/com/williamfiset/algorithms/datastructures/stack/Stack.java index a9b5fe669..1662d5e56 100644 --- a/src/main/java/com/williamfiset/algorithms/datastructures/stack/Stack.java +++ b/src/main/java/com/williamfiset/algorithms/datastructures/stack/Stack.java @@ -1,54 +1,13 @@ -/** - * A linked list implementation of a stack - * - * @author William Fiset, william.alexandre.fiset@gmail.com - */ package com.williamfiset.algorithms.datastructures.stack; -public class Stack implements Iterable { +public interface Stack { + public int size(); - private java.util.LinkedList list = new java.util.LinkedList(); + public boolean isEmpty(); - // Create an empty stack - public Stack() {} + public void push(T elem); - // Create a Stack with an initial element - public Stack(T firstElem) { - push(firstElem); - } + public T pop(); - // Return the number of elements in the stack - public int size() { - return list.size(); - } - - // Check if the stack is empty - public boolean isEmpty() { - return size() == 0; - } - - // Push an element on the stack - public void push(T elem) { - list.addLast(elem); - } - - // Pop an element off the stack - // Throws an error is the stack is empty - public T pop() { - if (isEmpty()) throw new java.util.EmptyStackException(); - return list.removeLast(); - } - - // Peek the top of the stack without removing an element - // Throws an exception if the stack is empty - public T peek() { - if (isEmpty()) throw new java.util.EmptyStackException(); - return list.peekLast(); - } - - // Allow users to iterate through the stack using an iterator - @Override - public java.util.Iterator iterator() { - return list.iterator(); - } + public T peek(); } diff --git a/src/main/java/com/williamfiset/algorithms/datastructures/stack/impl/ArrayStack.java b/src/main/java/com/williamfiset/algorithms/datastructures/stack/impl/ArrayStack.java new file mode 100644 index 000000000..a14193e14 --- /dev/null +++ b/src/main/java/com/williamfiset/algorithms/datastructures/stack/impl/ArrayStack.java @@ -0,0 +1,60 @@ +package com.williamfiset.algorithms.datastructures.stack.impl; + +import com.williamfiset.algorithms.datastructures.stack.Stack; +import java.util.Arrays; +import java.util.EmptyStackException; + +public class ArrayStack implements Stack { + private int capacity; + private Object[] data; + private int size; + + public ArrayStack() { + // the factor of 10 is the magic of speed!! + capacity = 10; + size = 0; + data = new Object[capacity]; + } + + @Override + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return 0 == size; + } + + @Override + public void push(T elem) { + if (size == capacity) { + adjustCap(); + } + data[size++] = elem; + } + + private void adjustCap() { + if (capacity == Integer.MAX_VALUE) { + throw new OutOfMemoryError(); + } + capacity += capacity; + data = Arrays.copyOf(data, capacity); + } + + @Override + @SuppressWarnings("unchecked") + public T pop() { + if (isEmpty()) throw new EmptyStackException(); + T elem = (T) data[--size]; + data[size] = null; + return elem; + } + + @Override + @SuppressWarnings("unchecked") + public T peek() { + if (isEmpty()) throw new EmptyStackException(); + return (T) data[size - 1]; + } +} diff --git a/src/main/java/com/williamfiset/algorithms/datastructures/stack/IntStack.java b/src/main/java/com/williamfiset/algorithms/datastructures/stack/impl/IntStack.java similarity index 74% rename from src/main/java/com/williamfiset/algorithms/datastructures/stack/IntStack.java rename to src/main/java/com/williamfiset/algorithms/datastructures/stack/impl/IntStack.java index 0e8b6e026..db71e6b95 100644 --- a/src/main/java/com/williamfiset/algorithms/datastructures/stack/IntStack.java +++ b/src/main/java/com/williamfiset/algorithms/datastructures/stack/impl/IntStack.java @@ -7,7 +7,9 @@ * * @author William Fiset, william.alexandre.fiset@gmail.com */ -package com.williamfiset.algorithms.datastructures.stack; +package com.williamfiset.algorithms.datastructures.stack.impl; + +import com.williamfiset.algorithms.datastructures.stack.Stack; public class IntStack { @@ -83,13 +85,30 @@ private static void benchMarkTest() { System.out.println("IntStack Time: " + (end - start) / 1e9); // ArrayDeque times at around 1.438 seconds - java.util.ArrayDeque arrayDeque = new java.util.ArrayDeque<>(); - // java.util.ArrayDeque arrayDeque = new java.util.ArrayDeque<>(n); // strangely the + // java.util.ArrayDeque arrayDeque = new java.util.ArrayDeque<>(); + // java.util.Stack arrayDeque = new java.util.Stack<>(); + java.util.ArrayDeque arrayDeque = new java.util.ArrayDeque<>(n); // strangely the // ArrayQueue is slower when you give it an initial capacity. start = System.nanoTime(); for (int i = 0; i < n; i++) arrayDeque.push(i); for (int i = 0; i < n; i++) arrayDeque.pop(); end = System.nanoTime(); System.out.println("ArrayDeque Time: " + (end - start) / 1e9); + + Stack listStack = new ListStack<>(); + + start = System.nanoTime(); + for (int i = 0; i < n; i++) listStack.push(i); + for (int i = 0; i < n; i++) listStack.pop(); + end = System.nanoTime(); + System.out.println("ListStack Time: " + (end - start) / 1e9); + + Stack arrayStack = new ArrayStack<>(); + + start = System.nanoTime(); + for (int i = 0; i < n; i++) arrayStack.push(i); + for (int i = 0; i < n; i++) arrayStack.pop(); + end = System.nanoTime(); + System.out.println("ArrayStack Time: " + (end - start) / 1e9); } } diff --git a/src/main/java/com/williamfiset/algorithms/datastructures/stack/impl/ListStack.java b/src/main/java/com/williamfiset/algorithms/datastructures/stack/impl/ListStack.java new file mode 100644 index 000000000..f6be1d904 --- /dev/null +++ b/src/main/java/com/williamfiset/algorithms/datastructures/stack/impl/ListStack.java @@ -0,0 +1,56 @@ +/** + * A linked list implementation of a stack + * + * @author William Fiset, william.alexandre.fiset@gmail.com + */ +package com.williamfiset.algorithms.datastructures.stack.impl; + +import com.williamfiset.algorithms.datastructures.stack.Stack; + +public class ListStack implements Iterable, Stack { + + private java.util.LinkedList list = new java.util.LinkedList(); + + // Create an empty stack + public ListStack() {} + + // Create a Stack with an initial element + public ListStack(T firstElem) { + push(firstElem); + } + + // Return the number of elements in the stack + public int size() { + return list.size(); + } + + // Check if the stack is empty + public boolean isEmpty() { + return size() == 0; + } + + // Push an element on the stack + public void push(T elem) { + list.addLast(elem); + } + + // Pop an element off the stack + // Throws an error is the stack is empty + public T pop() { + if (isEmpty()) throw new java.util.EmptyStackException(); + return list.removeLast(); + } + + // Peek the top of the stack without removing an element + // Throws an exception if the stack is empty + public T peek() { + if (isEmpty()) throw new java.util.EmptyStackException(); + return list.peekLast(); + } + + // Allow users to iterate through the stack using an iterator + @Override + public java.util.Iterator iterator() { + return list.iterator(); + } +} diff --git a/src/test/java/com/williamfiset/algorithms/datastructures/stack/StackTest.java b/src/test/java/com/williamfiset/algorithms/datastructures/stack/StackTest.java index f94000f45..ce562051a 100644 --- a/src/test/java/com/williamfiset/algorithms/datastructures/stack/StackTest.java +++ b/src/test/java/com/williamfiset/algorithms/datastructures/stack/StackTest.java @@ -3,6 +3,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import com.williamfiset.algorithms.datastructures.stack.impl.ListStack; import org.junit.Before; import org.junit.Test; @@ -12,7 +13,8 @@ public class StackTest { @Before public void setup() { - stack = new Stack(); + stack = new ListStack(); + // stack = new ArrayStack<>(); } @Test From 75ecbea69be1dccd80732273dc49dbcae3bf1fdd Mon Sep 17 00:00:00 2001 From: ttskym Date: Thu, 4 Jun 2020 00:22:09 +0800 Subject: [PATCH 02/11] add array stack --- .../algorithms/datastructures/stack/Stack.java | 7 +++++++ .../algorithms/datastructures/stack/impl/ArrayStack.java | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/src/main/java/com/williamfiset/algorithms/datastructures/stack/Stack.java b/src/main/java/com/williamfiset/algorithms/datastructures/stack/Stack.java index 1662d5e56..61cacfcb5 100644 --- a/src/main/java/com/williamfiset/algorithms/datastructures/stack/Stack.java +++ b/src/main/java/com/williamfiset/algorithms/datastructures/stack/Stack.java @@ -1,12 +1,19 @@ package com.williamfiset.algorithms.datastructures.stack; +/** + * @author liujingkun + */ public interface Stack { + // return the number of elements in the stack public int size(); + // return if the stack is empty public boolean isEmpty(); + // push the element on the stack public void push(T elem); + // pop the element off the stack public T pop(); public T peek(); diff --git a/src/main/java/com/williamfiset/algorithms/datastructures/stack/impl/ArrayStack.java b/src/main/java/com/williamfiset/algorithms/datastructures/stack/impl/ArrayStack.java index a14193e14..06db035bc 100644 --- a/src/main/java/com/williamfiset/algorithms/datastructures/stack/impl/ArrayStack.java +++ b/src/main/java/com/williamfiset/algorithms/datastructures/stack/impl/ArrayStack.java @@ -4,6 +4,9 @@ import java.util.Arrays; import java.util.EmptyStackException; +/** + * @author liujingkun + */ public class ArrayStack implements Stack { private int capacity; private Object[] data; @@ -34,6 +37,7 @@ public void push(T elem) { data[size++] = elem; } + // adjust the capacity to store more element private void adjustCap() { if (capacity == Integer.MAX_VALUE) { throw new OutOfMemoryError(); From 33f65cd3f676f787932e8236abee0f7200d217a6 Mon Sep 17 00:00:00 2001 From: ttskym Date: Thu, 4 Jun 2020 00:30:56 +0800 Subject: [PATCH 03/11] verify google java format --- .../williamfiset/algorithms/datastructures/stack/Stack.java | 4 +--- .../algorithms/datastructures/stack/impl/ArrayStack.java | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/williamfiset/algorithms/datastructures/stack/Stack.java b/src/main/java/com/williamfiset/algorithms/datastructures/stack/Stack.java index 61cacfcb5..15a269688 100644 --- a/src/main/java/com/williamfiset/algorithms/datastructures/stack/Stack.java +++ b/src/main/java/com/williamfiset/algorithms/datastructures/stack/Stack.java @@ -1,8 +1,6 @@ package com.williamfiset.algorithms.datastructures.stack; -/** - * @author liujingkun - */ +/** @author liujingkun */ public interface Stack { // return the number of elements in the stack public int size(); diff --git a/src/main/java/com/williamfiset/algorithms/datastructures/stack/impl/ArrayStack.java b/src/main/java/com/williamfiset/algorithms/datastructures/stack/impl/ArrayStack.java index 06db035bc..9ced6057b 100644 --- a/src/main/java/com/williamfiset/algorithms/datastructures/stack/impl/ArrayStack.java +++ b/src/main/java/com/williamfiset/algorithms/datastructures/stack/impl/ArrayStack.java @@ -4,9 +4,7 @@ import java.util.Arrays; import java.util.EmptyStackException; -/** - * @author liujingkun - */ +/** @author liujingkun */ public class ArrayStack implements Stack { private int capacity; private Object[] data; From 735974b08e56c4f32d721400e7497ccb8f775170 Mon Sep 17 00:00:00 2001 From: ttskym Date: Fri, 5 Jun 2020 00:48:42 +0800 Subject: [PATCH 04/11] array stack --- .../stack/{impl => }/ArrayStack.java | 8 +- .../stack/{impl => }/IntStack.java | 4 +- .../stack/{impl => }/ListStack.java | 4 +- .../datastructures/stack/StackTest.java | 78 +++++++++++-------- 4 files changed, 51 insertions(+), 43 deletions(-) rename src/main/java/com/williamfiset/algorithms/datastructures/stack/{impl => }/ArrayStack.java (83%) rename src/main/java/com/williamfiset/algorithms/datastructures/stack/{impl => }/IntStack.java (96%) rename src/main/java/com/williamfiset/algorithms/datastructures/stack/{impl => }/ListStack.java (90%) diff --git a/src/main/java/com/williamfiset/algorithms/datastructures/stack/impl/ArrayStack.java b/src/main/java/com/williamfiset/algorithms/datastructures/stack/ArrayStack.java similarity index 83% rename from src/main/java/com/williamfiset/algorithms/datastructures/stack/impl/ArrayStack.java rename to src/main/java/com/williamfiset/algorithms/datastructures/stack/ArrayStack.java index 9ced6057b..4b42e9f12 100644 --- a/src/main/java/com/williamfiset/algorithms/datastructures/stack/impl/ArrayStack.java +++ b/src/main/java/com/williamfiset/algorithms/datastructures/stack/ArrayStack.java @@ -1,6 +1,5 @@ -package com.williamfiset.algorithms.datastructures.stack.impl; +package com.williamfiset.algorithms.datastructures.stack; -import com.williamfiset.algorithms.datastructures.stack.Stack; import java.util.Arrays; import java.util.EmptyStackException; @@ -11,8 +10,7 @@ public class ArrayStack implements Stack { private int size; public ArrayStack() { - // the factor of 10 is the magic of speed!! - capacity = 10; + capacity = 16; size = 0; data = new Object[capacity]; } @@ -24,7 +22,7 @@ public int size() { @Override public boolean isEmpty() { - return 0 == size; + return size == 0; } @Override diff --git a/src/main/java/com/williamfiset/algorithms/datastructures/stack/impl/IntStack.java b/src/main/java/com/williamfiset/algorithms/datastructures/stack/IntStack.java similarity index 96% rename from src/main/java/com/williamfiset/algorithms/datastructures/stack/impl/IntStack.java rename to src/main/java/com/williamfiset/algorithms/datastructures/stack/IntStack.java index db71e6b95..bbd99d698 100644 --- a/src/main/java/com/williamfiset/algorithms/datastructures/stack/impl/IntStack.java +++ b/src/main/java/com/williamfiset/algorithms/datastructures/stack/IntStack.java @@ -7,9 +7,7 @@ * * @author William Fiset, william.alexandre.fiset@gmail.com */ -package com.williamfiset.algorithms.datastructures.stack.impl; - -import com.williamfiset.algorithms.datastructures.stack.Stack; +package com.williamfiset.algorithms.datastructures.stack; public class IntStack { diff --git a/src/main/java/com/williamfiset/algorithms/datastructures/stack/impl/ListStack.java b/src/main/java/com/williamfiset/algorithms/datastructures/stack/ListStack.java similarity index 90% rename from src/main/java/com/williamfiset/algorithms/datastructures/stack/impl/ListStack.java rename to src/main/java/com/williamfiset/algorithms/datastructures/stack/ListStack.java index f6be1d904..de270a2a1 100644 --- a/src/main/java/com/williamfiset/algorithms/datastructures/stack/impl/ListStack.java +++ b/src/main/java/com/williamfiset/algorithms/datastructures/stack/ListStack.java @@ -3,9 +3,7 @@ * * @author William Fiset, william.alexandre.fiset@gmail.com */ -package com.williamfiset.algorithms.datastructures.stack.impl; - -import com.williamfiset.algorithms.datastructures.stack.Stack; +package com.williamfiset.algorithms.datastructures.stack; public class ListStack implements Iterable, Stack { diff --git a/src/test/java/com/williamfiset/algorithms/datastructures/stack/StackTest.java b/src/test/java/com/williamfiset/algorithms/datastructures/stack/StackTest.java index ce562051a..e26a26410 100644 --- a/src/test/java/com/williamfiset/algorithms/datastructures/stack/StackTest.java +++ b/src/test/java/com/williamfiset/algorithms/datastructures/stack/StackTest.java @@ -1,73 +1,87 @@ package com.williamfiset.algorithms.datastructures.stack; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; -import com.williamfiset.algorithms.datastructures.stack.impl.ListStack; +import java.util.ArrayList; +import java.util.List; import org.junit.Before; import org.junit.Test; public class StackTest { - Stack stack; + List stacks = new ArrayList<>(); @Before public void setup() { - stack = new ListStack(); - // stack = new ArrayStack<>(); + stacks.add(new ListStack()); + stacks.add(new ArrayStack<>()); } @Test public void testEmptyStack() { - assertTrue(stack.isEmpty()); - assertEquals(stack.size(), 0); + for (Stack stack : stacks) { + assertTrue(stack.isEmpty()); + assertEquals(stack.size(), 0); + } } @Test(expected = Exception.class) public void testPopOnEmpty() { - stack.pop(); + for (Stack stack : stacks) { + assertThrows(Exception.class, stack::pop); + } } @Test(expected = Exception.class) public void testPeekOnEmpty() { - stack.peek(); + for (Stack stack : stacks) { + stack.peek(); + } } @Test public void testPush() { - stack.push(2); - assertEquals(stack.size(), 1); + for (Stack stack : stacks) { + stack.push(2); + assertEquals(stack.size(), 1); + } } @Test public void testPeek() { - stack.push(2); - assertTrue(stack.peek() == 2); - assertEquals(stack.size(), 1); + for (Stack stack : stacks) { + stack.push(2); + assertEquals(2, (int) (Integer) stack.peek()); + assertEquals(stack.size(), 1); + } } @Test public void testPop() { - stack.push(2); - assertTrue(stack.pop() == 2); - assertEquals(stack.size(), 0); + for (Stack stack : stacks) { + stack.push(2); + assertEquals(2, (int) stack.pop()); + assertEquals(stack.size(), 0); + } } @Test public void testExhaustively() { - assertTrue(stack.isEmpty()); - stack.push(1); - assertTrue(!stack.isEmpty()); - stack.push(2); - assertEquals(stack.size(), 2); - assertTrue(stack.peek() == 2); - assertEquals(stack.size(), 2); - assertTrue(stack.pop() == 2); - assertEquals(stack.size(), 1); - assertTrue(stack.peek() == 1); - assertEquals(stack.size(), 1); - assertTrue(stack.pop() == 1); - assertEquals(stack.size(), 0); - assertTrue(stack.isEmpty()); + for (Stack stack : stacks) { + assertTrue(stack.isEmpty()); + stack.push(1); + assertTrue(!stack.isEmpty()); + stack.push(2); + assertEquals(stack.size(), 2); + assertEquals(2, (int) stack.peek()); + assertEquals(stack.size(), 2); + assertEquals(2, (int) stack.pop()); + assertEquals(stack.size(), 1); + assertEquals(1, (int) stack.peek()); + assertEquals(stack.size(), 1); + assertEquals(1, (int) stack.pop()); + assertEquals(stack.size(), 0); + assertTrue(stack.isEmpty()); + } } } From 3d427b460c34c966ec099c5bacfdf3c0b677ab89 Mon Sep 17 00:00:00 2001 From: ttskym Date: Fri, 5 Jun 2020 00:57:37 +0800 Subject: [PATCH 05/11] array stack --- .../datastructures/stack/StackTest.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/test/java/com/williamfiset/algorithms/datastructures/stack/StackTest.java b/src/test/java/com/williamfiset/algorithms/datastructures/stack/StackTest.java index e26a26410..593762c8e 100644 --- a/src/test/java/com/williamfiset/algorithms/datastructures/stack/StackTest.java +++ b/src/test/java/com/williamfiset/algorithms/datastructures/stack/StackTest.java @@ -6,15 +6,16 @@ import java.util.List; import org.junit.Before; import org.junit.Test; +import org.junit.function.ThrowingRunnable; public class StackTest { - List stacks = new ArrayList<>(); + private List> stacks = new ArrayList<>(); @Before public void setup() { stacks.add(new ListStack()); - stacks.add(new ArrayStack<>()); + stacks.add(new ArrayStack()); } @Test @@ -25,7 +26,7 @@ public void testEmptyStack() { } } - @Test(expected = Exception.class) + @Test public void testPopOnEmpty() { for (Stack stack : stacks) { assertThrows(Exception.class, stack::pop); @@ -35,13 +36,13 @@ public void testPopOnEmpty() { @Test(expected = Exception.class) public void testPeekOnEmpty() { for (Stack stack : stacks) { - stack.peek(); + assertThrows(Exception.class, (ThrowingRunnable) stack.peek()); } } @Test public void testPush() { - for (Stack stack : stacks) { + for (Stack stack : stacks) { stack.push(2); assertEquals(stack.size(), 1); } @@ -49,7 +50,7 @@ public void testPush() { @Test public void testPeek() { - for (Stack stack : stacks) { + for (Stack stack : stacks) { stack.push(2); assertEquals(2, (int) (Integer) stack.peek()); assertEquals(stack.size(), 1); @@ -58,7 +59,7 @@ public void testPeek() { @Test public void testPop() { - for (Stack stack : stacks) { + for (Stack stack : stacks) { stack.push(2); assertEquals(2, (int) stack.pop()); assertEquals(stack.size(), 0); @@ -67,10 +68,10 @@ public void testPop() { @Test public void testExhaustively() { - for (Stack stack : stacks) { + for (Stack stack : stacks) { assertTrue(stack.isEmpty()); stack.push(1); - assertTrue(!stack.isEmpty()); + assertFalse(stack.isEmpty()); stack.push(2); assertEquals(stack.size(), 2); assertEquals(2, (int) stack.peek()); From 3b90001dd3dfb232e2cf2c16f73d95bab0b88db5 Mon Sep 17 00:00:00 2001 From: ttskym Date: Sun, 7 Jun 2020 03:46:37 +0800 Subject: [PATCH 06/11] fix IntQueue bugs and re-implement IntQueue, add ArrayQueue --- .../datastructures/queue/ArrayQueue.java | 63 +++++++++++++++ .../datastructures/queue/IntQueue.java | 43 ++++++---- .../datastructures/queue/LinkedQueue.java | 53 ++++++++++++ .../datastructures/queue/Queue.java | 56 +++---------- .../datastructures/queue/QueueTest.java | 80 +++++++++++-------- .../datastructures/stack/StackTest.java | 5 +- 6 files changed, 202 insertions(+), 98 deletions(-) create mode 100644 src/main/java/com/williamfiset/algorithms/datastructures/queue/ArrayQueue.java create mode 100644 src/main/java/com/williamfiset/algorithms/datastructures/queue/LinkedQueue.java diff --git a/src/main/java/com/williamfiset/algorithms/datastructures/queue/ArrayQueue.java b/src/main/java/com/williamfiset/algorithms/datastructures/queue/ArrayQueue.java new file mode 100644 index 000000000..a41dcb1d8 --- /dev/null +++ b/src/main/java/com/williamfiset/algorithms/datastructures/queue/ArrayQueue.java @@ -0,0 +1,63 @@ +package com.williamfiset.algorithms.datastructures.queue; + +/** + * Besides the Generics, the loss of property of size is another difference between ArrayQueue and + * IntQueue. The size of ArrayQueue is calculated by the formula, as are empty status and full + * status. + * + * @author liujingkun, liujkon@gmail.com + */ +public class ArrayQueue implements Queue { + private Object[] data; + private int front; + private int rear; + + public ArrayQueue(int capacity) { + data = new Object[capacity + 1]; + front = 0; + rear = 0; + } + + @Override + public void offer(T elem) { + if (isFull()) { + throw new RuntimeException("Queue is full"); + } + data[rear++] = elem; + rear = rear % data.length; + } + + @Override + @SuppressWarnings("unchecked") + public T poll() { + if (isEmpty()) { + throw new RuntimeException("Queue is empty"); + } + front = front % data.length; + return (T) data[front++]; + } + + @Override + @SuppressWarnings("unchecked") + public T peek() { + if (isEmpty()) { + throw new RuntimeException("Queue is empty"); + } + front = front % data.length; + return (T) data[front]; + } + + @Override + public int size() { + return (rear + data.length - front) % data.length; + } + + @Override + public boolean isEmpty() { + return rear == front; + } + + public boolean isFull() { + return (front + data.length - rear) % data.length == 1; + } +} diff --git a/src/main/java/com/williamfiset/algorithms/datastructures/queue/IntQueue.java b/src/main/java/com/williamfiset/algorithms/datastructures/queue/IntQueue.java index 270b98131..e2938291e 100644 --- a/src/main/java/com/williamfiset/algorithms/datastructures/queue/IntQueue.java +++ b/src/main/java/com/williamfiset/algorithms/datastructures/queue/IntQueue.java @@ -5,50 +5,59 @@ * is you need to know an upper bound on the number of elements that will be inside the queue at any * given time for this queue to work. * - * @author William Fiset, william.alexandre.fiset@gmail.com + * @author William Fiset, william.alexandre.fiset@gmail.com, liujingkun, liujkon@gmail.com */ package com.williamfiset.algorithms.datastructures.queue; public class IntQueue { - private int[] ar; - private int front, end, sz; + private int[] data; + private int front, end; + private int size; // maxSize is the maximum number of items // that can be in the queue at any given time public IntQueue(int maxSize) { - front = end = 0; - sz = maxSize + 1; - ar = new int[sz]; + front = end = size = 0; + data = new int[maxSize]; } // Return true/false on whether the queue is empty public boolean isEmpty() { - return front == end; + return size == 0; } // Return the number of elements inside the queue public int size() { - if (front > end) return (end + sz - front); - return end - front; + return size; } public int peek() { - return ar[front]; + if (size == 0) { + throw new RuntimeException("Queue is empty"); + } + front = front % data.length; + return data[front]; } // Add an element to the queue public void enqueue(int value) { - ar[end] = value; - if (++end == sz) end = 0; - if (end == front) throw new RuntimeException("Queue too small!"); + if (size == data.length) { + throw new RuntimeException("Queue too small!"); + } + data[end++] = value; + size++; + end = end % data.length; } // Make sure you check is the queue is not empty before calling dequeue! public int dequeue() { - int ret_val = ar[front]; - if (++front == sz) front = 0; - return ret_val; + if (size == 0) { + throw new RuntimeException("Queue is empty"); + } + size--; + front = front % data.length; + return data[front++]; } // Example usage @@ -80,7 +89,7 @@ public static void main(String[] args) { System.out.println(q.isEmpty()); // true - benchMarkTest(); + // benchMarkTest(); } // BenchMark IntQueue vs ArrayDeque. diff --git a/src/main/java/com/williamfiset/algorithms/datastructures/queue/LinkedQueue.java b/src/main/java/com/williamfiset/algorithms/datastructures/queue/LinkedQueue.java new file mode 100644 index 000000000..14830d203 --- /dev/null +++ b/src/main/java/com/williamfiset/algorithms/datastructures/queue/LinkedQueue.java @@ -0,0 +1,53 @@ +/** + * A simple queue implementation with a linkedlist + * + * @author William Fiset, william.alexandre.fiset@gmail.com + */ +package com.williamfiset.algorithms.datastructures.queue; + +public class LinkedQueue implements Iterable, Queue { + + private java.util.LinkedList list = new java.util.LinkedList(); + + public LinkedQueue() {} + + public LinkedQueue(T firstElem) { + offer(firstElem); + } + + // Return the size of the queue + public int size() { + return list.size(); + } + + // Returns whether or not the queue is empty + public boolean isEmpty() { + return size() == 0; + } + + // Peek the element at the front of the queue + // The method throws an error is the queue is empty + public T peek() { + if (isEmpty()) throw new RuntimeException("Queue Empty"); + return list.peekFirst(); + } + + // Poll an element from the front of the queue + // The method throws an error is the queue is empty + public T poll() { + if (isEmpty()) throw new RuntimeException("Queue Empty"); + return list.removeFirst(); + } + + // Add an element to the back of the queue + public void offer(T elem) { + list.addLast(elem); + } + + // Return an iterator to alow the user to traverse + // through the elements found inside the queue + @Override + public java.util.Iterator iterator() { + return list.iterator(); + } +} diff --git a/src/main/java/com/williamfiset/algorithms/datastructures/queue/Queue.java b/src/main/java/com/williamfiset/algorithms/datastructures/queue/Queue.java index 09813b4c9..8ab15b8dc 100644 --- a/src/main/java/com/williamfiset/algorithms/datastructures/queue/Queue.java +++ b/src/main/java/com/williamfiset/algorithms/datastructures/queue/Queue.java @@ -1,53 +1,17 @@ -/** - * A simple queue implementation with a linkedlist - * - * @author William Fiset, william.alexandre.fiset@gmail.com - */ package com.williamfiset.algorithms.datastructures.queue; -public class Queue implements Iterable { - - private java.util.LinkedList list = new java.util.LinkedList(); - - public Queue() {} - - public Queue(T firstElem) { - offer(firstElem); - } - - // Return the size of the queue - public int size() { - return list.size(); - } - - // Returns whether or not the queue is empty - public boolean isEmpty() { - return size() == 0; - } +/** + * @author liujingkun, liujkon@gmail.com + * @param the type of element held int the queue + */ +public interface Queue { + public void offer(T elem); - // Peek the element at the front of the queue - // The method throws an error is the queue is empty - public T peek() { - if (isEmpty()) throw new RuntimeException("Queue Empty"); - return list.peekFirst(); - } + public T poll(); - // Poll an element from the front of the queue - // The method throws an error is the queue is empty - public T poll() { - if (isEmpty()) throw new RuntimeException("Queue Empty"); - return list.removeFirst(); - } + public T peek(); - // Add an element to the back of the queue - public void offer(T elem) { - list.addLast(elem); - } + public int size(); - // Return an iterator to alow the user to traverse - // through the elements found inside the queue - @Override - public java.util.Iterator iterator() { - return list.iterator(); - } + public boolean isEmpty(); } diff --git a/src/test/java/com/williamfiset/algorithms/datastructures/queue/QueueTest.java b/src/test/java/com/williamfiset/algorithms/datastructures/queue/QueueTest.java index a593d5920..058c965d3 100644 --- a/src/test/java/com/williamfiset/algorithms/datastructures/queue/QueueTest.java +++ b/src/test/java/com/williamfiset/algorithms/datastructures/queue/QueueTest.java @@ -1,71 +1,87 @@ package com.williamfiset.algorithms.datastructures.queue; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; +import java.util.ArrayList; +import java.util.List; import org.junit.Before; import org.junit.Test; public class QueueTest { - Queue queue; + private List> queues = new ArrayList<>(); @Before public void setup() { - queue = new Queue(); + queues.add(new ArrayQueue(2)); + queues.add(new LinkedQueue()); } @Test public void testEmptyQueue() { - assertTrue(queue.isEmpty()); - assertEquals(queue.size(), 0); + for (Queue queue : queues) { + assertTrue(queue.isEmpty()); + assertEquals(queue.size(), 0); + } } - @Test(expected = Exception.class) + @Test public void testPollOnEmpty() { - queue.poll(); + for (Queue queue : queues) { + assertThrows(Exception.class, queue::poll); + } } - @Test(expected = Exception.class) + @Test public void testPeekOnEmpty() { - queue.peek(); + for (Queue queue : queues) { + assertThrows(Exception.class, queue::peek); + } } @Test public void testOffer() { - queue.offer(2); - assertEquals(queue.size(), 1); + for (Queue queue : queues) { + queue.offer(2); + assertEquals(queue.size(), 1); + } } @Test public void testPeek() { - queue.offer(2); - assertTrue(queue.peek() == 2); - assertEquals(queue.size(), 1); + for (Queue queue : queues) { + queue.offer(2); + assertEquals(2, (int) queue.peek()); + assertEquals(queue.size(), 1); + } } @Test public void testPoll() { - queue.offer(2); - assertTrue(queue.poll() == 2); - assertEquals(queue.size(), 0); + for (Queue queue : queues) { + queue.offer(2); + assertEquals(2, (int) queue.poll()); + assertEquals(queue.size(), 0); + } } @Test public void testExhaustively() { - assertTrue(queue.isEmpty()); - queue.offer(1); - assertTrue(!queue.isEmpty()); - queue.offer(2); - assertEquals(queue.size(), 2); - assertTrue(queue.peek() == 1); - assertEquals(queue.size(), 2); - assertTrue(queue.poll() == 1); - assertEquals(queue.size(), 1); - assertTrue(queue.peek() == 2); - assertEquals(queue.size(), 1); - assertTrue(queue.poll() == 2); - assertEquals(queue.size(), 0); - assertTrue(queue.isEmpty()); + for (Queue queue : queues) { + assertTrue(queue.isEmpty()); + queue.offer(1); + assertFalse(queue.isEmpty()); + queue.offer(2); + assertEquals(queue.size(), 2); + assertEquals(1, (int) queue.peek()); + assertEquals(queue.size(), 2); + assertEquals(1, (int) queue.poll()); + assertEquals(queue.size(), 1); + assertEquals(2, (int) queue.peek()); + assertEquals(queue.size(), 1); + assertEquals(2, (int) queue.poll()); + assertEquals(queue.size(), 0); + assertTrue(queue.isEmpty()); + } } } diff --git a/src/test/java/com/williamfiset/algorithms/datastructures/stack/StackTest.java b/src/test/java/com/williamfiset/algorithms/datastructures/stack/StackTest.java index 593762c8e..d58687196 100644 --- a/src/test/java/com/williamfiset/algorithms/datastructures/stack/StackTest.java +++ b/src/test/java/com/williamfiset/algorithms/datastructures/stack/StackTest.java @@ -6,7 +6,6 @@ import java.util.List; import org.junit.Before; import org.junit.Test; -import org.junit.function.ThrowingRunnable; public class StackTest { @@ -33,10 +32,10 @@ public void testPopOnEmpty() { } } - @Test(expected = Exception.class) + @Test public void testPeekOnEmpty() { for (Stack stack : stacks) { - assertThrows(Exception.class, (ThrowingRunnable) stack.peek()); + assertThrows(Exception.class, stack::peek); } } From c1245e65ec2ea4607c29bf0286e1b8c59b2299d4 Mon Sep 17 00:00:00 2001 From: ttskym Date: Sun, 7 Jun 2020 04:38:48 +0800 Subject: [PATCH 07/11] add comment for ArrayQueue --- .../algorithms/datastructures/queue/ArrayQueue.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/com/williamfiset/algorithms/datastructures/queue/ArrayQueue.java b/src/main/java/com/williamfiset/algorithms/datastructures/queue/ArrayQueue.java index a41dcb1d8..05d7bcfc7 100644 --- a/src/main/java/com/williamfiset/algorithms/datastructures/queue/ArrayQueue.java +++ b/src/main/java/com/williamfiset/algorithms/datastructures/queue/ArrayQueue.java @@ -5,6 +5,11 @@ * IntQueue. The size of ArrayQueue is calculated by the formula, as are empty status and full * status. * + *

ArrayQueue maximum size is data.length - 1. The place of the variant rear is always in font of + * the variant front logistically if regard the data array as circular array. so the number of + * status of rear and front is the length of data array. And one status is used to be judge if the + * queue is empty or full. + * * @author liujingkun, liujkon@gmail.com */ public class ArrayQueue implements Queue { @@ -13,6 +18,7 @@ public class ArrayQueue implements Queue { private int rear; public ArrayQueue(int capacity) { + // ArrayQueue maximum size is data.length - 1. data = new Object[capacity + 1]; front = 0; rear = 0; From b418d44d94245f0bd9a86c2b75425a0e854a723f Mon Sep 17 00:00:00 2001 From: ttskym Date: Sun, 7 Jun 2020 04:45:30 +0800 Subject: [PATCH 08/11] modify the comment --- .../algorithms/datastructures/queue/ArrayQueue.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/williamfiset/algorithms/datastructures/queue/ArrayQueue.java b/src/main/java/com/williamfiset/algorithms/datastructures/queue/ArrayQueue.java index 05d7bcfc7..8e9fd0832 100644 --- a/src/main/java/com/williamfiset/algorithms/datastructures/queue/ArrayQueue.java +++ b/src/main/java/com/williamfiset/algorithms/datastructures/queue/ArrayQueue.java @@ -5,10 +5,10 @@ * IntQueue. The size of ArrayQueue is calculated by the formula, as are empty status and full * status. * - *

ArrayQueue maximum size is data.length - 1. The place of the variant rear is always in font of - * the variant front logistically if regard the data array as circular array. so the number of - * status of rear and front is the length of data array. And one status is used to be judge if the - * queue is empty or full. + *

ArrayQueue maximum size is data.length - 1. The place of the variable rear is always in front + * of the variable front logistically if regard the data array as circular. so the number of states + * of the combination of rear and front is the length of the data array. And one of the total states + * is used to be the judge if the queue is empty or full. * * @author liujingkun, liujkon@gmail.com */ From 2ac70feed1390d4b6949545dfb2360d7b51f6a0f Mon Sep 17 00:00:00 2001 From: ttskym Date: Wed, 10 Jun 2020 05:27:26 +0800 Subject: [PATCH 09/11] fix the mod problem --- .../datastructures/queue/ArrayQueue.java | 4 +- .../datastructures/queue/IntQueue.java | 57 +++++++++++-------- .../datastructures/stack/IntStack.java | 11 ++-- .../datastructures/queue/IntQueueTest.java | 42 +++++++------- .../datastructures/queue/QueueTest.java | 1 + .../datastructures/stack/StackTest.java | 1 + 6 files changed, 64 insertions(+), 52 deletions(-) diff --git a/src/main/java/com/williamfiset/algorithms/datastructures/queue/ArrayQueue.java b/src/main/java/com/williamfiset/algorithms/datastructures/queue/ArrayQueue.java index 8e9fd0832..8f99b9083 100644 --- a/src/main/java/com/williamfiset/algorithms/datastructures/queue/ArrayQueue.java +++ b/src/main/java/com/williamfiset/algorithms/datastructures/queue/ArrayQueue.java @@ -30,7 +30,7 @@ public void offer(T elem) { throw new RuntimeException("Queue is full"); } data[rear++] = elem; - rear = rear % data.length; + rear = rear >= data.length ? rear - data.length : rear; } @Override @@ -55,7 +55,7 @@ public T peek() { @Override public int size() { - return (rear + data.length - front) % data.length; + return (rear + data.length - front) >= data.length ? rear - front : rear + data.length - front; } @Override diff --git a/src/main/java/com/williamfiset/algorithms/datastructures/queue/IntQueue.java b/src/main/java/com/williamfiset/algorithms/datastructures/queue/IntQueue.java index e2938291e..956fe39e7 100644 --- a/src/main/java/com/williamfiset/algorithms/datastructures/queue/IntQueue.java +++ b/src/main/java/com/williamfiset/algorithms/datastructures/queue/IntQueue.java @@ -9,7 +9,7 @@ */ package com.williamfiset.algorithms.datastructures.queue; -public class IntQueue { +public class IntQueue implements Queue { private int[] data; private int front, end; @@ -32,17 +32,23 @@ public int size() { return size; } - public int peek() { - if (size == 0) { + @Override + public Integer peek() { + if (isEmpty()) { throw new RuntimeException("Queue is empty"); } front = front % data.length; return data[front]; } + public boolean isFull() { + return size == data.length; + } + // Add an element to the queue - public void enqueue(int value) { - if (size == data.length) { + @Override + public void offer(Integer value) { + if (isFull()) { throw new RuntimeException("Queue too small!"); } data[end++] = value; @@ -50,8 +56,9 @@ public void enqueue(int value) { end = end % data.length; } - // Make sure you check is the queue is not empty before calling dequeue! - public int dequeue() { + // Make sure you check is the queue is not empty before calling poll! + @Override + public Integer poll() { if (size == 0) { throw new RuntimeException("Queue is empty"); } @@ -65,27 +72,27 @@ public static void main(String[] args) { IntQueue q = new IntQueue(5); - q.enqueue(1); - q.enqueue(2); - q.enqueue(3); - q.enqueue(4); - q.enqueue(5); + q.offer(1); + q.offer(2); + q.offer(3); + q.offer(4); + q.offer(5); - System.out.println(q.dequeue()); // 1 - System.out.println(q.dequeue()); // 2 - System.out.println(q.dequeue()); // 3 - System.out.println(q.dequeue()); // 4 + System.out.println(q.poll()); // 1 + System.out.println(q.poll()); // 2 + System.out.println(q.poll()); // 3 + System.out.println(q.poll()); // 4 System.out.println(q.isEmpty()); // false - q.enqueue(1); - q.enqueue(2); - q.enqueue(3); + q.offer(1); + q.offer(2); + q.offer(3); - System.out.println(q.dequeue()); // 5 - System.out.println(q.dequeue()); // 1 - System.out.println(q.dequeue()); // 2 - System.out.println(q.dequeue()); // 3 + System.out.println(q.poll()); // 5 + System.out.println(q.poll()); // 1 + System.out.println(q.poll()); // 2 + System.out.println(q.poll()); // 3 System.out.println(q.isEmpty()); // true @@ -100,8 +107,8 @@ private static void benchMarkTest() { // IntQueue times at around 0.0324 seconds long start = System.nanoTime(); - for (int i = 0; i < n; i++) intQ.enqueue(i); - for (int i = 0; i < n; i++) intQ.dequeue(); + for (int i = 0; i < n; i++) intQ.offer(i); + for (int i = 0; i < n; i++) intQ.poll(); long end = System.nanoTime(); System.out.println("IntQueue Time: " + (end - start) / 1e9); diff --git a/src/main/java/com/williamfiset/algorithms/datastructures/stack/IntStack.java b/src/main/java/com/williamfiset/algorithms/datastructures/stack/IntStack.java index bbd99d698..42c661c6a 100644 --- a/src/main/java/com/williamfiset/algorithms/datastructures/stack/IntStack.java +++ b/src/main/java/com/williamfiset/algorithms/datastructures/stack/IntStack.java @@ -9,7 +9,7 @@ */ package com.williamfiset.algorithms.datastructures.stack; -public class IntStack { +public class IntStack implements Stack { private int[] ar; private int pos = 0; @@ -31,17 +31,20 @@ public boolean isEmpty() { } // Returns the element at the top of the stack - public int peek() { + @Override + public Integer peek() { return ar[pos - 1]; } // Add an element to the top of the stack - public void push(int value) { + @Override + public void push(Integer value) { ar[pos++] = value; } // Make sure you check that the stack is not empty before calling pop! - public int pop() { + @Override + public Integer pop() { return ar[--pos]; } diff --git a/src/test/java/com/williamfiset/algorithms/datastructures/queue/IntQueueTest.java b/src/test/java/com/williamfiset/algorithms/datastructures/queue/IntQueueTest.java index d1eeb2f5a..4aba85054 100644 --- a/src/test/java/com/williamfiset/algorithms/datastructures/queue/IntQueueTest.java +++ b/src/test/java/com/williamfiset/algorithms/datastructures/queue/IntQueueTest.java @@ -22,7 +22,7 @@ public void testEmptyQueue() { // @Test(expected=Exception.class) // public void testPollOnEmpty() { // IntQueue queue = new IntQueue(0); - // queue.dequeue(); + // queue.poll(); // } // Doesn't apply to this implementation because of wrap @@ -33,9 +33,9 @@ public void testEmptyQueue() { // } @Test - public void testEnqueueOneElement() { + public void testofferOneElement() { IntQueue queue = new IntQueue(1); - queue.enqueue(77); + queue.offer(77); assertEquals(queue.size(), 1); } @@ -45,45 +45,45 @@ public void testAll() { IntQueue queue = new IntQueue(10); assertTrue(queue.isEmpty()); for (int i = 1; i <= n; i++) { - queue.enqueue(i); + queue.offer(i); assertFalse(queue.isEmpty()); } for (int i = 1; i <= n; i++) { - assertEquals(i, queue.peek()); - assertEquals(i, queue.dequeue()); + assertEquals(i, (int) queue.peek()); + assertEquals(i, (int) queue.poll()); assertEquals(queue.size(), n - i); } assertTrue(queue.isEmpty()); n = 8; for (int i = 1; i <= n; i++) { - queue.enqueue(i); + queue.offer(i); assertFalse(queue.isEmpty()); } for (int i = 1; i <= n; i++) { - assertEquals(i, queue.peek()); - assertEquals(i, queue.dequeue()); + assertEquals(i, (int) queue.peek()); + assertEquals(i, (int) queue.poll()); assertEquals(queue.size(), n - i); } assertTrue(queue.isEmpty()); n = 9; for (int i = 1; i <= n; i++) { - queue.enqueue(i); + queue.offer(i); assertFalse(queue.isEmpty()); } for (int i = 1; i <= n; i++) { - assertEquals(i, queue.peek()); - assertEquals(i, queue.dequeue()); + assertEquals(i, (int) queue.peek()); + assertEquals(i, (int) queue.poll()); assertEquals(queue.size(), n - i); } assertTrue(queue.isEmpty()); n = 10; for (int i = 1; i <= n; i++) { - queue.enqueue(i); + queue.offer(i); assertFalse(queue.isEmpty()); } for (int i = 1; i <= n; i++) { - assertEquals(i, queue.peek()); - assertEquals(i, queue.dequeue()); + assertEquals(i, (int) queue.peek()); + assertEquals(i, (int) queue.poll()); assertEquals(queue.size(), n - i); } assertTrue(queue.isEmpty()); @@ -92,16 +92,16 @@ public void testAll() { @Test public void testPeekOneElement() { IntQueue queue = new IntQueue(1); - queue.enqueue(77); + queue.offer(77); assertTrue(queue.peek() == 77); assertEquals(queue.size(), 1); } @Test - public void testDequeueOneElement() { + public void testpollOneElement() { IntQueue queue = new IntQueue(1); - queue.enqueue(77); - assertTrue(queue.dequeue() == 77); + queue.offer(77); + assertTrue(queue.poll() == 77); assertEquals(queue.size(), 0); } @@ -124,11 +124,11 @@ public void testRandom() { int elem = (int) (1000 * Math.random()); if (javaQ.size() < qSize) { javaQ.offer(elem); - intQ.enqueue(elem); + intQ.offer(elem); } } else { if (!javaQ.isEmpty()) { - assertEquals((int) javaQ.poll(), (int) intQ.dequeue()); + assertEquals((int) javaQ.poll(), (int) intQ.poll()); } } diff --git a/src/test/java/com/williamfiset/algorithms/datastructures/queue/QueueTest.java b/src/test/java/com/williamfiset/algorithms/datastructures/queue/QueueTest.java index 058c965d3..531d3c5e0 100644 --- a/src/test/java/com/williamfiset/algorithms/datastructures/queue/QueueTest.java +++ b/src/test/java/com/williamfiset/algorithms/datastructures/queue/QueueTest.java @@ -15,6 +15,7 @@ public class QueueTest { public void setup() { queues.add(new ArrayQueue(2)); queues.add(new LinkedQueue()); + queues.add(new IntQueue(2)); } @Test diff --git a/src/test/java/com/williamfiset/algorithms/datastructures/stack/StackTest.java b/src/test/java/com/williamfiset/algorithms/datastructures/stack/StackTest.java index d58687196..486a4138a 100644 --- a/src/test/java/com/williamfiset/algorithms/datastructures/stack/StackTest.java +++ b/src/test/java/com/williamfiset/algorithms/datastructures/stack/StackTest.java @@ -15,6 +15,7 @@ public class StackTest { public void setup() { stacks.add(new ListStack()); stacks.add(new ArrayStack()); + stacks.add(new IntStack(2)); } @Test From ac5b2edec5ec63778152f6bbb0e43332c1d8c012 Mon Sep 17 00:00:00 2001 From: ttskym Date: Wed, 10 Jun 2020 05:43:25 +0800 Subject: [PATCH 10/11] fix the mod problem --- .../algorithms/datastructures/queue/ArrayQueue.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/williamfiset/algorithms/datastructures/queue/ArrayQueue.java b/src/main/java/com/williamfiset/algorithms/datastructures/queue/ArrayQueue.java index 8f99b9083..82be4bd4c 100644 --- a/src/main/java/com/williamfiset/algorithms/datastructures/queue/ArrayQueue.java +++ b/src/main/java/com/williamfiset/algorithms/datastructures/queue/ArrayQueue.java @@ -30,7 +30,7 @@ public void offer(T elem) { throw new RuntimeException("Queue is full"); } data[rear++] = elem; - rear = rear >= data.length ? rear - data.length : rear; + rear = adjustIndex(rear, data.length); } @Override @@ -39,7 +39,7 @@ public T poll() { if (isEmpty()) { throw new RuntimeException("Queue is empty"); } - front = front % data.length; + front = adjustIndex(front, data.length); return (T) data[front++]; } @@ -49,13 +49,13 @@ public T peek() { if (isEmpty()) { throw new RuntimeException("Queue is empty"); } - front = front % data.length; + front = adjustIndex(front, data.length); return (T) data[front]; } @Override public int size() { - return (rear + data.length - front) >= data.length ? rear - front : rear + data.length - front; + return adjustIndex(rear + data.length - front, data.length); } @Override @@ -66,4 +66,8 @@ public boolean isEmpty() { public boolean isFull() { return (front + data.length - rear) % data.length == 1; } + + private int adjustIndex(int index, int size) { + return index >= size ? index - size : index; + } } From 64f169b27215f9d50d156e2cb63a8560e2f83e8a Mon Sep 17 00:00:00 2001 From: ttskym Date: Fri, 26 Jun 2020 00:02:48 +0800 Subject: [PATCH 11/11] update BST --- .../binarysearchtree/BinarySearchTree.java | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/williamfiset/algorithms/datastructures/binarysearchtree/BinarySearchTree.java b/src/main/java/com/williamfiset/algorithms/datastructures/binarysearchtree/BinarySearchTree.java index 23ff3755c..d48e7f1a6 100644 --- a/src/main/java/com/williamfiset/algorithms/datastructures/binarysearchtree/BinarySearchTree.java +++ b/src/main/java/com/williamfiset/algorithms/datastructures/binarysearchtree/BinarySearchTree.java @@ -112,25 +112,14 @@ private Node remove(Node node, T elem) { // no subtree at all. In this situation just // swap the node we wish to remove with its right child. if (node.left == null) { - - Node rightChild = node.right; - - node.data = null; - node = null; - - return rightChild; + return node.right; // This is the case with only a left subtree or // no subtree at all. In this situation just // swap the node we wish to remove with its left child. } else if (node.right == null) { - Node leftChild = node.left; - - node.data = null; - node = null; - - return leftChild; + return node.left; // When removing a node from a binary tree with two links the // successor of the node being removed can either be the largest