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
25 changes: 25 additions & 0 deletions src/LinkedList/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package LinkedList;

import java.util.ArrayList;
import java.util.Iterator;

public class Main {
public static void main(String[] args) {
MyLinkedList<Student> 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<Student> studentIterator = listStudents.iterator();
while (studentIterator.hasNext()) {
System.out.println(studentIterator.next());
}

}
}
149 changes: 149 additions & 0 deletions src/LinkedList/MyLinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package LinkedList;

import org.jetbrains.annotations.NotNull;

import java.util.Iterator;
import java.util.NoSuchElementException;

public class MyLinkedList<T> implements Iterable<T>{


@NotNull
@Override
public Iterator<T> iterator() {
return new MyIterator<>(this);
}

public class MyIterator<E> implements Iterator<E>{

int index = 0;
MyLinkedList<E> eMyLinkedList;

public MyIterator(MyLinkedList<E> 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--;
}
}
35 changes: 35 additions & 0 deletions src/LinkedList/Student.java
Original file line number Diff line number Diff line change
@@ -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 +
'}';
}
}