Skip to content

Commit cfcee8b

Browse files
committed
GP-32 Update Stack(0-2 + 0-6) for completed
1 parent 8b5887a commit cfcee8b

File tree

5 files changed

+221
-8
lines changed

5 files changed

+221
-8
lines changed

2-0-data-structures-and-algorithms/src/main/java/com/bobocode/stack/LinkedStack.java

+42-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.bobocode.stack;
22

33
import com.bobocode.stack.exception.EmptyStackException;
4-
import com.bobocode.util.ExerciseNotCompletedException;
4+
5+
import java.util.Objects;
6+
import java.util.stream.Stream;
57

68
/**
79
* {@link LinkedStack} represents a last-in-first-out (LIFO) stack of objects that is based on singly linked generic nodes.
@@ -10,6 +12,21 @@
1012
* @param <T> generic type parameter
1113
*/
1214
public class LinkedStack<T> implements Stack<T> {
15+
private static class Node<T> {
16+
T element;
17+
Node<T> next;
18+
19+
public static <T> Node<T> valueOf(T element) {
20+
return new Node<>(element);
21+
}
22+
23+
private Node(T element) {
24+
this.element = element;
25+
}
26+
}
27+
28+
private Node<T> head;
29+
private int size = 0;
1330

1431
/**
1532
* This method creates a stack of provided elements
@@ -19,7 +36,9 @@ public class LinkedStack<T> implements Stack<T> {
1936
* @return a new stack of elements that were passed as method parameters
2037
*/
2138
public static <T> LinkedStack<T> of(T... elements) {
22-
throw new ExerciseNotCompletedException(); // todo: implement this method
39+
LinkedStack<T> linkedStack = new LinkedStack<>();
40+
Stream.of(elements).forEach(linkedStack::push);
41+
return linkedStack;
2342
}
2443

2544
/**
@@ -30,7 +49,13 @@ public static <T> LinkedStack<T> of(T... elements) {
3049
*/
3150
@Override
3251
public void push(T element) {
33-
throw new ExerciseNotCompletedException(); // todo: implement this method
52+
Objects.requireNonNull(element);
53+
Node<T> newNode = Node.valueOf(element);
54+
if (head != null) {
55+
newNode.next = head;
56+
}
57+
head = newNode;
58+
size++;
3459
}
3560

3661
/**
@@ -42,7 +67,18 @@ public void push(T element) {
4267
*/
4368
@Override
4469
public T pop() {
45-
throw new ExerciseNotCompletedException(); // todo: implement this method
70+
if (head != null) {
71+
size--;
72+
return retrieveHead();
73+
} else {
74+
throw new EmptyStackException();
75+
}
76+
}
77+
78+
private T retrieveHead() {
79+
T element = head.element;
80+
this.head = head.next;
81+
return element;
4682
}
4783

4884
/**
@@ -52,7 +88,7 @@ public T pop() {
5288
*/
5389
@Override
5490
public int size() {
55-
throw new ExerciseNotCompletedException(); // todo: implement this method
91+
return size;
5692
}
5793

5894
/**
@@ -62,6 +98,6 @@ public int size() {
6298
*/
6399
@Override
64100
public boolean isEmpty() {
65-
throw new ExerciseNotCompletedException(); // todo: implement this method;
101+
return head == null;
66102
}
67103
}

2-0-data-structures-and-algorithms/src/test/java/com/bobocode/stack/StackTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class StackTest {
1717
@Test
1818
@Order(1)
1919
void pushAndPopElementOntoEmptyStack() {
20-
intStack.push(234);
20+
intStack.push(243);
2121

2222
assertThat(intStack.pop()).isEqualTo(243);
2323
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.bobocode.stack;
2+
3+
public class EmptyStackException extends RuntimeException{
4+
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.bobocode.stack;
2+
3+
import java.util.Objects;
4+
import java.util.stream.Stream;
5+
6+
/**
7+
* {@link LinkedStack} represents a last-in-first-out (LIFO) stack of objects that is based on singly linked generic nodes.
8+
* A node is implemented as inner static class {@link Node<T>}.
9+
*
10+
* @param <T> generic type parameter
11+
*/
12+
public class LinkedStack<T> implements Stack<T> {
13+
private static class Node<T> {
14+
T element;
15+
Node<T> next;
16+
17+
public static <T> Node<T> valueOf(T element) {
18+
return new Node<>(element);
19+
}
20+
21+
private Node(T element) {
22+
this.element = element;
23+
}
24+
}
25+
26+
private Node<T> head;
27+
private int size = 0;
28+
29+
/**
30+
* This method creates a stack of provided elements
31+
*
32+
* @param elements elements to add
33+
* @param <T> generic type
34+
* @return a new stack of elements that were passed as method parameters
35+
*/
36+
public static <T> LinkedStack<T> of(T... elements) {
37+
LinkedStack<T> linkedStack = new LinkedStack<>();
38+
Stream.of(elements).forEach(linkedStack::push);
39+
return linkedStack;
40+
}
41+
42+
/**
43+
* The method pushes an element onto the top of this stack. This has exactly the same effect as:
44+
* addElement(item)
45+
*
46+
* @param element elements to add
47+
*/
48+
@Override
49+
public void push(T element) {
50+
Objects.requireNonNull(element);
51+
Node<T> newNode = Node.valueOf(element);
52+
if (head != null) {
53+
newNode.next = head;
54+
}
55+
head = newNode;
56+
size++;
57+
}
58+
59+
/**
60+
* This method removes the object at the top of this stack
61+
* and returns that object as the value of this function.
62+
*
63+
* @return The object at the top of this stack
64+
* @throws EmptyStackException - if this stack is empty
65+
*/
66+
@Override
67+
public T pop() {
68+
if (head != null) {
69+
size--;
70+
return retrieveHead();
71+
} else {
72+
throw new EmptyStackException();
73+
}
74+
}
75+
76+
private T retrieveHead() {
77+
T element = head.element;
78+
this.head = head.next;
79+
return element;
80+
}
81+
82+
/**
83+
* Returns the number of elements in the stack
84+
*
85+
* @return number of elements
86+
*/
87+
@Override
88+
public int size() {
89+
return size;
90+
}
91+
92+
/**
93+
* Checks if a stack is empty
94+
*
95+
* @return {@code true} if a stack is empty, {@code false} otherwise
96+
*/
97+
@Override
98+
public boolean isEmpty() {
99+
return head == null;
100+
}
101+
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,75 @@
11
package com.bobocode.stack;
22

3-
public class StackTest {
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
import static org.junit.jupiter.api.Assertions.assertThrows;
7+
8+
class StackTest {
9+
10+
private Stack<Integer> intStack = new LinkedStack<>();
11+
12+
@Test
13+
void pushAndPopElementOntoEmptyStack() {
14+
intStack.push(243);
15+
16+
assertThat(intStack.pop()).isEqualTo(243);
17+
}
18+
19+
@Test
20+
void popElementFromEmptyStack() {
21+
assertThrows(EmptyStackException.class, () -> intStack.pop());
22+
}
23+
24+
@Test
25+
void pushElements() {
26+
intStack = LinkedStack.of(23, 35, 72);
27+
28+
intStack.push(55);
29+
30+
assertThat(intStack.pop()).isEqualTo(55);
31+
}
32+
33+
@Test
34+
void popElements() {
35+
intStack = LinkedStack.of(87, 53, 66);
36+
37+
intStack.pop();
38+
intStack.push(234);
39+
Integer lastElement = intStack.pop();
40+
41+
assertThat(lastElement).isEqualTo(234);
42+
}
43+
44+
@Test
45+
void size() {
46+
intStack = LinkedStack.of(87, 53, 66);
47+
48+
int actualSize = intStack.size();
49+
50+
assertThat(actualSize).isEqualTo(3);
51+
}
52+
53+
@Test
54+
void sizeOnEmptyStack() {
55+
int actualSize = intStack.size();
56+
57+
assertThat(actualSize).isEqualTo(0);
58+
}
59+
60+
@Test
61+
void isEmpty() {
62+
intStack = LinkedStack.of(87, 53, 66);
63+
64+
boolean stackEmpty = intStack.isEmpty();
65+
66+
assertThat(stackEmpty).isEqualTo(false);
67+
}
68+
69+
@Test
70+
void isEmptyOnEmptyStack() {
71+
boolean stackEmpty = intStack.isEmpty();
72+
73+
assertThat(stackEmpty).isEqualTo(true);
74+
}
475
}

0 commit comments

Comments
 (0)