码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构双向链表 DoublyLinkedList

时间:2020-07-10 19:19:46      阅读:52      评论:0      收藏:0      [点我收藏+]

标签:data   over   oid   row   asn   链表   数据   ext   nod   

package datastructures.linked;

import java.util.Iterator;

/**
 * @author warriorg
 */
public class DoublyLinkedList<T> implements Iterable<T> {
    private int size = 0;
    private Node<T> head = null;
    private Node<T> tail = null;

    class Node<T> {
        T data;
        Node<T> prev;
        Node<T> next;

        Node(T data, Node<T> prev, Node<T> next) {
            this.data = data;
            this.prev = prev;
            this.next = next;
        }

        @Override
        public String toString() {
            return "[" + prev + "|" + data + "|" + next + "]";
        } 
    }

    public T peek() {
        return head == null ? null : head.data;
    }


    public Boolean add(T item) {
        addLast(item); 
        return true; 
    }

    public void addFirst(T item) {
        final Node<T> first = head;
        final Node<T> node = new Node<>(item, null, head);
        head = node;
        if (first == null) {
            tail = node;
        } else {
            first.prev = node;
        }
        size++;
    }

    public void addLast(T item) {
        final Node<T> prev = tail; 
        final Node<T> node = new Node<>(item, prev, null);
        tail = node;
        if (prev == null) {
            head = node;
        } else {
            prev.next = node;
        }
        size++;
    }

    public void clear() {
        for (Node<T> curr = head; curr != null;) {
            Node<T> next = curr.next;
            curr.data = null;
            curr.prev = null;
            curr.next = null;
            curr = next;
        }

        head = tail = null;
        size = 0;
    }

    public T get(int index) {
        checkElementIndex(index);

        Node<T> curr = head;
        for (int i = 0; i < size; i++) {
            if (index == i) {
                return curr.data;
            }
            curr = curr.next;            
        }
        return null;
    }

    public boolean remove(T item) {
        return true;
    }

    public int size() {
        return size;
    }

    private void checkElementIndex(int index) {
        if (!(index > 0 && index < size)) {
           throw new IndexOutOfBoundsException(); 
        }
    }


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

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

            @Override
            public T next() {
                T data = curr.data;
                curr = curr.next;
                return data;
            }

            @Override
            public void remove() {

            }
        };
    }
}

数据结构双向链表 DoublyLinkedList

标签:data   over   oid   row   asn   链表   数据   ext   nod   

原文地址:https://www.cnblogs.com/warrior/p/13280623.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!