From aa8f055933ff55e2a8c8bd1ac0b3e2b194955f5a Mon Sep 17 00:00:00 2001 From: hasksy Date: Sat, 28 Nov 2020 10:56:12 +0300 Subject: [PATCH 1/5] Added parts of LinkedList realisation --- src/LinkedList/MyLinkedList.java | 109 +++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 src/LinkedList/MyLinkedList.java diff --git a/src/LinkedList/MyLinkedList.java b/src/LinkedList/MyLinkedList.java new file mode 100644 index 0000000..2aaa531 --- /dev/null +++ b/src/LinkedList/MyLinkedList.java @@ -0,0 +1,109 @@ +package LinkedList; + +class MyLinkedList { + + static class Node { + private int value; + private Node nextNode; + private Node prevNode; + + public Node(int value) { + this.value = value; + } + + public int getValue() { + return value; + } + + public Node getNextNode() { + return nextNode; + } + + public Node getPrevNode() { + return prevNode; + } + + public void setValue(int value) { + this.value = value; + } + + public void setNextNode(Node nextNode) { + this.nextNode = nextNode; + } + + public void setPrevNode(Node prevNode) { + this.prevNode = prevNode; + } + } + + int length; + Node startNode; + Node endNode; + + public MyLinkedList() { + this.length = 0; + this.endNode = null; + this.startNode = null; + } + + public int get(int index) { + Node head = startNode; + if (index >= length || index < 0) return -1; + for (int count = 0; count < index; count++) head = head.nextNode; + return head.value; + } + + public void addAtHead(int val) { + Node add = new Node(val); + add.nextNode = startNode; + add.prevNode = null; + startNode.prevNode = add; + startNode = add; + length++; + } + + public void addAtTail(int val) { + Node add = new Node(val); + add.prevNode = endNode; + add.nextNode = null; + endNode.nextNode = add; + endNode = add; + length++; + } + + public void addAtIndex(int index, int val) { + Node add = new Node(val); + Node head = startNode; + + if (index == 0) { + addAtHead(val); + return; + } + + if (index == length - 1) { + addAtTail(val); + return; + } + + if (index >= length || index < 0) + return; + + for (int i = 0; i < index; i++) head = head.nextNode; + + add.nextNode = head.nextNode; + add.prevNode = head; + head.nextNode.nextNode.prevNode = add; + head.nextNode = add; + length++; + } + + public void deleteAtIndex(int index) { + Node head = startNode; + if (index >= length || index < 0) + return; + + for (int i = 0; i < index; i++) head = head.nextNode; + + length--; + } +} From 9ff51d180a1f8ce484bc36e4aaa4eceaa7031116 Mon Sep 17 00:00:00 2001 From: hasksy Date: Sat, 28 Nov 2020 11:54:43 +0300 Subject: [PATCH 2/5] Some little fixes (Still not ready) --- src/LinkedList/MyLinkedList.java | 58 +++++++++++++------------------- 1 file changed, 24 insertions(+), 34 deletions(-) diff --git a/src/LinkedList/MyLinkedList.java b/src/LinkedList/MyLinkedList.java index 2aaa531..2b0415d 100644 --- a/src/LinkedList/MyLinkedList.java +++ b/src/LinkedList/MyLinkedList.java @@ -3,36 +3,14 @@ class MyLinkedList { static class Node { - private int value; + private final int value; private Node nextNode; private Node prevNode; public Node(int value) { this.value = value; - } - - public int getValue() { - return value; - } - - public Node getNextNode() { - return nextNode; - } - - public Node getPrevNode() { - return prevNode; - } - - public void setValue(int value) { - this.value = value; - } - - public void setNextNode(Node nextNode) { - this.nextNode = nextNode; - } - - public void setPrevNode(Node prevNode) { - this.prevNode = prevNode; + nextNode = null; + prevNode = null; } } @@ -47,9 +25,9 @@ public MyLinkedList() { } public int get(int index) { - Node head = startNode; + Node head = startNode.nextNode; if (index >= length || index < 0) return -1; - for (int count = 0; count < index; count++) head = head.nextNode; + for (int i = 0; i < index; i++) head = head.nextNode; return head.value; } @@ -57,7 +35,6 @@ public void addAtHead(int val) { Node add = new Node(val); add.nextNode = startNode; add.prevNode = null; - startNode.prevNode = add; startNode = add; length++; } @@ -66,14 +43,13 @@ public void addAtTail(int val) { Node add = new Node(val); add.prevNode = endNode; add.nextNode = null; - endNode.nextNode = add; endNode = add; length++; } public void addAtIndex(int index, int val) { Node add = new Node(val); - Node head = startNode; + Node head = startNode.nextNode; if (index == 0) { addAtHead(val); @@ -90,20 +66,34 @@ public void addAtIndex(int index, int val) { for (int i = 0; i < index; i++) head = head.nextNode; - add.nextNode = head.nextNode; - add.prevNode = head; - head.nextNode.nextNode.prevNode = add; + Node tmp = head.nextNode; head.nextNode = add; + head.nextNode.nextNode = tmp; length++; } public void deleteAtIndex(int index) { - Node head = startNode; + Node head = startNode.nextNode; + + if (index == 0) { + // Добавить удаление самого элемента + startNode = startNode.nextNode; + startNode.prevNode = null; + } + + if (index == length - 1) { + // Добавить удаление самого элемента + endNode = endNode.prevNode; + endNode.nextNode = null; + } if (index >= length || index < 0) return; for (int i = 0; i < index; i++) head = head.nextNode; + head.prevNode.nextNode = head.nextNode; + head.nextNode.prevNode = head.prevNode; + head = null; length--; } } From 1faa7e1232795fd1af80973b03c19f779314951f Mon Sep 17 00:00:00 2001 From: hasksy Date: Sat, 28 Nov 2020 16:42:07 +0300 Subject: [PATCH 3/5] Reworked and working LinkedList for integers --- src/LinkedList/MyLinkedList.java | 67 +++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 23 deletions(-) diff --git a/src/LinkedList/MyLinkedList.java b/src/LinkedList/MyLinkedList.java index 2b0415d..77751e4 100644 --- a/src/LinkedList/MyLinkedList.java +++ b/src/LinkedList/MyLinkedList.java @@ -19,13 +19,13 @@ public Node(int value) { Node endNode; public MyLinkedList() { - this.length = 0; - this.endNode = null; - this.startNode = null; + length = 0; + startNode = null; + endNode = null; } public int get(int index) { - Node head = startNode.nextNode; + Node head = startNode; if (index >= length || index < 0) return -1; for (int i = 0; i < index; i++) head = head.nextNode; return head.value; @@ -33,64 +33,85 @@ public int get(int index) { public void addAtHead(int val) { Node add = new Node(val); + if (startNode == null) { + startNode = add; + endNode = add; + length++; + return; + } add.nextNode = startNode; - add.prevNode = null; + startNode.prevNode = add; startNode = add; length++; } public void addAtTail(int val) { Node add = new Node(val); + if (startNode == null) { + startNode = add; + endNode = add; + length++; + return; + } add.prevNode = endNode; - add.nextNode = null; + endNode.nextNode = add; endNode = add; length++; } public void addAtIndex(int index, int val) { - Node add = new Node(val); - Node head = startNode.nextNode; + if (index > length || index < 0) return; if (index == 0) { addAtHead(val); return; } - if (index == length - 1) { + if (index == length) { addAtTail(val); return; } - if (index >= length || index < 0) - return; + Node add = new Node(val); + Node head = startNode; for (int i = 0; i < index; i++) head = head.nextNode; - Node tmp = head.nextNode; - head.nextNode = add; - head.nextNode.nextNode = tmp; + add.prevNode = head.prevNode; + add.nextNode = head; + head.prevNode.nextNode = add; + head.prevNode = add; length++; } public void deleteAtIndex(int index) { - Node head = startNode.nextNode; + if (index >= length || index < 0) return; if (index == 0) { - // Добавить удаление самого элемента - startNode = startNode.nextNode; - startNode.prevNode = null; + if (length == 1) { + startNode = null; + endNode = null; + } else { + Node del = startNode; + startNode.nextNode.prevNode = null; + startNode = startNode.nextNode; + del = null; + } + length--; + return; } if (index == length - 1) { - // Добавить удаление самого элемента + Node del = endNode; + endNode.prevNode.nextNode = null; endNode = endNode.prevNode; - endNode.nextNode = null; - } - if (index >= length || index < 0) + del = null; + length--; return; + } + Node head = startNode; for (int i = 0; i < index; i++) head = head.nextNode; - head.prevNode.nextNode = head.nextNode; head.nextNode.prevNode = head.prevNode; head = null; From ba1b8a5186fa2e095d49e98dd502ff4654337b58 Mon Sep 17 00:00:00 2001 From: hasksy Date: Sat, 28 Nov 2020 18:37:03 +0300 Subject: [PATCH 4/5] Added working iterable MyLinkedList for any Object type --- src/LinkedList/MyLinkedList.java | 57 ++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 14 deletions(-) diff --git a/src/LinkedList/MyLinkedList.java b/src/LinkedList/MyLinkedList.java index 77751e4..7e9ce4e 100644 --- a/src/LinkedList/MyLinkedList.java +++ b/src/LinkedList/MyLinkedList.java @@ -1,17 +1,51 @@ package LinkedList; -class MyLinkedList { +import org.jetbrains.annotations.NotNull; - static class Node { - private final int value; +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class MyLinkedList implements Iterable{ + + + @NotNull + @Override + public Iterator iterator() { + return new MyIterator<>(this); + } + + public class MyIterator implements Iterator{ + + int index = 0; + MyLinkedList eMyLinkedList; + + public MyIterator(MyLinkedList eMyLinkedList) { + this.eMyLinkedList = eMyLinkedList; + } + + @Override + public boolean hasNext() { + return eMyLinkedList.length >= index + 1; + } + + @Override + public E next() throws NoSuchElementException { + return eMyLinkedList.get(index++); + } + + } + + class Node { + private final T value; private Node nextNode; private Node prevNode; - public Node(int value) { + public Node(T value) { this.value = value; nextNode = null; prevNode = null; } + } int length; @@ -24,14 +58,14 @@ public MyLinkedList() { endNode = null; } - public int get(int index) { + public T get(int index) { Node head = startNode; - if (index >= length || index < 0) return -1; + if (index >= length || index < 0) return null; //Здесь уже -1 не подходит, на что можно заменить? for (int i = 0; i < index; i++) head = head.nextNode; return head.value; } - public void addAtHead(int val) { + public void addAtHead(T val) { Node add = new Node(val); if (startNode == null) { startNode = add; @@ -45,7 +79,7 @@ public void addAtHead(int val) { length++; } - public void addAtTail(int val) { + public void addAtTail(T val) { Node add = new Node(val); if (startNode == null) { startNode = add; @@ -59,7 +93,7 @@ public void addAtTail(int val) { length++; } - public void addAtIndex(int index, int val) { + public void addAtIndex(int index, T val) { if (index > length || index < 0) return; if (index == 0) { @@ -92,20 +126,16 @@ public void deleteAtIndex(int index) { startNode = null; endNode = null; } else { - Node del = startNode; startNode.nextNode.prevNode = null; startNode = startNode.nextNode; - del = null; } length--; return; } if (index == length - 1) { - Node del = endNode; endNode.prevNode.nextNode = null; endNode = endNode.prevNode; - del = null; length--; return; } @@ -114,7 +144,6 @@ public void deleteAtIndex(int index) { for (int i = 0; i < index; i++) head = head.nextNode; head.prevNode.nextNode = head.nextNode; head.nextNode.prevNode = head.prevNode; - head = null; length--; } } From da03d5bacb5b669a171e3e4778d1eea3c7a1b8c2 Mon Sep 17 00:00:00 2001 From: hasksy Date: Sat, 28 Nov 2020 18:38:01 +0300 Subject: [PATCH 5/5] Added some tests for MyLinkedList class --- src/LinkedList/Main.java | 25 +++++++++++++++++++++++++ src/LinkedList/Student.java | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 src/LinkedList/Main.java create mode 100644 src/LinkedList/Student.java diff --git a/src/LinkedList/Main.java b/src/LinkedList/Main.java new file mode 100644 index 0000000..e44a4b1 --- /dev/null +++ b/src/LinkedList/Main.java @@ -0,0 +1,25 @@ +package LinkedList; + +import java.util.ArrayList; +import java.util.Iterator; + +public class Main { + public static void main(String[] args) { + MyLinkedList listStudents = new MyLinkedList<>(); + listStudents.addAtHead(new Student("Ivanov", 202, 2)); + listStudents.addAtHead(new Student("Petrov", 202, 2)); + listStudents.addAtHead(new Student("Sidorov", 201, 3)); + listStudents.addAtHead(new Student("Guy", 322, 1337)); + + for(Student student: listStudents) { + System.out.println(student); + } + + System.out.println("\n\n"); + Iterator studentIterator = listStudents.iterator(); + while (studentIterator.hasNext()) { + System.out.println(studentIterator.next()); + } + + } +} diff --git a/src/LinkedList/Student.java b/src/LinkedList/Student.java new file mode 100644 index 0000000..08db13c --- /dev/null +++ b/src/LinkedList/Student.java @@ -0,0 +1,35 @@ +package LinkedList; + +public class Student { + + private final String secondName; + private final int group; + private final int subgroup; + + public Student(String secondName, int group, int subgroup) { + this.secondName = secondName; + this.group = group; + this.subgroup = subgroup; + } + + public String getSecondName() { + return secondName; + } + + public int getGroup() { + return group; + } + + public int getSubgroup() { + return subgroup; + } + + @Override + public String toString() { + return "Student{" + + "secondName='" + secondName + '\'' + + ", group=" + group + + ", subgroup=" + subgroup + + '}'; + } +}