美文网首页
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();
    }

}

验证结果:

相关文章

  • 双向链表&双向循环链表

    链表分为:单链表、单向循环链表、双向链表、双向循环链表本节主要说明:双向链表、双向循环链表 定义结点 一、双向链表...

  • 0x05双向循环链表

    1 双向循环链表创建 2 双向循环链表插入元素 3 遍历双向循环链表 4双向循环链表删除结点

  • 06-双向循环链表

    完整代码: 验证结果:

  • 双向链表

    双向链表的结构 既然单链表有循环链表,双向链表当然也有双向循环链表,如下图: 问: 双向链表中某个节点p的后继节点...

  • 线性表--链式存储结构--双向链表

    双向链表 一、双向链表结构 双向链表结点结构 既然单链表可以有循环链表,那么双向链表当然也可以有。 由于这是双向链...

  • 数据结构与算法之数组与链表

    线性表包括数组,链表(单链表,双向链表,循环链表,双向循环链表,静态链表),栈(顺序栈,链式栈),队列(普通队列,...

  • 链表

    链表 缺点:查找复杂有点:定点删除/插入元素 单链表 双向链表 循环链表 双向循环链表 数组与链表的区别 数据存储...

  • 数据结构与算法之栈与队列

    线性表包括数组,链表(单链表,双向链表,循环链表,双向循环链表,静态链表),栈(顺序栈,链式栈),队列(普通队列,...

  • 常见的数据结构

    常见的数据结构有: 数组 链表单链表、双向链表、循环链表、双向循环链表、静态链表 栈顺序栈、链式栈 队列普通队列、...

  • 33_双向循环链表的实现

    关键词:双向循环链表 0. 课程目标 使用Linux内核链表实现双向循环链表 template < typenam...

网友评论

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

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