Skip to content

Commit ca09567

Browse files
committed
Simple stack from list
1 parent d6f0bfb commit ca09567

File tree

2 files changed

+74
-21
lines changed

2 files changed

+74
-21
lines changed

src/com/deepak/data/structures/Stack/SimpleStackFromList.java

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Data-Structures-And-Algorithms-in-Java
2+
* Data-Structures-In-Java
33
* SimpleStackFromList.java
44
*/
55
package com.deepak.data.structures.Stack;
@@ -8,37 +8,19 @@
88

99
/**
1010
* Class implementing simple stack based on the list
11+
*
1112
* @author Deepak
1213
*/
1314
public class SimpleStackFromList {
1415

15-
/**
16-
* Sample implementation of stack
17-
* @param args
18-
*/
19-
public static void main(String[] args) {
20-
SimpleStackFromList stack = new SimpleStackFromList();
21-
System.out.println("Creating a fresh Stack");
22-
System.out.println("Size of Stack => " + stack.size());
23-
System.out.println("Is Stack Empty => " + stack.isEmpty());
24-
System.out.println("Inserting two items in Stack.");
25-
stack.push(10);
26-
stack.push(20);
27-
System.out.println("After Insertion, Size of Stack => " + stack.size());
28-
System.out.println("After Insertion, Is Stack Empty => " + stack.isEmpty());
29-
System.out.println("Removing an item from Stack.");
30-
stack.pop();
31-
System.out.println("After Removal, Size of Stack => " + stack.size());
32-
System.out.println("Top element on Stack => " + stack.peek());
33-
}
34-
3516
/**
3617
* Linked list to hold items
3718
*/
3819
private LinkedList<Object> list = new LinkedList<>();
3920

4021
/**
4122
* Method to push a item on top of stack
23+
*
4224
* @param item
4325
*/
4426
public void push(Object item) {
@@ -47,6 +29,7 @@ public void push(Object item) {
4729

4830
/**
4931
* Method to remove a item from top of stack
32+
*
5033
* @return {@link Object}
5134
*/
5235
public Object pop() {
@@ -55,6 +38,7 @@ public Object pop() {
5538

5639
/**
5740
* Method to look up top item in stack
41+
*
5842
* @return {@link Object}
5943
*/
6044
public Object peek() {
@@ -63,6 +47,7 @@ public Object peek() {
6347

6448
/**
6549
* Method to check size of the stack
50+
*
6651
* @return {@link int}
6752
*/
6853
public int size() {
@@ -71,6 +56,7 @@ public int size() {
7156

7257
/**
7358
* Method to check if stack is empty
59+
*
7460
* @return {@link boolean}
7561
*/
7662
public boolean isEmpty() {
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Data-Structures-in-Java
3+
* SimpleStackTest.java
4+
*/
5+
package com.deepak.data.structures.Stack;
6+
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
10+
public class SimpleStackTest {
11+
12+
/**
13+
* Test case for push feature
14+
*/
15+
@Test
16+
public void testPush() {
17+
SimpleStackFromList stack = new SimpleStackFromList();
18+
Assert.assertTrue(stack.isEmpty());
19+
Assert.assertTrue(stack.size() == 0);
20+
stack.push(3);
21+
Assert.assertFalse(stack.isEmpty());
22+
Assert.assertTrue(stack.size() == 1);
23+
stack.push(7);
24+
stack.push(31);
25+
Assert.assertTrue(stack.size() == 3);
26+
}
27+
28+
/**
29+
* Test case for pop feature
30+
*/
31+
@Test
32+
public void testPop() {
33+
SimpleStackFromList stack = new SimpleStackFromList();
34+
Assert.assertTrue(stack.isEmpty());
35+
Assert.assertTrue(stack.size() == 0);
36+
stack.push(3);
37+
stack.push(17);
38+
stack.push(35);
39+
stack.push(13);
40+
Assert.assertTrue(stack.size() == 4);
41+
Assert.assertEquals(stack.pop(), 13);
42+
Assert.assertEquals(stack.pop(), 35);
43+
Assert.assertEquals(stack.pop(), 17);
44+
Assert.assertTrue(stack.size() == 1);
45+
}
46+
47+
/**
48+
* Test case for peek feature
49+
*/
50+
@Test
51+
public void testPeek() {
52+
SimpleStackFromList stack = new SimpleStackFromList();
53+
Assert.assertTrue(stack.isEmpty());
54+
Assert.assertTrue(stack.size() == 0);
55+
stack.push(3);
56+
stack.push(17);
57+
stack.push(35);
58+
stack.push(13);
59+
Assert.assertTrue(stack.size() == 4);
60+
Assert.assertEquals(stack.peek(), 13);
61+
Assert.assertTrue(stack.size() == 4);
62+
stack.pop();
63+
Assert.assertTrue(stack.size() == 3);
64+
Assert.assertEquals(stack.peek(), 35);
65+
}
66+
67+
}

0 commit comments

Comments
 (0)