3.1、双向循环链表(java实现)

1、创建节点类

public class CNode<T> {
    public CNode prev;
    public CNode next;
    public T data;

    public CNode() {
    }

    public CNode(T data) {
        this.data = data;
    }
}

2、实现类

public class DoubleLinkNode<T> {

    /**
     * 初始化
     *
     * @param head
     */
    public void init(CNode head) {
        head.prev = head;
        head.next = head;
    }

    /**
     * 插入
     *
     * @param head:插入对应链表
     * @param index:插入的位置
     * @param element:    对应位置插入的元素
     */
    public void insert(CNode head, int index, T element) {
        CNode temp = head;
        CNode<T> newnode = new CNode<>(element);
        int j = -1;
        while (temp.next != head && j < index - 1) {
            temp = temp.next;
            j++;
        }

        if (j != index - 1) {
            System.out.println("插入的下标有问题");
            return;
        }
        newnode.next = temp.next;//---->
        temp.next.prev = newnode;//<----
        newnode.prev = temp;//---->
        temp.next = newnode;//<---
    }

    /**
     * 删除
     * @param head:所要删除的链表
     * @param index :删除的元素下标
     */
    public void delete(CNode head, int index) {
        CNode temp = head;
        int j = -1;
        if (temp.next == head) {
            System.out.println("链表为空,没有元素可删除");
            return;
        }
        while (temp.next.next != head && j < index - 1) {
            temp = temp.next;
            j++;
        }

        if (j != index - 1) {
            System.out.println("删除元素位置下标出错");
            return;
        }
        temp.next = temp.next.next;
        temp = temp.next.next.prev;
    }

    public void indexGetElement(CNode head,int index) {
        CNode temp = head;
        int j = -1;
        if (temp.next == head){
            System.out.println("为空,无法获取你要的值");
            return;
        }
        while (temp.next != head && j< index){
            temp = temp.next;
            j++;
        }
        if (j != index){
            System.out.println("您给的下标有问题");
            return;
        }
        System.out.println("您要获取的值:"+temp.data);
    }

    /**
     * 求当前元素个数
     *
     * @param head
     */
    public void getLength(CNode head) {
        CNode temp = head;
        int length = 0;
        if (temp.next == head) {
            System.out.println("链表为空,长度为0");
            return;
        }
        while (temp.next != head) {
            temp = temp.next;
            length++;
        }
        System.out.println("长度为: " + length);
    }

    /**
     * 打印
     *
     * @param head
     */
    public void print(CNode head) {
        CNode temp = head;
        if (temp.next == head) {
            System.out.println("链表为空,没有可打印元素");
            return;
        }
        while (temp.next != head) {
            temp = temp.next;
            System.out.print(temp.data + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        CNode<Integer> cNode = new CNode<>();
        DoubleLinkNode doubleLinkNode = new DoubleLinkNode();
        doubleLinkNode.init(cNode);
        doubleLinkNode.indexGetElement(cNode,0);

        doubleLinkNode.insert(cNode, 0, 8);
        doubleLinkNode.insert(cNode, 0, 7);
        doubleLinkNode.getLength(cNode);
        doubleLinkNode.print(cNode);
        for (int i = 0; i < 5; i++) {
            doubleLinkNode.insert(cNode, i, i);
        }

        doubleLinkNode.print(cNode);
        doubleLinkNode.getLength(cNode);

        doubleLinkNode.delete(cNode,6);
        doubleLinkNode.print(cNode);
        doubleLinkNode.getLength(cNode);

        doubleLinkNode.indexGetElement(cNode,5);
    }
}

3、实验结果

为空,无法获取你要的值
长度为: 2
7 8 
0 1 2 3 4 7 8 
长度为: 7
0 1 2 3 4 7 
长度为: 6
您要获取的值:7

 

posted @ 2019-07-17 22:09  编程小白1024  阅读(414)  评论(0编辑  收藏  举报