Skip to content

Gp 32 merge stack into exercise/completed #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.bobocode.stack;

import com.bobocode.stack.exception.EmptyStackException;
import com.bobocode.util.ExerciseNotCompletedException;

import java.util.Objects;
import java.util.stream.Stream;

/**
* {@link LinkedStack} represents a last-in-first-out (LIFO) stack of objects that is based on singly linked generic nodes.
Expand All @@ -10,6 +12,21 @@
* @param <T> generic type parameter
*/
public class LinkedStack<T> implements Stack<T> {
private static class Node<T> {
T element;
Node<T> next;

public static <T> Node<T> valueOf(T element) {
return new Node<>(element);
}

private Node(T element) {
this.element = element;
}
}

private Node<T> head;
private int size = 0;

/**
* This method creates a stack of provided elements
Expand All @@ -19,7 +36,9 @@ public class LinkedStack<T> implements Stack<T> {
* @return a new stack of elements that were passed as method parameters
*/
public static <T> LinkedStack<T> of(T... elements) {
throw new ExerciseNotCompletedException(); // todo: implement this method
LinkedStack<T> linkedStack = new LinkedStack<>();
Stream.of(elements).forEach(linkedStack::push);
return linkedStack;
}

/**
Expand All @@ -30,7 +49,13 @@ public static <T> LinkedStack<T> of(T... elements) {
*/
@Override
public void push(T element) {
throw new ExerciseNotCompletedException(); // todo: implement this method
Objects.requireNonNull(element);
Node<T> newNode = Node.valueOf(element);
if (head != null) {
newNode.next = head;
}
head = newNode;
size++;
}

/**
Expand All @@ -42,7 +67,18 @@ public void push(T element) {
*/
@Override
public T pop() {
throw new ExerciseNotCompletedException(); // todo: implement this method
if (head != null) {
size--;
return retrieveHead();
} else {
throw new EmptyStackException();
}
}

private T retrieveHead() {
T element = head.element;
this.head = head.next;
return element;
}

/**
Expand All @@ -52,7 +88,7 @@ public T pop() {
*/
@Override
public int size() {
throw new ExerciseNotCompletedException(); // todo: implement this method
return size;
}

/**
Expand All @@ -62,6 +98,6 @@ public int size() {
*/
@Override
public boolean isEmpty() {
throw new ExerciseNotCompletedException(); // todo: implement this method;
return head == null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.bobocode.stack;

public class EmptyStackException extends RuntimeException{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.bobocode.stack;

import java.util.Objects;
import java.util.stream.Stream;

/**
* {@link LinkedStack} represents a last-in-first-out (LIFO) stack of objects that is based on singly linked generic nodes.
* A node is implemented as inner static class {@link Node<T>}.
*
* @param <T> generic type parameter
*/
public class LinkedStack<T> implements Stack<T> {
private static class Node<T> {
T element;
Node<T> next;

public static <T> Node<T> valueOf(T element) {
return new Node<>(element);
}

private Node(T element) {
this.element = element;
}
}

private Node<T> head;
private int size = 0;

/**
* This method creates a stack of provided elements
*
* @param elements elements to add
* @param <T> generic type
* @return a new stack of elements that were passed as method parameters
*/
public static <T> LinkedStack<T> of(T... elements) {
LinkedStack<T> linkedStack = new LinkedStack<>();
Stream.of(elements).forEach(linkedStack::push);
return linkedStack;
}

/**
* The method pushes an element onto the top of this stack. This has exactly the same effect as:
* addElement(item)
*
* @param element elements to add
*/
@Override
public void push(T element) {
Objects.requireNonNull(element);
Node<T> newNode = Node.valueOf(element);
if (head != null) {
newNode.next = head;
}
head = newNode;
size++;
}

/**
* This method removes the object at the top of this stack
* and returns that object as the value of this function.
*
* @return The object at the top of this stack
* @throws EmptyStackException - if this stack is empty
*/
@Override
public T pop() {
if (head != null) {
size--;
return retrieveHead();
} else {
throw new EmptyStackException();
}
}

private T retrieveHead() {
T element = head.element;
this.head = head.next;
return element;
}

/**
* Returns the number of elements in the stack
*
* @return number of elements
*/
@Override
public int size() {
return size;
}

/**
* Checks if a stack is empty
*
* @return {@code true} if a stack is empty, {@code false} otherwise
*/
@Override
public boolean isEmpty() {
return head == null;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,75 @@
package com.bobocode.stack;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class StackTest {

private Stack<Integer> intStack = new LinkedStack<>();

@Test
void pushAndPopElementOntoEmptyStack() {
intStack.push(243);

assertThat(intStack.pop()).isEqualTo(243);
}

@Test
void popElementFromEmptyStack() {
assertThrows(EmptyStackException.class, () -> intStack.pop());
}

@Test
void pushElements() {
intStack = LinkedStack.of(23, 35, 72);

intStack.push(55);

assertThat(intStack.pop()).isEqualTo(55);
}

@Test
void popElements() {
intStack = LinkedStack.of(87, 53, 66);

intStack.pop();
intStack.push(234);
Integer lastElement = intStack.pop();

assertThat(lastElement).isEqualTo(234);
}

@Test
void size() {
intStack = LinkedStack.of(87, 53, 66);

int actualSize = intStack.size();

assertThat(actualSize).isEqualTo(3);
}

@Test
void sizeOnEmptyStack() {
int actualSize = intStack.size();

assertThat(actualSize).isEqualTo(0);
}

@Test
void isEmpty() {
intStack = LinkedStack.of(87, 53, 66);

boolean stackEmpty = intStack.isEmpty();

assertThat(stackEmpty).isEqualTo(false);
}

@Test
void isEmptyOnEmptyStack() {
boolean stackEmpty = intStack.isEmpty();

assertThat(stackEmpty).isEqualTo(true);
}
}