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/MyLinkedList.java b/src/LinkedList/MyLinkedList.java new file mode 100644 index 0000000..7e9ce4e --- /dev/null +++ b/src/LinkedList/MyLinkedList.java @@ -0,0 +1,149 @@ +package LinkedList; + +import org.jetbrains.annotations.NotNull; + +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(T value) { + this.value = value; + nextNode = null; + prevNode = null; + } + + } + + int length; + Node startNode; + Node endNode; + + public MyLinkedList() { + length = 0; + startNode = null; + endNode = null; + } + + public T get(int index) { + Node head = startNode; + if (index >= length || index < 0) return null; //Здесь уже -1 не подходит, на что можно заменить? + for (int i = 0; i < index; i++) head = head.nextNode; + return head.value; + } + + public void addAtHead(T val) { + Node add = new Node(val); + if (startNode == null) { + startNode = add; + endNode = add; + length++; + return; + } + add.nextNode = startNode; + startNode.prevNode = add; + startNode = add; + length++; + } + + public void addAtTail(T val) { + Node add = new Node(val); + if (startNode == null) { + startNode = add; + endNode = add; + length++; + return; + } + add.prevNode = endNode; + endNode.nextNode = add; + endNode = add; + length++; + } + + public void addAtIndex(int index, T val) { + if (index > length || index < 0) return; + + if (index == 0) { + addAtHead(val); + return; + } + + if (index == length) { + addAtTail(val); + return; + } + + Node add = new Node(val); + + Node head = startNode; + for (int i = 0; i < index; i++) head = head.nextNode; + + add.prevNode = head.prevNode; + add.nextNode = head; + head.prevNode.nextNode = add; + head.prevNode = add; + length++; + } + + public void deleteAtIndex(int index) { + if (index >= length || index < 0) return; + + if (index == 0) { + if (length == 1) { + startNode = null; + endNode = null; + } else { + startNode.nextNode.prevNode = null; + startNode = startNode.nextNode; + } + length--; + return; + } + + if (index == length - 1) { + endNode.prevNode.nextNode = null; + endNode = endNode.prevNode; + 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; + length--; + } +} 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 + + '}'; + } +}