Skip to content
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

Lecture03 #1022

Open
wants to merge 2 commits into
base: lecture03
Choose a base branch
from
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
108 changes: 95 additions & 13 deletions lecture03/src/main/java/ru/atom/list/CustomLinkedList.java
Original file line number Diff line number Diff line change
@@ -1,61 +1,143 @@
package ru.atom.list;

import java.util.Collection;

import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Collection;
import java.util.ListIterator;


public class CustomLinkedList<E> implements List<E> {

private int size = 0;
private ListNode<E> head;
private ListNode<E> tail;

@Override
public int size() {
throw new UnsupportedOperationException();
return size;
}

@Override
public boolean isEmpty() {
throw new UnsupportedOperationException();
return size == 0;
}

@Override
public boolean contains(Object o) {
throw new UnsupportedOperationException();
ListNode<E> ptr = head;
while (ptr != null) {
if (ptr.value.equals(o)) {
return true;
}
ptr = ptr.next;
}
return false;
}

@Override
public Iterator<E> iterator() {
throw new UnsupportedOperationException();
return new Iterator<>() {

private ListNode<E> ptr = head;

@Override
public boolean hasNext() {
return ptr != null;
}

@Override
public E next() {
if (ptr == null) {
throw new NoSuchElementException();
}
E nextVal = ptr.value;
ptr = ptr.next;
return nextVal;
}
};
}

@Override
public boolean add(E e) {
throw new UnsupportedOperationException();
if (isEmpty()) {
head = new ListNode<>(e, null, null);
tail = head;
} else {
tail.next = new ListNode<>(e, tail, null);
tail = tail.next;
}
size++;
return true;
}

@Override
public boolean remove(Object o) {
throw new UnsupportedOperationException();
if (size == 0) {
return false;
} else if (head.value.equals(o)) {
head = head.next;
size--;
if (size == 0) {
tail = null;
}
return true;
}
ListNode<E> ptr = head;
while (ptr.next != null) {
if (ptr.next.value.equals(o)) {
if (tail == ptr.next) {
tail = tail.prev;
}
ptr.next = ptr.next.next;
if (ptr.next.next != null) {
ptr.next.next.prev = ptr;
}
size--;
return true;
}
ptr = ptr.next;
}
return false;
}

@Override
public void clear() {
throw new UnsupportedOperationException();
head = null;
tail = null;
size = 0;
}

@Override
public E get(int index) {
throw new UnsupportedOperationException();
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
ListNode<E> ptr = head;
for (int i = 0; i < index; i++) {
ptr = ptr.next;
}
return ptr.value;
}

@Override
public int indexOf(Object o) {
throw new UnsupportedOperationException();
ListNode<E> ptr = head;
for (int i = 0; i < size; i++) {
if (ptr.value.equals(o)) {
return i;
}
ptr = ptr.next;
}
return -1;
}

@Override
public boolean addAll(Collection<? extends E> c) {
throw new UnsupportedOperationException();
for (E element : c) {
add(element);
}
return true;
}


Expand All @@ -66,7 +148,7 @@ public boolean addAll(Collection<? extends E> c) {
public boolean containsAll(Collection<?> c) {
for (Object o : c) {
if (!contains(o)) {
return true;
return false;
}
}
return true;
Expand Down
9 changes: 9 additions & 0 deletions lecture03/src/main/java/ru/atom/list/ListNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,13 @@
* Contains ref to next node, prev node and value
*/
public class ListNode<E> {
E value;
ListNode<E> prev;
ListNode<E> next;

public ListNode(E value, ListNode<E> prev, ListNode<E> next) {
this.value = value;
this.prev = prev;
this.next = next;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ru.atom.list;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import java.util.Arrays;
Expand All @@ -11,8 +10,6 @@
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;


@Ignore
public class CustomLinkedListTest {
private List<Integer> intList = new CustomLinkedList<>();
private List<String> stringList = new CustomLinkedList<>();
Expand Down