Singly LinkedList - Java Data Structure

Java examples for Data Structure:List

Description

Singly LinkedList

Demo Code

package com.company.stucts.list;

import java.util.Iterator;

@SuppressWarnings({ "Duplicates", "WeakerAccess" })
public class SinglyLinkedList<T> implements Iterable<T> {
    private Node<T> head;
    private int size;

    public SinglyLinkedList() {
        head = null;/*from www.  j a  v  a 2s.  c  o  m*/
    }

    public void addFront(T value) {
        if (head == null) {
            head = new Node<T>(value);
            return;
        }

        Node<T> newNode = new Node<>(value);
        newNode.next = head;
        head = newNode;

        size++;
    }

    public void addBack(T value) {
        addAfter(size, value);
    }

    public void addAfter(int position, T value) {
        if (position > size) {
            throw new IllegalStateException("Position is out of list size!");
        }

        Node<T> node = head;
        for (int i = 0; i < position; i++) {
            node = node.next;
        }

        Node<T> newNode = new Node<T>(value);
        newNode.next = node.next;
        node.next = newNode;
        size++;
    }

    public void addAt(int position, T value) {
        if (position > size) {
            throw new IllegalStateException("Position is out of list size!");
        }

        Node<T> node = head;
        for (int i = 0; i < position; i++) {
            node = node.next;
        }

        Node<T> newNode = new Node<T>(value);
        newNode.next = node.next;
        node.next = newNode;

        swapNodeValue(node, newNode);
        size++;
    }

    public void removeFront() {
        Node<T> nextNode = head.next;
        head.next = null;
        head = nextNode;
    }

    public void removeBack() {
        removeAt(size);
    }

    public void removeAfter(int position) {
        if (position > size - 1) {
            throw new IllegalStateException("Position is out of list size!");
        }

        Node<T> node = head;
        for (int i = 0; i < position; i++) {
            node = node.next;
        }

        Node<T> removeNode = node.next;
        node.next = removeNode.next;
        removeNode.next = null;

        size--;
    }

    public void removeAt(int position) {
        if (position > size) {
            throw new IllegalStateException("Position is out of list size!");
        }

        Node<T> node = head;
        Node<T> parentNode = null;
        for (int i = 0; i < position; i++) {
            parentNode = node;
            node = node.next;
        }

        if (node.next != null) {
            Node<T> nextNode = node.next;
            node.next = nextNode.next;
            swapNodeValue(node, nextNode);
            nextNode.next = null;
        } else {
            if (parentNode != null) {
                parentNode.next = null;
            }
        }

        size--;
    }

    public void reverseList() {
        Node<T> currentNode = head;
        Node<T> prevNode = null;

        while (currentNode != null) {
            Node<T> nextNode = currentNode.next;
            currentNode.next = prevNode;
            prevNode = currentNode;
            currentNode = nextNode;
        }

        head = prevNode;
    }

    private void swapNodeValue(Node<T> node1, Node<T> node2) {
        T tmp = node1.value;
        node1.value = node2.value;
        node2.value = tmp;
    }

    public boolean isEmpty() {
        return head == null;
    }

    public int size() {
        return size;
    }

    @Override
    public Iterator<T> iterator() {
        return new Iterator<T>() {
            private Node<T> node = head;

            @Override
            public boolean hasNext() {
                return node != null;
            }

            @Override
            public T next() {
                T value = node.value;
                node = node.next;
                return value;
            }
        };
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder("List: ");
        for (T v : this) {
            builder.append("[").append(v).append("]");
        }
        return builder.toString();
    }

    private static class Node<T> {
        private T value;
        private Node<T> next;

        Node(T value) {
            this.value = value;
        }
    }

    /**
     * Test main method
     */
    public static void main(String[] args) {
        SinglyLinkedList<Integer> list = new SinglyLinkedList<>();
        list.addFront(3);
        list.addFront(2);
        list.addFront(1);

        list.addAfter(2, 55);
        System.out.println(list);

        list.reverseList();
        list.addBack(13);

        System.out.println(list);
    }
}

Related Tutorials