集册 Java实例教程 单一链接列表

单一链接列表

欢马劈雪     最近更新时间:2020-01-02 10:19:05

555
单一链接列表

package com.company.stucts.list;//时代Java公众号 提 供


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;

    }


    public void addFront(T value) {

        if (head == null) {

            head = new Node<T>(value);

            return;/* 来 自 n o w j a v a . c o m*/

        }


        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: ");

 
展开阅读全文