Doubly LinkedList - Java Data Structure

Java examples for Data Structure:List

Description

Doubly LinkedList

Demo Code

package com.company.stucts.list;

import java.util.Iterator;

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

    public void addBack(T value) {
        if (tail == null) {
            head = tail = new Node<T>(value);
            size++;//from   w w  w .  ja v  a 2 s  . c o  m
            return;
        }

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

        size++;
    }

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

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

    public void removeFront() {
        if (size == 0) {
            throw new IllegalStateException("List is empty");
        }

        Node<T> newHead = head.next;
        newHead.prev = null;
        head.next = null;
        head = newHead;
    }

    public void removeBack() {
        if (size == 0) {
            throw new IllegalStateException("List is empty");
        }

        Node<T> newTail = tail.prev;
        newTail.next = null;
        tail.prev = null;
        tail = newTail;
    }

    public T popFront() {
        if (size == 0) {
            throw new IllegalStateException("List is empty");
        }

        Node<T> newHead = head.next;
        newHead.prev = null;
        head.next = null;
        T value = head.value;
        head = newHead;
        return value;
    }

    public void reverseList() {
        Node<T> node = tail;
        while (node != null) {
            swapLink(node);
            node = node.next;
        }

        Node<T> tmp = head;
        head = tail;
        tail = tmp;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    private void swapLink(Node<T> node) {
        Node<T> tmp = node.next;
        node.next = node.prev;
        node.prev = tmp;
    }

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

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

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

    @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;
            }
        };
    }

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

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

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

        builder.append("\nList reversed order:");
        for (T v : reversedIterator()) {
            builder.append("[").append(v).append("]");
        }

        return builder.toString();
    }

    /**
     * Test main method
     */
    public static void main(String[] args) {
        DoublyLinkedList<Integer> list = new DoublyLinkedList<>();
        list.addBack(1);
        list.removeBack();
        System.out.println(list);
    }
}

Related Tutorials