Skip to content
Open
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
107 changes: 96 additions & 11 deletions linked-list/src/main/java/com/bobocode/LinkedList.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.bobocode;

import java.util.Arrays;

/**
* {@link LinkedList} is a list implementation that is based on singly linked generic nodes. A node is implemented as
* inner static class {@link Node<T>}. In order to keep track on nodes, {@link LinkedList} keeps a reference to a head node.
*
* @param <T> generic type parameter
*/
public class LinkedList<T> implements List<T> {
private Node<T> head;
private int size = 0;

/**
* This method creates a list of provided elements
Expand All @@ -15,8 +19,12 @@ public class LinkedList<T> implements List<T> {
* @param <T> generic type
* @return a new list of elements the were passed as method parameters
*/
public static <T> List<T> of(T... elements) {
throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method
@SafeVarargs
static <T> List<T> of(T... elements) {
List<T> linkedList = new LinkedList<>();
Arrays.stream(elements)
.forEach(linkedList::add);
return linkedList;
}

/**
Expand All @@ -26,7 +34,14 @@ public static <T> List<T> of(T... elements) {
*/
@Override
public void add(T element) {
throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method
if (isEmpty()) {
head = new Node<>(element);
head.next = head;
head.prev = head;
size++;
} else {
insertNode(element, head);
}
}

/**
Expand All @@ -38,7 +53,15 @@ public void add(T element) {
*/
@Override
public void add(int index, T element) {
throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method
if (index == 0) {
if (isEmpty()) {
add(element);
} else {
head = insertNode(element, head);
}
} else {
insertNode(element, findNode(index - 1).next);
}
}

/**
Expand All @@ -50,7 +73,8 @@ public void add(int index, T element) {
*/
@Override
public void set(int index, T element) {
throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method
Node<T> node = findNode(index);
node.value = element;
}

/**
Expand All @@ -62,7 +86,7 @@ public void set(int index, T element) {
*/
@Override
public T get(int index) {
throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method
return findNode(index).value;
}

/**
Expand All @@ -73,7 +97,20 @@ public T get(int index) {
*/
@Override
public void remove(int index) {
throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method
if (index == 0) {
Node<T> newHead = head.next;
newHead.prev = head.prev;
head.prev.next = newHead;
head = newHead;
size--;
return;
}
Node<T> node = findNode(index);
Node<T> previousNode = node.prev;

previousNode.next = node.next;
node.next.prev = previousNode;
size--;
}


Expand All @@ -84,7 +121,17 @@ public void remove(int index) {
*/
@Override
public boolean contains(T element) {
throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method
if (isEmpty()) {
return false;
}
Node<T> node = head;
while(node.next != head) {
if (node.value == element) {
return true;
}
node = node.next;
}
return false;
}

/**
Expand All @@ -94,7 +141,7 @@ public boolean contains(T element) {
*/
@Override
public boolean isEmpty() {
throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method
return size == 0;
}

/**
Expand All @@ -104,14 +151,52 @@ public boolean isEmpty() {
*/
@Override
public int size() {
throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method
return size;
}

/**
* Removes all list elements
*/
@Override
public void clear() {
throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method
head = null;
size = 0;
}

private Node<T> findNode(int index) {
if (size - 1 < index || index < 0 || isEmpty()) {
throw new IndexOutOfBoundsException();
}
Node<T> node = head;
while(index > 0) {
index--;
node = node.next;
}
return node;
}

private Node<T> insertNode(T element, Node<T> nextNode) {
Node<T> newNode = new Node<>(element);
Node<T> prevNode = nextNode.prev;

newNode.prev = prevNode;
newNode.next = nextNode;

prevNode.next = newNode;
nextNode.prev = newNode;

size++;

return newNode;
}

private class Node<V> {
private V value;
private Node<V> next;
private Node<V> prev;

Node(V value) {
this.value = value;
}
}
}
33 changes: 29 additions & 4 deletions linked-queue/src/main/java/com/bobocode/LinkedQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,24 @@
* @param <T> a generic parameter
*/
public class LinkedQueue<T> implements Queue<T> {
private int size = 0;
private Node<T> head;
private Node<T> tale;

/**
* Adds an element to the end of the queue.
*
* @param element the element to add
*/
public void add(T element) {
throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method
if (isEmpty()) {
head = new Node<>(element);
tale = head;
} else {
tale.next = new Node<>(element);
tale = tale.next;
}
size++;
}

/**
Expand All @@ -24,7 +34,13 @@ public void add(T element) {
* @return an element that was retrieved from the head or null if queue is empty
*/
public T poll() {
throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method
if (isEmpty()) {
return null;
}
T value = head.value;
head = head.next;
size--;
return value;
}

/**
Expand All @@ -33,7 +49,7 @@ public T poll() {
* @return an integer value that is a size of queue
*/
public int size() {
throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method
return size;
}

/**
Expand All @@ -42,6 +58,15 @@ public int size() {
* @return {@code true} if the queue is empty, returns {@code false} if it's not
*/
public boolean isEmpty() {
throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method
return size == 0;
}

private class Node<V> {
private Node<V> next;
private V value;

Node(V value) {
this.value = value;
}
}
}