Skip to content

Commit b30700a

Browse files
author
Deepak Malik
committed
Queue based stack
1 parent 124dc07 commit b30700a

3 files changed

Lines changed: 156 additions & 0 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* Data-Structures-In-Java
3+
* QueueBasedStack.java
4+
*/
5+
package com.deepak.data.structures.Stack;
6+
7+
import java.util.LinkedList;
8+
9+
/**
10+
* Queue based stack implementation
11+
*
12+
* @author Deepak
13+
*
14+
* @param <T>
15+
*/
16+
public class QueueBasedStack<T> {
17+
18+
/* We will use only one queue */
19+
private LinkedList<T> queue = null;
20+
21+
/**
22+
* Default Constructor
23+
*/
24+
public QueueBasedStack() {
25+
queue = new LinkedList<>();
26+
}
27+
28+
/**
29+
* Method to push an item on stack
30+
*
31+
* @param item
32+
*/
33+
public void push(T item) {
34+
queue.add(item);
35+
int size = queue.size();
36+
while (size > 1) {
37+
queue.add(queue.remove());
38+
size--;
39+
}
40+
}
41+
42+
/**
43+
* Method to pop an item from stack
44+
*
45+
* @return {@link T}
46+
*/
47+
public T pop() {
48+
return queue.remove();
49+
}
50+
51+
/**
52+
* Method to peek an item from stack
53+
*
54+
* @return {@link T}
55+
*/
56+
public T peek() {
57+
return queue.peek();
58+
}
59+
60+
/**
61+
* Method to return size of stack
62+
*
63+
* @return {@link int}
64+
*/
65+
public int size() {
66+
return queue.size();
67+
}
68+
69+
/**
70+
* Method to check if stack is empty
71+
*
72+
* @return {@link boolean}
73+
*/
74+
public boolean isEmpty() {
75+
return queue.isEmpty();
76+
}
77+
78+
79+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* Data-Structures-in-Java
3+
* QueueBasedStackTest.java
4+
*/
5+
package com.deepak.data.structures.Stack;
6+
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
10+
/**
11+
* Test cases for queue based stack
12+
*
13+
* @author Deepak
14+
*/
15+
public class QueueBasedStackTest {
16+
17+
/**
18+
* Test case for push feature
19+
*/
20+
@Test
21+
public void testPush() {
22+
QueueBasedStack<Integer> stack = new QueueBasedStack<>();
23+
Assert.assertTrue(stack.isEmpty());
24+
Assert.assertTrue(stack.size() == 0);
25+
stack.push(3);
26+
Assert.assertFalse(stack.isEmpty());
27+
Assert.assertTrue(stack.size() == 1);
28+
stack.push(7);
29+
stack.push(31);
30+
Assert.assertTrue(stack.size() == 3);
31+
}
32+
33+
/**
34+
* Test case for pop feature
35+
*/
36+
@Test
37+
public void testPop() {
38+
QueueBasedStack<Integer> stack = new QueueBasedStack<>();
39+
Assert.assertTrue(stack.isEmpty());
40+
Assert.assertTrue(stack.size() == 0);
41+
stack.push(3);
42+
stack.push(17);
43+
stack.push(35);
44+
stack.push(13);
45+
Assert.assertTrue(stack.size() == 4);
46+
Assert.assertTrue(stack.pop() == 13);
47+
Assert.assertTrue(stack.pop() == 35);
48+
Assert.assertTrue(stack.pop() == 17);
49+
Assert.assertTrue(stack.size() == 1);
50+
}
51+
52+
/**
53+
* Test case for peek feature
54+
*/
55+
@Test
56+
public void testPeek() {
57+
QueueBasedStack<Integer> stack = new QueueBasedStack<>();
58+
Assert.assertTrue(stack.isEmpty());
59+
Assert.assertTrue(stack.size() == 0);
60+
stack.push(3);
61+
stack.push(17);
62+
stack.push(35);
63+
stack.push(13);
64+
Assert.assertTrue(stack.size() == 4);
65+
Assert.assertTrue(stack.peek() == 13);
66+
Assert.assertTrue(stack.size() == 4);
67+
stack.pop();
68+
Assert.assertTrue(stack.size() == 3);
69+
Assert.assertTrue(stack.peek() == 35);
70+
}
71+
72+
}

‎test/com/deepak/data/structures/Stack/SimpleStackTest.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
import org.junit.Assert;
88
import org.junit.Test;
99

10+
/**
11+
* Test cases for simple stack
12+
*
13+
* @author Deepak
14+
*/
1015
public class SimpleStackTest {
1116

1217
/**

0 commit comments

Comments
 (0)