From c6705cfc9db2429f75bfd5a8dcfc798bbb717e7f Mon Sep 17 00:00:00 2001 From: wheelman1995 Date: Fri, 7 Sep 2018 11:16:31 +0300 Subject: [PATCH] add lesson4 homework --- lesson4/src/DoubleLinkedList.java | 119 ++++++++++++++++++++++++++++++ lesson4/src/RelatedList.java | 8 +- lesson4/src/Relations.java | 34 +++++---- 3 files changed, 144 insertions(+), 17 deletions(-) create mode 100644 lesson4/src/DoubleLinkedList.java diff --git a/lesson4/src/DoubleLinkedList.java b/lesson4/src/DoubleLinkedList.java new file mode 100644 index 0000000..4149487 --- /dev/null +++ b/lesson4/src/DoubleLinkedList.java @@ -0,0 +1,119 @@ +import java.util.Iterator; +import java.util.Objects; +import java.util.function.Consumer; + +public class DoubleLinkedList extends RelatedList { + private class Node { + 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 node = (Node) o; + return Objects.equals(c, node.c); + } + + Node prev; + Node next; + } + + private Node head; + private Node iterNext; + + public DoubleLinkedList() { + head = null; + iterNext = head; + } + + + @Override + public void insert(Object c) { + Node n = new Node((T)c); + n.next = head; + if (head != null) { + head.prev = n; + } + head = n; + iterNext = head; + } + + + public T delete(T c) { + Node 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 n = new Node<>((T) c); + Node 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 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 toBeReturned = iterNext; + iterNext = iterNext.next; + return toBeReturned.c; + } +} diff --git a/lesson4/src/RelatedList.java b/lesson4/src/RelatedList.java index 4d722b2..d49c642 100644 --- a/lesson4/src/RelatedList.java +++ b/lesson4/src/RelatedList.java @@ -1,8 +1,8 @@ import java.util.Objects; public class RelatedList { - private class Node { - T c; + protected class Node { + private T c; Node next; public Node(T c) { this.c = c; @@ -20,7 +20,7 @@ public boolean equals(Object o) { } } - private Node head; + protected Node head; public RelatedList() { head = null; @@ -56,7 +56,7 @@ public boolean contains(T c) { return true; } - public T delete(String name) { + public final T delete(String name) { Node current = head; Node previous = head; while (!(((Cat)current.c).getName()).equals(name)) { diff --git a/lesson4/src/Relations.java b/lesson4/src/Relations.java index 087cffd..6f36c1f 100644 --- a/lesson4/src/Relations.java +++ b/lesson4/src/Relations.java @@ -5,20 +5,28 @@ public class Relations { public static void main(String[] args) { - RelatedList 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 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 map = new HashMap<>(); - Set> set = map.entrySet(); - Iterator> iter = set.iterator(); - while (iter.hasNext()) { - System.out.println(iter.next().getKey() + iter.next().getValue()); +// HashMap map = new HashMap<>(); +// Set> set = map.entrySet(); +// Iterator> 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()); } } }