数据结构——单链表节点的增删改查

单链表节点的增删改查

场景所需的实体类
class Person{
    int num; //编号
    String name; //姓名
    int age; //年龄
    Person next; //下一个节点

    public Person() {
    }

    public Person(int num, String name, int age) {
        this.num = num;
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "num=" + num +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
单链表实现
class SingleLinkedList {
    //创建一个头节点,头节点不能动,不用于存放数据
    private Person head = new Person(0,"",0);

    //返回头节点
    public Person getHead() {
        return head;
    }

    //不考虑顺序向链表中添加数据
    public void add(Person person) {
        //因为头节点不能动,创建一个辅助节点
        Person temp = head;
        //遍历链表,找到链表最后位置
        while (true){
            //判断是否到最后
            if (temp.next == null) {
                break;
            }
            //如果没有到最后,就接着往后面找
            temp = temp.next;
        }
        //将新的元素添加到链表最后
        temp.next = person;
    }

    //考虑顺序向链表中添加数据
    public void addByOrder(Person person) {
        Person temp = head;
        //创建一个标识判断链表所添加的元素的编号是否已经存在
        boolean flag = false;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.next.num > person.num){
                break;
            }else if (temp.next.num == person.num) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            System.out.println("准备插入的编号"+person.num+"已存在,插入失败");
        } else {
            person.next = temp.next;
            temp.next = person;
        }
    }

    //修改链表中元素的值
    public void update(Person newPerson) {
        //判断是否为空
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        Person temp = head.next;
        boolean flag = false;

        //遍历找到
        while (true) {
            if (temp == null) {
                break;
            }
            if (temp.num == newPerson.num) {
                //找到
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            temp.name = newPerson.name;
            temp.age = newPerson.age;
        } else {
            System.out.println("没有找到编号为"+newPerson.name+"的节点");
        }
    }

    //删除节点
    public void del(int num) {
        Person temp = head;
        boolean flag = false;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.next.num == num) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag){
            temp.next = temp.next.next;
        } else {
            System.out.println("要删除的"+num+"节点不存在");
        }
    }

    //遍历链表
    public void list() {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }

        Person temp = head.next;
        while (true) {
            if (temp == null ){
                break;
            }
            System.out.println(temp);
            temp = temp.next;
        }
    }
}
测试

1. 测试不考虑顺序向链表中添加数据

public class LinkedListDemo {
    public static void main(String[] args) {
        Person p1 = new Person(1, "张三", 18);
        Person p2 = new Person(2, "李四", 34);
        Person p3 = new Person(3, "王五", 24);
        Person p4 = new Person(4, "赵六", 45);

        SingleLinkedList singleLinkedList = new SingleLinkedList();

        singleLinkedList.add(p1);
        singleLinkedList.add(p4);
        singleLinkedList.add(p2);
        singleLinkedList.add(p3);
        singleLinkedList.list();
    }
}

测试结果:由下图可知现在每次都在链表最后插入
在这里插入图片描述
2. 测试考虑顺序向链表中添加数据

public class LinkedListDemo {
    public static void main(String[] args) {
        Person p1 = new Person(1, "张三", 18);
        Person p2 = new Person(2, "李四", 34);
        Person p3 = new Person(3, "王五", 24);
        Person p4 = new Person(4, "赵六", 45);

        SingleLinkedList singleLinkedList = new SingleLinkedList();

        singleLinkedList.addByOrder(p1);
        singleLinkedList.addByOrder(p4);
        singleLinkedList.addByOrder(p2);
        singleLinkedList.addByOrder(p3);
        singleLinkedList.list();
    }
}

测试结果:由下图可知现在会根据插入节点的编号进行插入
在这里插入图片描述
3. 测试修改节点信息

public class LinkedListDemo {
    public static void main(String[] args) {
        Person p1 = new Person(1, "张三", 18);
        Person p2 = new Person(2, "李四", 34);
        Person p3 = new Person(3, "王五", 24);
        Person p4 = new Person(4, "赵六", 45);

        SingleLinkedList singleLinkedList = new SingleLinkedList();

        singleLinkedList.addByOrder(p1);
        singleLinkedList.addByOrder(p4);
        singleLinkedList.addByOrder(p2);
        singleLinkedList.addByOrder(p3);
        singleLinkedList.list();

        System.out.println();
        Person newPerson = new Person(4, "赵六2", 46);
        singleLinkedList.update(newPerson);
        singleLinkedList.list();
    }
}

测试结果:
在这里插入图片描述
4. 测试删除节点

public class LinkedListDemo {
    public static void main(String[] args) {
        Person p1 = new Person(1, "张三", 18);
        Person p2 = new Person(2, "李四", 34);
        Person p3 = new Person(3, "王五", 24);
        Person p4 = new Person(4, "赵六", 45);

        SingleLinkedList singleLinkedList = new SingleLinkedList();

        singleLinkedList.addByOrder(p1);
        singleLinkedList.addByOrder(p4);
        singleLinkedList.addByOrder(p2);
        singleLinkedList.addByOrder(p3);
        singleLinkedList.list();

        System.out.println();
        singleLinkedList.del(2);
        singleLinkedList.list();
    }
}

测试结果:
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值