数据结构—单向链表

该博客详细介绍了如何使用Java编程语言实现单向链表,包括带头节点的链表初始化、节点的添加、更新、删除以及链表的展示。示例代码展示了如何创建节点类和单向链表类,并提供了测试用例来验证这些基本操作的功能。
摘要由CSDN通过智能技术生成

小结:

1、以节点的方式存储数据;

2、每个节点包括:data域(存储的数据)& next域(指向下一个节点)

3、节点不一定连续存放

4、类似一串大蒜,每颗大蒜是一个节点

使用 java 语言实现单向链表(带头节点),以及 add / upData / delete / show 等基础功能

节点类:

public class Node {

    int data; // 存储的数据
    String data1; // 存储的数据
    Node next; // 指向下一个节点

    public Node(int data, String data1) { // 构造方法
        this.data = data;
        this.data1 = data1;
    }

    @Override
    public String toString() {
        return "Node{" +
                "data=" + data +
                ", data1='" + data1 + '\'' +
                '}';
    }
}

单向链表类:

public class SingleLinkedList {

    // 初始化头节点
    Node head = new Node(0, "");

    // add方法
    public void add(Node node) {
        // 创建一个辅助变量指向 head,一个布尔标记
        Node assist = head;
        boolean f = false;

        while (true) {
            // 当前指针指向尾节点,可以add到此处,终止循环
            if (assist.next == null) {
                break;
            }
            // 此时找到了添加节点的位置,即 assist 指针指向节点的后面
            if (assist.next.data > node.data) {
                break;
            }
            // 在链表中有与 node 重复的节点,无法添加,终止循环
            if (assist.next.data == node.data) {
                f = true;
                break;
            }
            // 向后移动指针
            assist = assist.next;
        }

        if (f) { // 此时无法继续添加
            System.out.printf("无法继续添加编号为:%d 的新节点\n", node.data);
        } else { // 此时可以添加新节点,并添加在 assist 后面
            node.next = assist.next; // 新节点指向 assist 指向的节点,即原 assist.next
            assist.next = node;// assist 指向新加入的节点
        }

    }

    // upData方法
    public void upData(Node newNode) {

        // 判断是否为空链表
        if (head.next == null) {
            System.out.println("空链表");
            return;
        }

        // 创建辅助指针,指向头节点 & 布尔标记
        Node assist = head;
        boolean f = false;

        while (true) {
            // 判断是否遍历完毕
            if (assist.next == null) {
                break;
            }
            // 此时找到了需要修改的节点,即 assist.next
            if (assist.next.data == newNode.data) {
                f = true;
                break;
            }
            // 指针后移一位
            assist = assist.next;
        }

        if (f) { // 修改节点
             assist.next.data = newNode.data;
             assist.next.data1 = newNode.data1;
        } else {
            System.out.printf("找不到编号:%d 的节点进行修改\n", newNode.data);
        }

    }

    // delete方法
    public void delete(int data) {

        // 判断是否为空链表
        if (head.next == null) {
            System.out.println("空链表");
            return;
        }

        // 创建辅助指针,指向头节点 & 布尔标记
        Node assist = head;
        boolean f = false;

        while (true) {
            // 判断是否遍历完毕
            if (assist.next == null) {
                break;
            }
            // 找到了待删除的元素
            if (assist.next.data == data) {
                f = true;
                break;
            }
            // 指针后移一位
            assist = assist.next;
        }

        if (f) {
            assist.next = assist.next.next;
        } else {
            System.out.printf("找不到编号为:%d 的元素进行删除", data);
        }

    }

    // show方法
    public void show() {

        // 先判断链表是否为空
        if (head.next == null) {
            System.out.println("空链表");
            return;
        }
        System.out.println("遍历链表,得到:");

        // 辅助指针
        Node assist = head.next;

        while (true) {
            // 打印节点信息
            System.out.println(assist);

            // 判断是否遍历完毕
            if (assist.next == null) {
                break;
            }

            // 指针后移一位
            assist = assist.next;
        }

    }

}

测试单向链表:

public class test03 {

    public static void main(String[] args) {
        // 创建链表对象
        SingleLinkedList singleLinkedList = new SingleLinkedList();
        // 创建 Node 对象
        Node one = new Node(1,"一");
        Node two = new Node(2,"二");
        Node three = new Node(3,"三");
        Node four = new Node(4,"四");
        Node fakeOne = new Node(1,"假一");

        // add
        singleLinkedList.add(one);
        singleLinkedList.add(three);
        singleLinkedList.add(two);
        singleLinkedList.add(four);
        singleLinkedList.show();

        // upData
        singleLinkedList.upData(fakeOne);
        singleLinkedList.show();

        // delete
        singleLinkedList.delete(1);
        singleLinkedList.delete(4);
        singleLinkedList.delete(2);
        singleLinkedList.show();
        
    }

}

得到的结果为:

遍历链表,得到:
Node{data=1, data1='一'}
Node{data=2, data1='二'}
Node{data=3, data1='三'}
Node{data=4, data1='四'}
遍历链表,得到:
Node{data=1, data1='假一'}
Node{data=2, data1='二'}
Node{data=3, data1='三'}
Node{data=4, data1='四'}
遍历链表,得到:
Node{data=3, data1='三'}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值