Skip to content

Commit fc93b66

Browse files
author
KAKANAKOU Miguel Stephane
committed
Add LinkedList implementation of Stack in the com.stacks package and also its testcase
1 parent 733e41e commit fc93b66

File tree

5 files changed

+235
-3
lines changed

5 files changed

+235
-3
lines changed

Java-data-Structures/src/main/java/com/stacks/ArrayImplOfStack.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
* the stack.
1313
*
1414
* {@link https://en.wikipedia.org/wiki/Stack_(abstract_data_type)}
15+
*
16+
* This is a Resizing Array implementation of the Stack API
1517
*
1618
* @author STEPHANE MIGUEL KAKANAKOU (Skakanakou@gmail.com)
1719
*
1820
* @param <Item>
1921
*/
20-
public class ArrayImplOfStack<Item> implements Stack<Item>, Iterable<Item> {
22+
public class ArrayImplOfStack<Item> implements Stack<Item> {
2123

2224
/*** Contains the size of the stack. */
2325
private int arraySize;
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
package com.stacks;
2+
3+
import java.util.Iterator;
4+
5+
/**
6+
* In computer science, a stack is an abstract data type that serves as a
7+
* collection of elements, with two principal operations: push, which adds an
8+
* element to the collection, and pop, which removes the most recently added
9+
* element that was not yet removed. The order in which elements come off a
10+
* stack gives rise to its alternative name, LIFO (for last in, first out).
11+
* Additionally, a peek operation may give access to the top without modifying
12+
* the stack.
13+
*
14+
* {@link https://en.wikipedia.org/wiki/Stack_(abstract_data_type)}
15+
*
16+
* This is a Linked List implementation of the Stack API
17+
*
18+
* @author STEPHANE MIGUEL KAKANAKOU (Skakanakou@gmail.com)
19+
*
20+
* @param <Item>
21+
*/
22+
public class LinkedListImplOfStack<Item> implements Stack<Item> {
23+
24+
/*
25+
* Structure of each node of the linked list
26+
*/
27+
private class Node {
28+
Item item;
29+
Node next;
30+
public Node(Item item, Node next) {
31+
this.item = item;
32+
this.next = next;
33+
}
34+
}
35+
36+
/*** A reference to the Node on the top of the stack. */
37+
private Node topStack;
38+
39+
/*** Contains the size of the stack. */
40+
private int listSize;
41+
42+
/**
43+
* Constructs an empty stack.
44+
*/
45+
public LinkedListImplOfStack() {
46+
this.listSize = 0;
47+
this.topStack = null;
48+
}
49+
50+
/*** {@inheritDoc} */
51+
@Override
52+
public Item pop() {
53+
if (this.isEmpty()) {
54+
throw new IllegalStateException("The stack is empty");
55+
}
56+
Item poppedItem = topStack.item;
57+
topStack = topStack.next;
58+
listSize--;
59+
return poppedItem;
60+
}
61+
62+
/*** {@inheritDoc} */
63+
@Override
64+
public Item peek() {
65+
if (this.isEmpty()) {
66+
throw new IllegalStateException("The stack is empty");
67+
}
68+
return topStack.item;
69+
}
70+
71+
/*** {@inheritDoc} */
72+
@Override
73+
public void push(Item item) {
74+
topStack = new Node(item, topStack);
75+
listSize++;
76+
}
77+
78+
/*** {@inheritDoc} */
79+
@Override
80+
public boolean isEmpty() {
81+
if(topStack == null)
82+
return true;
83+
return false;
84+
}
85+
86+
/*** {@inheritDoc} */
87+
@Override
88+
public void clear() {
89+
topStack = null;
90+
listSize = 0;
91+
}
92+
93+
/*** {@inheritDoc} */
94+
@Override
95+
public int size() {
96+
return listSize;
97+
}
98+
99+
/*** {@inheritDoc} */
100+
@Override
101+
public boolean contains(Item item) {
102+
Node top = topStack;
103+
while (top != null) {
104+
if (top.item.equals(item))
105+
return true;
106+
top = top.next;
107+
}
108+
return false;
109+
}
110+
111+
/*** {@inheritDoc} */
112+
@Override
113+
public Iterator<Item> iterator() {
114+
return new IteratorLinkedListImplOfStack();
115+
}
116+
117+
/**
118+
* IteratorLinkedListImplOfStack implements Iterator interface in order to
119+
* provide iterable capabilities to the stack.
120+
*
121+
* @author STEPHANE MIGUEL KAKANAKOU (Skakanakou@gmail.com)
122+
*
123+
*/
124+
private class IteratorLinkedListImplOfStack implements Iterator<Item> {
125+
126+
/*** Reference on the current node in the iteration. */
127+
private Node currentNode;
128+
129+
/*** Constructs IteratorLinkedListImplOfStack. */
130+
public IteratorLinkedListImplOfStack() {
131+
currentNode = topStack;
132+
}
133+
134+
/*** {@inheritDoc} */
135+
@Override
136+
public boolean hasNext() {
137+
if (currentNode != null)
138+
return true;
139+
return false;
140+
}
141+
142+
/*** {@inheritDoc} */
143+
@Override
144+
public Item next() {
145+
Item result = currentNode.item;
146+
currentNode = currentNode.next;
147+
return result;
148+
}
149+
}
150+
151+
}

Java-data-Structures/src/main/java/com/stacks/Stack.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
* @param <Item>
1717
*/
18-
public interface Stack<Item> {
18+
public interface Stack<Item> extends Iterable<Item>{
1919

2020
/**
2121
* Removes and return the object at the top of this stack.

Java-data-Structures/src/test/java/com/test/stacks/TestArrayImplOfStack.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import junit.framework.TestCase;
77

88
import com.stacks.ArrayImplOfStack;
9+
import com.stacks.Stack;
910

1011
/**
1112
* @author MacBook
@@ -14,7 +15,7 @@
1415
public class TestArrayImplOfStack extends TestCase{
1516

1617
public void testStack() {
17-
ArrayImplOfStack<String> stack = new ArrayImplOfStack<>(87);
18+
Stack<String> stack = new ArrayImplOfStack<>(87);
1819
assertEquals(true, stack.isEmpty());
1920

2021
// testing push
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.test.stacks;
2+
3+
4+
import com.stacks.LinkedListImplOfStack;
5+
import com.stacks.Stack;
6+
7+
import junit.framework.TestCase;
8+
9+
public class TestLinkedListImplOfStack extends TestCase{
10+
11+
public void testStack() {
12+
Stack<String> stack = new LinkedListImplOfStack<>();
13+
assertEquals(true, stack.isEmpty());
14+
15+
// testing push
16+
stack.push("quick");
17+
stack.push("brown");
18+
stack.push("fox");
19+
assertEquals(false, stack.isEmpty());
20+
assertEquals(3, stack.size());
21+
22+
stack.clear();
23+
assertEquals(true, stack.isEmpty());
24+
25+
stack.push("quick");
26+
stack.push("brown");
27+
stack.push("fox");
28+
assertEquals(false, stack.isEmpty());
29+
assertEquals(3, stack.size());
30+
31+
// testing pop
32+
assertEquals("fox", stack.pop());
33+
assertEquals("brown", stack.pop());
34+
assertEquals(1, stack.size());
35+
assertEquals("quick", stack.pop());
36+
assertEquals(true, stack.isEmpty());
37+
assertEquals(0, stack.size());
38+
39+
// testing peek
40+
stack.clear();
41+
stack.push("over");
42+
stack.push("little");
43+
stack.push("lazy");
44+
stack.push("dog");
45+
assertEquals("dog",stack.peek());
46+
assertEquals(4, stack.size());
47+
stack.pop();
48+
assertEquals("lazy",stack.peek());
49+
50+
//testing Iterable
51+
stack.clear();
52+
stack.push("quick");
53+
stack.push("brown");
54+
stack.push("fox");
55+
stack.push("over");
56+
stack.push("little");
57+
stack.push("lazy");
58+
stack.push("dog");
59+
60+
String[] expected = {"dog", "lazy", "little", "over", "fox", "brown", "quick"};
61+
String[] result = new String[7];
62+
int i=0;
63+
for(String s : stack){
64+
result[i] = s;
65+
assertEquals(expected[i], result[i]);
66+
i++;
67+
}
68+
69+
// testing underflow
70+
try {
71+
stack.pop();
72+
} catch (IllegalStateException e) {
73+
assertNotNull(e);
74+
} catch (Exception e) {
75+
assertNull(e);
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)