美文网首页
06-双向循环链表

06-双向循环链表

作者: weyan | 来源:发表于2021-07-22 15:36 被阅读0次
    //主要改变的操作是add和remove
    public void add(int index, E element) {
            // TODO Auto-generated method stub
            rangeCheekForAdd(index);
            if (index == size) {
                Node<E> oldLast = last;
                last = new Node<>(oldLast, element, first);
                if (oldLast == null) {
                    first = last;
                    first.next = first;
                    first.prev = first;
                }else {
                    oldLast.next = last;
                    first.prev = last;
                }
                
            }else {
                Node<E> next = node(index);
                Node<E> prev = next.prev;
                Node<E> node = new Node<>(prev, element, next);
                next.prev = node;
                prev.next = node;
                if (next == first) { //index == 0
                    first = node;
                }
            }
            size ++;
        }
    

    remove
    public E remove(int index) {
            // TODO Auto-generated method stub
            rangeCheck(index);
            Node<E> node = first;
            if (size == 1) {
                first = null;
                last = null;
            }else {
                node = node(index);
                Node<E> prev = node.prev;
                Node<E> next = node.next;
                prev.next = next;
                next.prev = prev;
                if (node == first) { // index == 0,删除第一个
                    first = next;
                }
                
                if (node == last) { //index == size - 1,删除最后一个
                    last = prev;
                }
            }
            size --;
            return node.element;
        }
    

    完整代码:

    package com.weyan;
    
    public class CircleLinkedList<E> implements List<E> {
        private static final int ELEMENT_NOT_FOUND = -1;
        private int size;
        private Node<E> first;
        private Node<E> last;
        
        //内部类
        private static class Node<E> {
            E element;
            Node<E> prev;
            Node<E> next;
            //构造函数
            public Node(Node<E> prev,E element, Node<E> next) {
                super();
                this.element = element;
                this.next = next;
                this.prev = prev;
            }
            
        }
    
        @Override
        public void clear() {
            // TODO Auto-generated method stub
            size = 0;
            first = null;
            last = null;
        }
    
        @Override
        public int size() {
            // TODO Auto-generated method stub
            return size;
        }
    
        @Override
        public boolean isEmpty() {
            // TODO Auto-generated method stub
            return size == 0;
        }
    
        @Override
        public boolean contains(E element) {
            // TODO Auto-generated method stub
            return indexOf(element) != ELEMENT_NOT_FOUND;
        }
    
        @Override
        public void add(E element) {
            // TODO Auto-generated method stub
            add(size, element);
        }
    
        @Override
        public E get(int index) {
            // TODO Auto-generated method stub
            return node(index).element;
        }
    
        @Override
        public E set(int index, E element) {
            // TODO Auto-generated method stub
            
            Node<E> node = node(index);
            E old = node.element;
            node.element = element;
            return old;
        }
    
        @Override
        public void add(int index, E element) {
            // TODO Auto-generated method stub
            rangeCheekForAdd(index);
            if (index == size) {
                Node<E> oldLast = last;
                last = new Node<>(oldLast, element, first);
                if (oldLast == null) {
                    first = last;
                    first.next = first;
                    first.prev = first;
                }else {
                    oldLast.next = last;
                    first.prev = last;
                }
                
            }else {
                Node<E> next = node(index);
                Node<E> prev = next.prev;
                Node<E> node = new Node<>(prev, element, next);
                next.prev = node;
                prev.next = node;
                if (next == first) { //index == 0
                    first = node;
                }
            }
            size ++;
        }
    
        @Override
        public E remove(int index) {
            // TODO Auto-generated method stub
            rangeCheck(index);
            Node<E> node = first;
            if (size == 1) {
                first = null;
                last = null;
            }else {
                node = node(index);
                Node<E> prev = node.prev;
                Node<E> next = node.next;
                prev.next = next;
                next.prev = prev;
                if (node == first) { // index == 0,删除第一个
                    first = next;
                }
                
                if (node == last) { //index == size - 1,删除最后一个
                    last = prev;
                }
            }
            size --;
            return node.element;
        }
    
        @Override
        public int indexOf(E element) {
            // TODO Auto-generated method stub
            Node<E> node = first;
            if (element == null) {
                for (int i = 0; i < size; i++) {
                    if (node.element == null) return i;
                    node = node.next;
                }
            }else {
                for (int i = 0; i < size; i++) {
                    if (element.equals(node.element)) return i;
                    node = node.next;
                }
            }
            return ELEMENT_NOT_FOUND;
        }
        
        /**
         * 获取index位置对应的节点对象
         * size >> 1  等于size/2
         * size << 2  等于size*2
         */
        private Node<E> node(int index) {
            rangeCheck(index);
            
            if (index < (size >> 1)) {
                Node<E> node = first;
                for (int i = 0; i < index; i++) {
                    node = node.next;
                }
                return node;
            }else {
                Node<E> node = last;
                for (int i = size - 1; i > index; i--) {
                    node = node.prev;
                }
                return node;
            }
        }
        
        private void outOfBounds(int index) {
            throw new IndexOutOfBoundsException("Index:" + index + ",size:" + size);
        }
        
        private void rangeCheck(int index) {
            // TODO Auto-generated method stub
            if (index < 0 || index >= size) {
                outOfBounds(index);
            }
        }
        
        public void rangeCheekForAdd(int index) {
            // TODO Auto-generated constructor stub
            if (index < 0 || index > size) {
                outOfBounds(index);
            }
        }
        
        @Override
        public String toString() {
            // TODO Auto-generated method stub
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("size=").append(size).append(",[");
            Node<E> node = first;
            for (int i = 0; i < size; i++) {
                if (i != 0) {
                    stringBuilder.append(", ");
                }
                stringBuilder.append(node.element);
                node = node.next;
            }
            stringBuilder.append("]");
            return stringBuilder.toString();
        }
    
    }
    
    

    验证结果:

    相关文章

      网友评论

          本文标题:06-双向循环链表

          本文链接:https://www.haomeiwen.com/subject/evcodltx.html