Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Spliterator;
- import java.util.function.Consumer;
- import java.util.Iterator;
- public class LinkedListImpl<T> implements Iterable<T> {
- private Node<T> head;
- private Node<T> tail;
- private int count;
- public LinkedListImpl() {
- this.head = null;
- this.tail = null;
- this.count = 0;
- }
- //Добавление элемента в конец списка
- public void add(T num) {
- if (!isEmpty()) {
- Node<T> temp = tail;
- tail = new Node<T>(num);
- temp.setNext(tail);
- tail.setPrevious(temp);
- } else {
- tail = new Node<T>(num);
- head = tail;
- }
- count++;
- }
- //Добавление элемента в начало списка
- public void addFirst(T num) {
- if (!isEmpty()) {
- Node<T> temp = head;
- head = new Node<T>(num);
- head.setNext(temp);
- temp.setPrevious(head);
- } else {
- tail = new Node<T>(num);
- head = tail;
- }
- count++;
- }
- //Удаление элемента по значению
- public boolean remove(T value) {
- boolean check = false;
- Node<T> previous = null;
- Node<T> current = head;
- while (current != null) {
- if (current.getValue().equals(value)) {
- check = true;
- if (count == 1) {
- head = null;
- tail = null;
- } else if (current.equals(head)) {
- head = head.getNext();
- head.setPrevious(null);
- } else if (current.equals(tail)) {
- tail = previous;
- tail.setNext(null);
- } else {
- previous.setNext(current.getNext());
- current.getNext().setPrevious(previous);
- }
- count--;
- break;
- }
- previous = current;
- current = previous.getNext();
- }
- if (!check) {
- System.out.println("Не удалось найти данный элемент");
- }
- return check;
- }
- //Удаление элемента по индексу
- public boolean removeByIndex(int index) {
- boolean check = false;
- Node<T> previous = null;
- Node<T> current = head;
- int i = 0;
- if (index <= count - 1) {
- check = true;
- if (count == 1) {
- head = null;
- tail = null;
- } else if (index == 0) {
- head = head.getNext();
- head.setPrevious(null);
- } else {
- while (i != index) {
- previous = current;
- current = current.getNext();
- i++;
- }
- if (current.equals(tail)) {
- tail = previous;
- tail.setNext(null);
- } else {
- previous.setNext(current.getNext());
- current.getNext().setPrevious(previous);
- }
- }
- }
- if (!check) {
- System.out.println("Не удалось найти элемент с данным индексом");
- }
- return check;
- }
- //Вывод списка
- public void print() {
- Node<T> current = head;
- while (current.getNext() != null) {
- System.out.print(current.getValue() + "->");
- current = current.getNext();
- }
- System.out.println(current.getValue());
- }
- //Получение элемента по индексу
- public T get(int index) {
- if (index > count - -1) {
- System.out.println("Элемент с данным индексом не найден");
- return null;
- } else {
- Node<T> current = head;
- int i = 0;
- while (i != index) {
- current = current.getNext();
- i++;
- }
- return (T) current.getValue();
- }
- }
- //Удаление всех элементов из списка
- public void clear() {
- head = null;
- tail = null;
- count = 0;
- }
- //Проверка наличия элементов
- public boolean isEmpty() {
- return count == 0;
- }
- @Override
- public Iterator iterator() {
- return new LinkedListIterator();
- }
- @Override
- public void forEach(Consumer action) {
- Node current = head;
- while (current.getNext() != null) {
- action.accept(current.getValue());
- current = current.getNext();
- }
- action.accept(current.getValue());
- }
- @Override
- public Spliterator spliterator() {
- return null;
- }
- private class LinkedListIterator implements Iterator<T> {
- private Node<T> currentNode;
- private int currentIndex;
- public LinkedListIterator() {
- this.currentIndex = 0;
- this.currentNode = new Node<T>(null);
- this.currentNode.setNext(head);
- }
- @Override
- public boolean hasNext() {
- return currentIndex < count ;
- }
- @Override
- public T next() {
- currentNode = currentNode.getNext();
- currentIndex++;
- return currentNode.value;
- }
- }
- public MyStream<T> stream() {
- return new MyStream<T>(this);
- }
- public static class Node<E> {
- private E value;
- private Node<E> next;
- private Node<E> previous;
- public Node(E value) {
- this.value = value;
- }
- public E getValue() {
- return value;
- }
- public void setValue(E value) {
- this.value = value;
- }
- public Node getNext() {
- return next;
- }
- public void setNext(Node next) {
- this.next = next;
- }
- public void setPrevious(Node previous) {
- this.previous = previous;
- }
- public Node getPrevious() {
- return previous;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement