数据结构-表-双向链表(Java语言)

 详细的代码可见github:

https://github.com/AbitGo/myClassWork/tree/master/workspace_ds

双向链表所需要实现的接口功能。

package com.company.ch2.interfaceFile;
 
public interface SequenceTableInterface {
    void clear();
    boolean isEmpty();
    int getLenght();
    Object get(int i) throws Exception;
    boolean insert(int index,Object x) throws Exception;
    boolean remove(int i);
    int indexOf(Object x);
    void display();
}

双向链表主要代码

package com.company.ch2.DuLinkTable;

import com.company.ch2.SequenceTableInterface;

public class DuLinkList implements SequenceTableInterface {
    DuNode head;

    public DuLinkList() {
        head = new DuNode();
        head.prior = head;
        head.next = head;
    }

    @Override
    public void clear() {
        this.head.next = head;
        this.head.prior = head;
    }

    @Override
    public boolean isEmpty() {
        if (head.next == head) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public int getLenght() {
        DuNode duNode = head.next;
        int len = 0;
        while (duNode != null) {
            len++;
            duNode = duNode.next;
        }
        return len;
    }

    @Override
    public Object get(int index) throws Exception {
        DuNode duNode = head;
        int i = -1;
        //达到需要插入的前缀
        while (!duNode.next.equals(head) && i < index-1) {
            duNode = duNode.next;
            i++;
        }

        if (i < index-1) {
            System.out.println("查找位置不合法");
            return null;
        } else {
            return duNode.data;
        }
    }

    @Override
    //这个函数有问题,就是不能在空表之前插入
    public boolean insert(int index, Object x) throws Exception {
        DuNode duNode = head;
        int i = -1;
        //当这个是只有头节点的时候只需要直接插入即可
        //为了挽救上面提到的问题
        if (duNode.next.equals(duNode)) {
            DuNode temp = new DuNode(x);
            temp.next = duNode.next;
            temp.prior = duNode;
            duNode.next = temp;
            duNode.prior = duNode;
            return true;
        }
        //达到需要插入的前缀
        while (!duNode.next.equals(head) && i < index-1) {
            duNode = duNode.next;
            i++;
        }

        if (i < index-1) {
            System.out.println("插入位置不合法");
            return false;
        } else {
            DuNode temp = new DuNode(x);

            //代码分为四部,分别是
            //duNode=temp=p三个节点之间的前驱动后驱代换
            temp.next = duNode.next;
            temp.prior = duNode;
            duNode.next.prior = temp;

            duNode.next = temp;
            return true;
        }
    }


    @Override
    public boolean remove(int index) {
        DuNode duNode = head;
        int i = -1;
        //达到需要插入的前缀
        while (!duNode.next.equals(head) && i<index-1) {
            duNode = duNode.next;
            i++;
        }
        if (i < index-1) {
            System.out.println("删除位置不合法");
            return false;
        } else {
            duNode.next.prior = duNode;
            duNode.next = duNode.next.next;
            return true;
        }
    }

    @Override
    public int indexOf(Object x) {
        DuNode duNode = head;
        int i = 0;
        //达到需要插入的前缀
        while (!duNode.next.equals(head)) {
            duNode = duNode.next;
            if(duNode.data.equals(x))
                return i;
            i++;
        }
        return -1;
    }

    @Override
    public void display() {
        DuNode duNode = head.next;
        while (!duNode.equals(head)) {

            System.out.print(duNode.data + " ");
            duNode = duNode.next;
        }
        System.out.println();
    }
}

成员类

package com.company.ch2.DuLinkTable;

public class DuNode {
    public Object data;
    public DuNode next;
    public DuNode prior;

    public DuNode(){
        this.data = null;
        this.next = null;
        this.prior = null;
    }

    public DuNode(Object data){
        this.data = data;
        this.prior  = null;
        this.next = null;
    }

    public DuNode(Object data,DuNode next,DuNode prior){
        this.data = data;
        this.next = next;
        this.prior = prior;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public DuNode getNext() {
        return next;
    }

    public void setNext(DuNode next) {
        this.next = next;
    }

    public DuNode getPrior() {
        return prior;
    }

    public void setPrior(DuNode prior) {
        this.prior = prior;
    }
}

 测试类:

package com.company.ch2.DuLinkTable;

public class DuLinkListTest {
    public static void main(String[] args) throws Exception {
        DuLinkList duLinkList = new DuLinkList();
        System.out.println("----------判断空操作:开始----------");
        if(duLinkList.isEmpty()==true){
            System.out.println("the link is empty");
        }else{
            System.out.println("the link is not empty");
        }
        System.out.println("----------判断空操作:结束----------");


        System.out.println("----------插入操作:开始----------");
        duLinkList.insert(0,0);
        duLinkList.display();
        duLinkList.insert(1,1);
        duLinkList.insert(2,2);
        //插入错误
        duLinkList.insert(5,4);
        duLinkList.display();
        System.out.println("----------插入操作:结束----------");


        System.out.println("----------删除操作:开始----------");

        duLinkList.remove(1);
        duLinkList.display();
        duLinkList.remove(100);
        duLinkList.display();
        System.out.println("----------删除操作:结束----------");

        System.out.println("----------查找操作:开始----------");

        Object result = duLinkList.get(1);
        if(result!=null){
            System.out.println("data:"+result);
        }

        System.out.println("----------查找操作:结束----------");

        System.out.println("----------索引操作:开始----------");
        int xxx= duLinkList.indexOf(2);
        if(xxx!=-1){
            System.out.println("indexOf:"+xxx);
        }

        System.out.println("----------索引操作:结束----------");

        System.out.println("----------清空操作:开始----------");
        duLinkList.clear();
        duLinkList.display();
        System.out.println("----------清空操作:结束----------");

    }
}

测试结果:

----------判断空操作:开始----------
the link is empty
----------判断空操作:结束----------
----------插入操作:开始----------
0 
插入位置不合法
0 1 2 
----------插入操作:结束----------
----------删除操作:开始----------
0 2 
删除位置不合法
0 2 
----------删除操作:结束----------
----------查找操作:开始----------
data:0
----------查找操作:结束----------
----------索引操作:开始----------
indexOf:1
----------索引操作:结束----------
----------清空操作:开始----------

----------清空操作:结束----------
发布了84 篇原创文章 · 获赞 39 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/Abit_Go/article/details/104137854