Skip to content

Commit 2f73ff7

Browse files
authored
Merge pull request #27 from yeoshuheng/stackq
Stack & Queue
2 parents 7834f3a + 2879f39 commit 2f73ff7

File tree

11 files changed

+209
-10
lines changed

11 files changed

+209
-10
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ This repository contains implementation of some of the fundamental data structur
4444

4545
## Short-cut to CS2040S Material
4646
1. Basic structures
47-
* Linked List
48-
* Stack
49-
* Queue
47+
* [Linked List](src/dataStructures/linkedList)
48+
* [Stack](src/dataStructures/stack)
49+
* [Queue](src/dataStructures/queue)
5050
2. Binary Search
5151
* Peak Finding
5252
3. Sorting
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package src.dataStructures.stackAndQueue.monotonicQueue;
1+
package src.dataStructures.queue;
22

33
import java.util.Deque;
44
import java.util.ArrayDeque;
@@ -11,7 +11,14 @@
1111
* max()
1212
* pop()
1313
* push()
14-
* @param <T> generic type for objects to be stored or queried
14+
* @param <T> generic type for objects to be stored or queried
15+
*
16+
* index v Increasing queue Decreasing queue
17+
* 1 5 [5] [5]
18+
* 2 3 [3] 3 kick out 5 [5, 3] #3->5
19+
* 3 1 [1] 1 kick out 3 [5, 3, 1] #1->3
20+
* 4 2 [1, 2] #2->1 [5, 3, 2] 2 kick out 1 #2->3
21+
* 5 4 [1, 2, 4] #4->2 [5,4] 4 kick out 2, 3 #4->2
1522
*/
1623
public class MonotonicQueue<T extends Comparable<T>> {
1724
private Deque<Pair<T>> dq = new ArrayDeque<>(); // or LinkedList
@@ -54,6 +61,9 @@ public T max() {
5461
* Removal will only be done all representation of the object has been accounted for.
5562
*/
5663
public T pop() {
64+
if (dq.isEmpty()) {
65+
return null;
66+
}
5767
Pair<T> node = dq.peek();
5868
if (node.countDeleted > 0) {
5969
node.countDeleted -= 1;

src/dataStructures/stackAndQueue/queue/Queue.java renamed to src/dataStructures/queue/Queue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package src.dataStructures.stackAndQueue.queue;
1+
package src.dataStructures.queue;
22

33
/**
44
* Implementation of a queue structure using LinkedList.

src/dataStructures/queue/README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Queue
2+
3+
A queue is a linear data structure that restricts the order in which operations can be performed on its elements.
4+
5+
### Operation Orders
6+
7+
![Queue](https://media.geeksforgeeks.org/wp-content/cdn-uploads/20221213113312/Queue-Data-Structures.png)
8+
9+
*Source: GeeksForGeeks*
10+
11+
Queue follows a FIFO, first in first out order.
12+
This means the earliest element
13+
added to the stack is the one operations are conducted on first.
14+
15+
A [stack](../stack/README.md) is a queue with operations conducted in an opposite manner.
16+
17+
## Analysis
18+
19+
As a queue only interacts with either the first or last element regardless during its operations,
20+
it only needs to keep the pointers of the two element at hand, which is constantly updated as more
21+
elements are removed / added. This allows stack operations to only incur a *O(1)* time complexity.
22+
23+
## Notes
24+
25+
### Stack vs Queues
26+
27+
Stack and queues only differ in terms of operation order, you should aim to use a stack when
28+
you want the most recent elements to be operated on.
29+
Some situations where a stack would work well include build redo / undo systems and backtracking problems.
30+
31+
On the other hand, a queue allows you to operate on elements that enter first. Some situations where
32+
this would be useful include Breadth First Search algorithms and task / resource allocation systems.
33+
34+
### Arrays vs Linked List
35+
It is worth noting that queues can be implemented with either a array or with a [linked list](../linkedList/README.md).
36+
In the context of ordered operations, the lookup is only restricted to the first element.
37+
38+
Hence, there is not much advantage in using a array, which only has a better lookup speed (*O(1)* time complexity)
39+
to implement a queue. Especially when using a linked list to construct your queue
40+
would allow you to grow or shrink the queue as you wish.
41+
42+
### Queue Variants
43+
44+
These are some variants of queue that are commonly used.
45+
46+
#### Double Ended Queue (Deque)
47+
48+
![Deque](https://media.geeksforgeeks.org/wp-content/uploads/anod.png)
49+
50+
*Source: GeeksForGeeks*
51+
52+
Deque is a variant of queue where elements can be removed or added from the head and tail of the queue.
53+
Deque could come in handy when trying to solve sliding window problems.
54+
55+
A deque can be implemented in multiple ways, using doubly linked lists, arrays or two stacks.
56+
57+
#### Monotonic Queue
58+
59+
This is a variant of queue where elements within the queue are either strictly increasing or decreasing.
60+
Monotonic queues are often implemented with a deque.
61+
62+
Within a increasing monotonic queue, any element that is smaller than the current minimum is removed.
63+
Within a decreasing monotonic queue, any element that is larger than the current maximum is removed.
64+
65+
It is worth mentioning that the most elements added to the monotonic queue would always be in a
66+
increasing / decreasing order,
67+
hence, we only need to compare down the monotonic queue from the back when adding new elements.
68+

src/dataStructures/stack/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Stack
2+
Stack is a linear data structure that restricts
3+
the order in which operations can be performed on its elements.
4+
5+
![Stack data structure](https://cdn.programiz.com/sites/tutorial2program/files/stack.png)
6+
7+
*Source: Programiz*
8+
9+
### Operation Orders
10+
11+
Stack follows a LIFO, last in first out order.
12+
This means the most recent element
13+
added to the stack is the one operations are conducted on first.
14+
15+
A [queue](../queue/README.md) conducts operations in the opposite order.
16+
17+
## Analysis
18+
19+
As a stack only interacts with the most recent element regardless of operation,
20+
it keeps the pointer of the most recent element at hand, which is constantly updated as more
21+
elements are removed / added. This allows stack operations to only incur a *O(1)* time complexity.
22+
23+
## Notes
24+
25+
### Stack vs Queues
26+
27+
Stack and queues only differ in terms of operation order, you should aim to use a stack when
28+
you want the most recent elements to be operated on.
29+
Some situations where a stack would work well include build redo / undo systems and backtracking problems.
30+
31+
On the other hand, a queue allows you to operate on elements that enter first. Some situations where
32+
this would be useful include Breadth First Search algorithms and task / resource allocation systems.
33+
34+
### Arrays vs Linked List
35+
It is worth noting that stacks can be implemented with either a array or with a [linked list](../linkedList/README.md).
36+
In the context of ordered operations, the lookup is only restricted to the first element.
37+
38+
Hence, there is not much advantage in using a array, which only has a better lookup speed (*O(1)* time complexity)
39+
to implement a stack. Especially when using a linked list to construct your stack
40+
would allow you to grow or shrink the stack as you wish.

src/dataStructures/stackAndQueue/stack/Stack.java renamed to src/dataStructures/stack/Stack.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package src.dataStructures.stackAndQueue.stack;
1+
package src.dataStructures.stack;
22

33
import java.util.List;
44
import java.util.ArrayList;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package test.dataStructures.queue;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import src.dataStructures.queue.Queue;
6+
7+
public class queueTest {
8+
@Test
9+
public void testEmptyQueue() {
10+
Queue<Integer> q = new Queue<>();
11+
Assert.assertEquals(0, q.size());
12+
Assert.assertEquals(true, q.isEmpty());
13+
Assert.assertEquals(null, q.dequeue());
14+
}
15+
@Test
16+
public void testEnqueue() {
17+
Queue<Integer> q = new Queue<>();
18+
q.enqueue(1);
19+
q.enqueue(2);
20+
q.enqueue(3);
21+
Assert.assertEquals(3, q.size());
22+
}
23+
24+
@Test
25+
public void testPeek() {
26+
Queue<Integer> q = new Queue<>();
27+
q.enqueue(1);
28+
Assert.assertEquals("1", q.peek().toString());
29+
q.enqueue(2);
30+
q.enqueue(3);
31+
q.peek();
32+
Assert.assertEquals("1", q.peek().toString());
33+
}
34+
35+
@Test
36+
public void testDequeue() {
37+
Queue<Integer> q = new Queue<>();
38+
q.enqueue(1);
39+
q.enqueue(2);
40+
q.enqueue(3);
41+
Assert.assertEquals("1", q.dequeue().toString());
42+
Assert.assertEquals(2, q.size());
43+
q.dequeue();
44+
Assert.assertEquals(1, q.size());
45+
Assert.assertEquals("3", q.dequeue().toString());
46+
Assert.assertEquals(0, q.size());
47+
}
48+
49+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package test.dataStructures.stack;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import src.dataStructures.stack.Stack;
6+
public class StackTest {
7+
@Test
8+
public void testEmpty(){
9+
Stack<Integer> stk = new Stack<>();
10+
Assert.assertEquals(null, stk.pop());
11+
Assert.assertEquals(null, stk.peek());
12+
}
13+
14+
@Test
15+
public void testPopAndPeek() {
16+
Stack<Integer> stk = new Stack<>(1, 2, 3);
17+
Assert.assertEquals("3", stk.peek().toString());
18+
Assert.assertEquals("3", stk.pop().toString());
19+
Assert.assertEquals("2", stk.peek().toString());
20+
}
21+
22+
@Test
23+
public void testPush() {
24+
Stack<Integer> stk = new Stack<>();
25+
stk.push(1);
26+
Assert.assertEquals("1", stk.peek().toString());
27+
stk.push(2);
28+
stk.push(3);
29+
Assert.assertEquals("3", stk.peek().toString());
30+
}
31+
32+
}

test/randomTests/changxian/stackAndQueue/monotonicQueue/Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package test.randomTests.changxian.stackAndQueue.monotonicQueue;
22

3-
import src.dataStructures.stackAndQueue.monotonicQueue.MonotonicQueue;
3+
import src.dataStructures.queue.MonotonicQueue;
44

55
import java.util.Arrays;
66
import java.util.ArrayList;

test/randomTests/changxian/stackAndQueue/queue/Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package test.randomTests.changxian.stackAndQueue.queue;
22

3-
import src.dataStructures.stackAndQueue.queue.Queue;
3+
import src.dataStructures.queue.Queue;
44

55
/**
66
* Basic Testing

0 commit comments

Comments
 (0)