Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package linked_list;
- import java.util.Iterator;
- public class LinkedList<E> implements Iterable<E> {
- private Node<E> head;
- private Node<E> tail;
- private int size;
- public int size() {
- return this.size;
- }
- public boolean add(E item) {
- int oldSize = this.size;
- addLast(item);
- return oldSize < this.size;
- }
- public void add(int index, E element) {
- if(index == 0) {
- addFirst(element);
- return;
- } else if(index == size) {
- addLast(element);
- return;
- }
- checkSize(index);
- Node<E> currentNode = head;
- Node<E> newNode = new Node<>(element);
- Node<E> previousNode = currentNode;
- for (int i = 0; i <= index; i++) {
- if(i == index) {
- currentNode.setNext(currentNode.getNext());
- newNode.setNext(currentNode);
- previousNode.setNext(newNode);
- size++;
- return;
- }
- previousNode = currentNode;
- currentNode = currentNode.getNext();
- }
- }
- public void addFirst(E element) {
- Node<E> oldHead = this.head;
- this.head = new Node<>(element);
- this.head.setNext(oldHead);
- if (this.tail == null) {
- this.tail = this.head;
- this.head.setNext(tail);
- this.tail.setNext(null);
- }
- this.size++;
- }
- public void addLast(E element) {
- Node<E> oldTail = this.tail;
- this.tail = new Node<>(element);
- this.tail.setNext(null);
- if (oldTail != null) {
- oldTail.setNext(tail);
- } else if (this.head == null) {
- this.head = this.tail;
- }
- this.size++;
- }
- public E remove() {
- return removeFirst();
- }
- public E remove(int index) {
- checkSize(index);
- Node<E> elementToRemove = head;
- Node<E> previousNode = elementToRemove;
- for (int i = 0; i < index; i++) {
- previousNode = elementToRemove;
- elementToRemove = elementToRemove.getNext();
- }
- if (elementToRemove == head) {
- head = elementToRemove.getNext();
- } else if (elementToRemove == tail) {
- tail = previousNode;
- tail.setNext(null);
- } else {
- previousNode.setNext(elementToRemove.getNext());
- }
- size--;
- return elementToRemove.getValue();
- }
- public boolean remove(E element) {
- checkSize();
- int indexOfElement = 0;
- Node<E> currentNode = head;
- for (int i = 0; i < size; i++) {
- if(currentNode.getValue() == element) {
- indexOfElement = i;
- remove(indexOfElement);
- return true;
- }
- currentNode = currentNode.getNext();
- }
- return false;
- }
- public E removeFirst() {
- checkSize();
- E oldHeadValue = this.head.getValue();
- Node<E> oldHead = this.head;
- this.head = oldHead.getNext();
- size--;
- return oldHeadValue;
- }
- public E removeLast() {
- checkSize();
- E oldTailValue = this.tail.getValue();
- Node<E> currentNode = this.head;
- Node<E> newTail = null;
- while (true) {
- if (currentNode.getNext() != null) {
- newTail = currentNode;
- } else {
- break;
- }
- currentNode = currentNode.getNext();
- }
- this.tail = newTail;
- if (this.tail != null) {
- this.tail.setNext(null);
- }
- size--;
- return oldTailValue;
- }
- @Override
- public Iterator<E> iterator() {
- return new LinkedListIterator();
- }
- private final class LinkedListIterator implements Iterator<E> {
- int count = 0;
- @Override
- public boolean hasNext() {
- return count < size;
- }
- @Override
- public E next() {
- count++;
- E node = head.getValue();
- head = head.getNext();
- return node;
- }
- }
- private void checkSize() {
- if (this.size == 0) {
- throw new IllegalArgumentException("Size must be positive");
- }
- }
- private void checkSize(int index) {
- checkSize();
- if (size < index) {
- throw new IndexOutOfBoundsException("Index out of range!");
- }
- }
- @Override
- public String toString() {
- Node<E> node = head;
- final StringBuilder sb = new StringBuilder("LinkedList{");
- while (node != null) {
- sb.append(node.getValue());
- if(node.getNext() != null) {
- sb.append(", ");
- }
- node = node.getNext();
- }
- sb.append('}');
- return sb.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement