(三)双向链表

这是一个使用Java编写的程序,实现了双向链表的数据结构。用户可以选择进行插入新节点、删除节点、更新节点值、正向及反向打印链表、在指定位置插入和删除、计算节点个数以及找到链表倒数第k个节点等操作。程序通过Scanner获取用户输入,动态交互进行链表操作。
摘要由CSDN通过智能技术生成

1、基本介绍

2、应用实例(双向链表)

package linkedlist;

import java.util.Scanner;

public class DoubleLinkedList {
    public static void main(String[] args) {
        LinkedNode head = new LinkedNode(0);
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        char op;
        int pos;
        int val;
        while(loop) {
            System.out.println("\n链表插入~(i)");
            System.out.println("链表删除~(d)");
            System.out.println("链表修改~(u)");
            System.out.println("链表打印~(p)");
            System.out.println("反向打印~(r)");
            System.out.println("链表插入指定位置~(c)");
            System.out.println("链表删除指定位置~(s)");
            System.out.println("链表有效节点个数~(n)");
            System.out.println("链表倒数第k个节点~(g)");
            System.out.println("退出程序(q)~");
            System.out.print("请选择你要执行的操作:");
            op = scanner.next().charAt(0);
            switch (op) {
                case 'i':
                    System.out.print("请输入要插入的值:");
                    val = scanner.nextInt();
                    insert(head, val);
                    break;
                case 'd':
                    delete(head);
                    break;
                case 'u':
                    System.out.print("请输入要更新的值:");
                    val = scanner.nextInt();
                    System.out.print("请输入元素的位置:");
                    pos =scanner.nextInt();
                    update(head, pos, val);
                    break;
                case 'p':
                    System.out.print("单链表元素:");
                    print(head);
                    break;
                case 'r':
                    System.out.print("单链表元素(反向遍历):");
                    reversePrint(head);
                    break;
                case 'c':
                    System.out.print("请输入要插入的值:");
                    val = scanner.nextInt();
                    System.out.print("请输入元素的位置:");
                    pos =scanner.nextInt();
                    insertByPosition(head, val, pos);
                    break;
                case 's':
                    System.out.print("请输入元素的位置:");
                    pos =scanner.nextInt();
                    deleteByPosition(head, pos);
                    break;
                case 'n':
                    count(head);
                    break;
                case 'g':
                    System.out.print("请输入k的值:");
                    pos =scanner.nextInt();
                    get(head, pos);
                    break;
                default:
                    scanner.close();
                    loop = false;
                    break;
            }
        }
    }


    public static void insert(LinkedNode head, int insertVal) {
        LinkedNode node = new LinkedNode(insertVal);
        LinkedNode temp = head;
        if (head.next == null) {
            head.next = node;
            node.pre = head;
            System.out.println("结点插入成功~");
        } else {
            while (temp.next != null) {
                temp = temp.next;
            }
            temp.next = node;
            node.pre = temp;
            System.out.println("结点插入成功~");
        }
    }

    public static void delete(LinkedNode head) {
        if (isEmpty(head)) {
            throw new RuntimeException("单链表没东西啦~");
        }
        LinkedNode temp = head.next; // temp:被删除节点
        int deleteVal = temp.data;
        temp.pre.next = temp.next;
        temp.next.pre = temp.pre;
        System.out.println("结点删除成功~");
        System.out.println("删除结点元素值:" + deleteVal);
    }

    public static void update(LinkedNode head, int pos, int updateVal) {
        LinkedNode temp = head;
        int count = 0;
        while (temp.next != null && count < pos) {
            count++;
            temp = temp.next;
        }
        if (temp != null) {
            temp.data = updateVal;
        } else {
            throw new RuntimeException("该元素不存在~");
        }
    }

    public static void print(LinkedNode head) {
        if (isEmpty(head)) {
            throw new RuntimeException("单链表没东西~");
        }
        LinkedNode temp = head.next;
        while (temp != null) {
            System.out.print(temp.data+ " ");
            temp = temp.next;
        }
        System.out.println();
    }

    private static void reversePrint(LinkedNode head) {
        if (isEmpty(head)) {
            throw new RuntimeException("单链表没东西~");
        }
        LinkedNode temp = head;
        while(temp.next != null) {
            temp = temp.next;
        }

        while (temp != head) {
            System.out.print(temp.data + " ");
            temp = temp.pre;
        }
        System.out.println();
    }

    public static void insertByPosition(LinkedNode head, int insertVal, int pos) {
        LinkedNode temp = head;
        LinkedNode node = new LinkedNode(insertVal);
        int count = 0;
        while (temp.next != null && count < pos - 1) {
            count++;
            temp = temp.next;
        }

        node.next = temp.next; // temp指向的是插入位置的前一个位置
        temp.next.pre = node;
        temp.next = node;
        node.pre = temp;
    }

    public static void deleteByPosition(LinkedNode head, int pos) {
        LinkedNode temp = head;
        int count = 0;
        while (temp.next != null && count < pos) {
            count++;
            temp = temp.next;
        }

        temp.pre.next = temp.next;  // temp:指向的是被删节点
        temp.next.pre = temp.pre;
    }

    private static int count(LinkedNode head) {
        if (isEmpty(head)) {
            throw new RuntimeException("链表没东西,臣妾很难做到啊~");
        }
        LinkedNode temp = head;
        int count = 0;
        while (temp.next != null) {
            temp = temp.next;
            count++;
        }
        System.out.println("链表有效节点个数:" + count);
        return count;
    }

    private static void get(LinkedNode head, int index) {
        if (isEmpty(head)) {
            throw new RuntimeException("链表没东西,臣妾很难做到啊~");
        }
        //  1、获取链表长度
        int size = count(head);

        //  2、边界判断
        if (index <= 0 || index > size) {
            System.out.println("越界了~朋友");
        }

        //  3、遍历到size-index位置,就是倒数第k个节点
        LinkedNode temp = head.next;
        for (int i = 0; i < size-index; i++) {
            temp = temp.next;
        }
        System.out.println("倒数第"+ index + "个节点的值:" + temp.data);
    }

    public static boolean isEmpty(LinkedNode head) {
        if (head.next == null) {
            return true;
        }
        return false;
    }
}

class LinkedNode{
    public int data;
    public LinkedNode pre;
    public LinkedNode next;
    public LinkedNode(int data) {
        this.data = data;
        this.next = null;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

来得晚一些也行

观众老爷,请赏~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值