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
5 changes: 5 additions & 0 deletions MyLinkedList/Collection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package MyLinkedList;

public interface Collection {
int size();
}
134 changes: 134 additions & 0 deletions MyLinkedList/LinkedListImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package MyLinkedList;

import java.util.Iterator;
import java.util.Spliterator;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.function.Consumer;

public class LinkedListImpl<T> implements MyList<T> {
static class MyLinkedListIterator<T> implements Iterator<T> {
private Node current;

public MyLinkedListIterator(Node first) {
this.current = first;
}

@Override
public boolean hasNext() {
return current.next != null;
}

@Override
public T next() {
current = current.next;
return (T) current.value;
}
}

private static class Node {
Node next;
Object value;

public Node(Object value) {
this.value = value;
this.next = null;
}
}

Node first;
Node last;
int size = 0;


@Override
public void add(T element) {
if(first == null) {
first = new Node(element);
last = first;
} else {
last.next = new Node(element);
last = last.next;
}
size++;
}

@Override
public void remove(int index) {
if (first == null) {
return;
}
if (first.value == get(index)) {
first = first.next;
--size;
return;
}
Node current = first;
while(current.next != null) {
if (current.next.value == get(index)) {
current.next = current.next.next;
size--;
return;
}
current = current.next;
}
}

@Override
public void remove() {
Node current = first;
first = current.next;
size--;
}

@Override
public void remove(T t) {
if (first == null) {
return;
}
if (first.value == t) {
first = first.next;
--size;
return;
}
Node current = first;
while(current.next != null) {
if (current.next.value == t) {
current.next = current.next.next;
size--;
return;
}
current = current.next;
}
}

@Override
public T get(int index) {
Node current = first;
for (int i = 0; i < index; i++) {
current = current.next;
}
return (T) current.value;
}

@Override
public int size() {
return size;
}

// Iterable

@Override
public Iterator<T> iterator() {
return new MyLinkedListIterator<T>(first);
}

@Override
public void forEach(Consumer<? super T> action) {

}

@Override
public Spliterator<T> spliterator() {
return null;
}
}
24 changes: 24 additions & 0 deletions MyLinkedList/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package MyLinkedList;
import MyLinkedList.MyList;
import MyLinkedList.LinkedListImpl;

import java.util.LinkedList;

public class Main {
public static void main(String[] args) {
MyList<String> l1 = new LinkedListImpl<>();
l1.add("10");
l1.add("10");
l1.add("10");
l1.add("15");
l1.add("20");
l1.add("25");
l1.add("30");
l1.add("35");
l1.remove(0);
System.out.println("size: " + l1.size());
for (int i = 0; i < l1.size(); i++) {
System.out.println(l1.get(i));
}
}
}
10 changes: 10 additions & 0 deletions MyLinkedList/MyList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package MyLinkedList;
//import com.company.Collection;

public interface MyList<T> extends Collection, Iterable<T> {
void add(T element);
void remove(int index);
void remove(T Object);
void remove();
T get(int index);
}
52 changes: 52 additions & 0 deletions MyLinkedList/Tests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package MyLinkedList;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import MyLinkedList.LinkedListImpl;
import MyLinkedList.MyList;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;


public class Tests {
static MyList<String> l1 = new LinkedListImpl<>();
@BeforeAll
public static void init() {
l1.add("1");
l1.add("2");
l1.add("3");
l1.add("4");
l1.add("5");
l1.add("6");
l1.add("7");
l1.add("8");
}

@Test
public void TestAdd() {
Assertions.assertEquals(8, l1.size());
}

@Test
public void TestRemoveByIndex() {
l1.remove(2);
Assertions.assertEquals("4", l1.get(2));
}

@Test
public void TestRemoveByObject() {
l1.remove("8");
Assertions.assertEquals("7", l1.get(6));
}

@Test
public void TestRemoveHead() {
l1.remove();
Assertions.assertEquals("2", l1.get(0));
}

@Test
public void TestGet() {
Assertions.assertEquals("5", l1.get(4));
}
}