此系列文章作为学校实验记录,若文中内容有误,请大家指出,谢谢

实验要求

1.建立一个空表。
2.在第i个位置插入新的元素x。
3.删除第i个位置上的元素。
4.取第i个位置上的元素。
5.返回元素x第一次出现在双向循环链表中的位置号。
6.求双向循环链表的长度,即元素个数。
7.输出双向循环链表中所有的元素值。
8.实现双向循环链表的就地逆置。

源代码

package keshe;

import java.util.*;

class DDNode {
    public Object data;
    public DDNode next;
    public DDNode pre;

    public DDNode(Object data, DDNode next, DDNode pre) {
        this.data = data;
        this.next = next;
        this.pre = pre;
    }

    public DDNode(Object data) {
        this(data,null,null);
    }

    public DDNode() {
        this(null,null,null);
    }
}

public class DDLinkList {
    public DDNode head;
    public DDNode tail;

    public DDLinkList() {
        head = new DDNode();
        head.next = head;
        head.pre = head;
    }
    public DDLinkList(int n) throws Exception {
        this();
        create(n);
    }

    public void create(int n) throws Exception {
        System.out.println("请输入链表元素的值:");
        Scanner sc = new Scanner(System.in);
        for (int j = 0; j < n; j++) {
            insert(length(), sc.next());
        }
    }

    public int length() {
        DDNode p = head.next;
        int length = 0;
        while(p != head) {
            p = p.next;
            ++length;
        }
        return length;
    }

    public void insert(int i, Object x) throws Exception {
        if(i==0) {
            DDNode s = new DDNode(x);
            s.next = head.next;
            head.next.pre = s;
            head.next = s;
            s.pre = head;
            updateTail();
        } else {
            DDNode p = head.next;
            int j = 0;
            while(p != head && j < i-1) {
                p = p.next;
                ++j;
            }
            if(j > i-1 || p == head)
                throw new Exception("插入位置不合法!");
            DDNode s = new DDNode(x);
            p.next.pre = s;//p后结点前驱指向s
            s.next = p.next;//s后继指向p后结点
            p.next = s;//p后继指向s
            s.pre = p;//s前驱指向p
            updateTail();
        }
    }

    public void remove(int i) throws Exception {
            DDNode p = head;
            int j = -1;
            while(p.next != head && j < i-1) {
                p = p.next;
                ++j;
            }
            if(j > i-1 || p.next == head)
                throw new Exception("删除位置不合法!");
            p.next.next.pre = p;
            p.next = p.next.next;
            updateTail();
    }

    public void display() {
        DDNode node = head.next;
        while(node != head) {
            System.out.print(node.data + " ");
            node = node.next;
        }
        System.out.println();
    }

    public void clear() {
        head.data = null;
        head.next = head;
        head.pre = head;
        updateTail();
    }

    public boolean isEmpty() {
        return head.next == head;
    }

    public Object get(int i) throws Exception {
        DDNode p = head.next;
        int j = 0;
        while(p != head && j < i) {
            p = p.next;
            ++j;
        }
        if(j > i || p == head) {
            throw new Exception("第" + i + "个元素不存在!");
        }
        System.out.println("get successfully");
        return p.data;
    }

    public int indexOf(Object x) {
        DDNode p = head.next;
        int j = 0;
        while(p != head && !p.data.equals(x)) {
            p = p.next;
            ++j;
        }
        if(p != head)
            return j;
        else
            return -1;
    }

    public void updateTail() {
        if(length() != 0) {
            tail = head.pre;
        } else {
            tail = null;
        }
    }

    public void DDResever() {
        DDNode p1 = head.next;
        DDNode test = head.next;
        while(test != head) {
            DDNode store = test.pre;
            test.pre = test.next;
            test.next = store;
            test = test.pre;
        }
        DDNode p2 = head.pre;
        head.next = p2;
        head.pre = p1;
        updateTail();
    }

    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入链表元素的个数:");
        DDLinkList a = new DDLinkList(sc.nextInt());
        System.out.println("请输入要插入元素的个数:");
        int sum = sc.nextInt();
        System.out.println("请输入要插入的序号和值:");
        while(sum > 0) {
            a.insert(sc.nextInt(), sc.next());
            sum--;
        }
        System.out.println("插入后的链表元素值为:");
        a.display();
        System.out.println("请输入要删除元素的个数");
        sum = sc.nextInt();
        System.out.println("请输入要删除的元素的序号:");
        while(sum > 0) {
            a.remove(sc.nextInt());
            sum--;
        }
        System.out.println("删除元素后的链表元素值为:");
        a.display();
        System.out.println("请输入要取出的元素的个数:");
        sum = sc.nextInt();
        System.out.println("请输入待取元素的序号:");
        while(sum > 0) {
            System.out.print(a.get(sc.nextInt()) + " ");
            sum--;
        }
        System.out.println();
        System.out.println("请输入要查找的元素的个数:");
        sum = sc.nextInt();
        System.out.println("请输入要查找的元素的值:");
        while(sum > 0) {
            System.out.print(a.indexOf(sc.next()) + " ");
            sum--;
        }
        System.out.println();
        System.out.println("双向循环链表的长度为:" + a.length());
        System.out.println("就地逆置前元素值为:");
        a.display();
        System.out.println("就地逆置后元素值为:");
        a.DDResever();
        a.display();
    }
}

运行结果

请输入链表元素的个数:
5
请输入链表元素的值:
1 2 3 4 5
请输入要插入元素的个数:
1
请输入要插入的序号和值:
0 100
插入后的链表元素值为:
100 1 2 3 4 5 
请输入要删除元素的个数
1
请输入要删除的元素的序号:
0
删除元素后的链表元素值为:
1 2 3 4 5 
请输入要取出的元素的个数:
1
请输入待取元素的序号:
4
get successfully
5 
请输入要查找的元素的个数:
1
请输入要查找的元素的值:
5
4 
双向循环链表的长度为:5
就地逆置前元素值为:
1 2 3 4 5 
就地逆置后元素值为:
5 4 3 2 1 

Process finished with exit code 0