Skip to content

Gp 26 add compleated solution to exercise/completed #16

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
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,8 @@
package com.bobocode.linked_list;


import com.bobocode.util.ExerciseNotCompletedException;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.stream.Stream;

/**
* {@link LinkedList} is a list implementation that is based on singly linked generic nodes. A node is implemented as
Expand All @@ -10,6 +11,9 @@
* @param <T> generic type parameter
*/
public class LinkedList<T> implements List<T> {
private Node<T> head;
private Node<T> tail;
private int size;

/**
* This method creates a list of provided elements
Expand All @@ -19,7 +23,9 @@ public class LinkedList<T> implements List<T> {
* @return a new list of elements the were passed as method parameters
*/
public static <T> List<T> of(T... elements) {
throw new ExerciseNotCompletedException(); // todo: implement this method
LinkedList<T> linkedList = new LinkedList<>();
Stream.of(elements).forEach(linkedList::add);
return linkedList;
}

/**
Expand All @@ -29,7 +35,7 @@ public static <T> List<T> of(T... elements) {
*/
@Override
public void add(T element) {
throw new ExerciseNotCompletedException(); // todo: implement this method
add(size, element);
}

/**
Expand All @@ -41,7 +47,51 @@ public void add(T element) {
*/
@Override
public void add(int index, T element) {
throw new ExerciseNotCompletedException(); // todo: implement this method
Node<T> newNode = Node.valueOf(element);
if (index == 0) {
addAsHead(newNode);
} else if (index == size) {
addAsTail(newNode);
} else {
add(index, newNode);
}
size++;
}

private void addAsHead(Node<T> newNode) {
newNode.next = head;
head = newNode;
if (head.next == null) {
tail = head;
}
}

private void addAsTail(Node<T> newNode) {
tail.next = newNode;
tail = newNode;
}

private void add(int index, Node<T> newNode) {
Node<T> node = findNodeByIndex(index - 1);
newNode.next = node.next;
node.next = newNode;
}

private Node<T> findNodeByIndex(int index) {
Objects.checkIndex(index, size);
if (index == size - 1) {
return tail;
} else {
return nodeAt(index);
}
}

private Node<T> nodeAt(int index) {
Node<T> currentNode = head;
for (int i = 0; i < index; i++) {
currentNode = currentNode.next;
}
return currentNode;
}

/**
Expand All @@ -53,7 +103,8 @@ public void add(int index, T element) {
*/
@Override
public void set(int index, T element) {
throw new ExerciseNotCompletedException(); // todo: implement this method
Node<T> node = findNodeByIndex(index);
node.value = element;
}

/**
Expand All @@ -65,7 +116,8 @@ public void set(int index, T element) {
*/
@Override
public T get(int index) {
throw new ExerciseNotCompletedException(); // todo: implement this method
Node<T> node = findNodeByIndex(index);
return node.value;
}

/**
Expand All @@ -76,7 +128,8 @@ public T get(int index) {
*/
@Override
public T getFirst() {
throw new ExerciseNotCompletedException(); // todo: implement this method
checkElementsExist();
return head.value;
}

/**
Expand All @@ -87,20 +140,44 @@ public T getFirst() {
*/
@Override
public T getLast() {
throw new ExerciseNotCompletedException(); // todo: implement this method
checkElementsExist();
return tail.value;
}

private void checkElementsExist() {
if (head == null) {
throw new NoSuchElementException();
}
}

/**
* Removes an elements by its position index. In case provided index in out of the list bounds it
* throws {@link IndexOutOfBoundsException}
*
* @param index element index
* @return an element value
*/
@Override
public void remove(int index) {
throw new ExerciseNotCompletedException(); // todo: implement this method
if (index == 0) {
Objects.checkIndex(index, size);
removeHead();
} else {
Node<T> previousNode = findNodeByIndex(index - 1);
previousNode.next = previousNode.next.next;
if (index == size - 1) {
tail = previousNode;
}
}
size--;
}

private void removeHead() {
head = head.next;
if (head == null) {
tail = null;
}
}

/**
* Checks if a specific exists in he list
Expand All @@ -109,7 +186,14 @@ public void remove(int index) {
*/
@Override
public boolean contains(T element) {
throw new ExerciseNotCompletedException(); // todo: implement this method
Node<T> currentNode = head;
while (currentNode != null) {
if (currentNode.value.equals(element)) {
return true;
}
currentNode = currentNode.next;
}
return false;
}

/**
Expand All @@ -119,7 +203,7 @@ public boolean contains(T element) {
*/
@Override
public boolean isEmpty() {
throw new ExerciseNotCompletedException(); // todo: implement this method
return head == null;
}

/**
Expand All @@ -129,14 +213,28 @@ public boolean isEmpty() {
*/
@Override
public int size() {
throw new ExerciseNotCompletedException(); // todo: implement this method
return size;
}

/**
* Removes all list elements
*/
@Override
public void clear() {
throw new ExerciseNotCompletedException(); // todo: implement this method
head = tail = null;
size = 0;
}

static class Node<T> {
private T value;
private Node<T> next;

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

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