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
119 changes: 119 additions & 0 deletions lesson4/src/DoubleLinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import java.util.Iterator;
import java.util.Objects;
import java.util.function.Consumer;

public class DoubleLinkedList<T> extends RelatedList {
private class Node<T> {
T c;
public Node(T c) {
this.c = c;
}

@Override
public String toString() {
return c.toString();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Node)) return false;
Node<T> node = (Node) o;
return Objects.equals(c, node.c);
}

Node<T> prev;
Node<T> next;
}

private Node<T> head;
private Node<T> iterNext;

public DoubleLinkedList() {
head = null;
iterNext = head;
}


@Override
public void insert(Object c) {
Node<T> n = new Node<T>((T)c);
n.next = head;
if (head != null) {
head.prev = n;
}
head = n;
iterNext = head;
}


public T delete(T c) {
Node<T> current = head;

while (!current.c.equals(c)) {
if (current.next == null)
return null;

current = current.next;

}
if (current == head) {
head = head.next;
iterNext = head;
} else {
current.prev.next = current.next;
current.next.prev = current.prev;
}
return current.c;
}

@Override
public boolean isEmpty() {
return head == null;
}

@Override
public T remove() {
if (isEmpty())
return null;
T temp = head.c;
head = head.next;
iterNext = head;
return temp;
}

@Override
public boolean contains(Object c) {
Node<T> n = new Node<>((T) c);
Node<T> current = head;
while (!current.equals(n)) {
if (current.next == null)
return false;
else
current = current.next;
}
return true;
}

@Override
public String toString() {
if (isEmpty()) return "[]";
Node<T> current = head;
StringBuilder sb = new StringBuilder("[");
while (current != null) {
sb.append(current);
current = current.next;
sb.append((current == null) ? "]" : ", ");
}
return sb.toString();
}

public boolean hasNext() {
return iterNext != null;
}

public T next() {
Node<T> toBeReturned = iterNext;
iterNext = iterNext.next;
return toBeReturned.c;
}
}
8 changes: 4 additions & 4 deletions lesson4/src/RelatedList.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import java.util.Objects;

public class RelatedList<T> {
private class Node<T> {
T c;
protected class Node<T> {
private T c;
Node<T> next;
public Node(T c) {
this.c = c;
Expand All @@ -20,7 +20,7 @@ public boolean equals(Object o) {
}
}

private Node<T> head;
protected Node<T> head;

public RelatedList() {
head = null;
Expand Down Expand Up @@ -56,7 +56,7 @@ public boolean contains(T c) {
return true;
}

public T delete(String name) {
public final T delete(String name) {
Node<T> current = head;
Node<T> previous = head;
while (!(((Cat)current.c).getName()).equals(name)) {
Expand Down
34 changes: 21 additions & 13 deletions lesson4/src/Relations.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,28 @@

public class Relations {
public static void main(String[] args) {
RelatedList<Cat> rl = new RelatedList<>();
rl.insert(new Cat(1, "Barsik"));
rl.insert(new Cat(2, "Murzik"));
rl.insert(new Cat(4, "Kissik"));
System.out.println(rl);
System.out.println(rl.remove());
System.out.println(rl);
System.out.println(rl.contains(new Cat(2, "Murzik")));
DoubleLinkedList<Cat> dll = new DoubleLinkedList<>();
Cat c = new Cat(2, "Murzik");
dll.insert(new Cat(1, "Barsik"));
dll.insert(c);
dll.insert(new Cat(4, "Kissik"));
// System.out.println(dll);
// System.out.println(dll.delete(c));
// System.out.println(dll);
// System.out.println(dll.contains(new Cat(2, "Murzik")));


HashMap<String, String> map = new HashMap<>();
Set<Map.Entry<String, String>> set = map.entrySet();
Iterator<Map.Entry<String, String>> iter = set.iterator();
while (iter.hasNext()) {
System.out.println(iter.next().getKey() + iter.next().getValue());
// HashMap<String, String> map = new HashMap<>();
// Set<Map.Entry<String, String>> set = map.entrySet();
// Iterator<Map.Entry<String, String>> iter = set.iterator();
// while (iter.hasNext()) {
// System.out.println(iter.next().getKey() + iter.next().getValue());
// }

dll.insert(c);

while (dll.hasNext()) {
System.out.println(dll.next());
}
}
}