[JDK17]单链表的简单实现

本文介绍了一个Java程序,实现了一个包含添加、修改、删除和查询功能的单链表。用户可以通过命令行输入操作节点,如编号、名称和昵称等。
摘要由CSDN通过智能技术生成

完整代码:

package LinkList;


import java.util.Scanner;

public class LinkedListTest {
    public static void main(String[] args) {
        LinkedListTest linkedListTest = new LinkedListTest();
        LinkedList linkedList = linkedListTest.new LinkedList();
        Scanner scan = new Scanner(System.in);
        boolean isLoop = true;
        while (isLoop) {
            System.out.println("-------------------------------------------------------------------------------------");
            System.out.println("SingleLinkedList Test");
            System.out.println("Enter 'l' to print all node");
            System.out.println("Enter 'au' to add or update node ");
            System.out.println("Enter 'r' to remove node");
            System.out.println("Enter 'q' to query node");
            System.out.println("Enter 'e' to exit program");
            System.out.println("-------------------------------------------------------------------------------------");

            String str = scan.next();

            switch (str) {
                case "l" -> {
                    try {
                        linkedList.list();
                    } catch (RuntimeException e) {
                        System.out.println(e.getMessage());
                    }
                }
                case "au"->{
                    int no;
                    String name;
                    String nickName;

                    System.out.println("Please enter 'no','name','nickName'[Update node when the 'no' is exist]:");
                    System.out.println("Enter the 'no':");
                    no = scan.nextInt();
                    System.out.println("Enter the 'name':");
                    name = scan.next();
                    System.out.println("Enter the 'nickName':");
                    nickName=scan.next();

                    linkedList.addOrUpdate(no,name,nickName);
                }
                case "r"->{
                    System.out.println("Enter the 'no' you'll remove:");
                    int no = scan.nextInt();
                    try {
                        HeroNode temp = linkedList.remove(no);
                        System.out.println("Remove no:[" + temp.no+"] success.");
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                }
                case "q"->{
                    System.out.println("Enter the 'no' you'll query:");
                    int no = scan.nextInt();
                    try {
                        HeroNode result = linkedList.query(no);
                        System.out.println("The node is:");
                        System.out.println(result);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                }
                case "e"-> isLoop = false;
            }
        }
        System.out.println("Program exit.");
    }

    /**
     * 节点类
     */
    public class HeroNode{
        public int no;
        public String name;
        public String nickname;
        public HeroNode next;

        public HeroNode(int no,String name,String nickname){
            this.no = no;
            this.name = name;
            this.nickname = nickname;
        }

        public HeroNode() {
        }

        @Override
        public String toString() {
            return "HeroNode{" +
                    "no=" + no +
                    ", name='" + name + '\'' +
                    ", nickname='" + nickname + '\'' +
                    ", next=" + next +
                    '}';
        }
    }

    /**
     * 单链表结构
     */
    public class LinkedList{
        HeroNode head = new HeroNode();

        /**
         * 添加或修改节点
         * @param no
         * @param name
         * @param nickName
         */
        public void addOrUpdate(int no,String name,String nickName){
            HeroNode temp = head;
            boolean isAdd = true;
            while (temp.next != null) {
                if (no == temp.no){
                    temp.name = name;
                    temp.nickname = nickName;
                    isAdd = false;
                    System.out.println("修改节点成功!");
                    break;
                }
                temp = temp.next;
            }
            if (isAdd){
                temp.next = new HeroNode(no,name,nickName);
                System.out.println("添加节点成功!");
            }
        }

        /**
         * 打印所有节点
         */
        public void list(){
            HeroNode temp = head.next;
            boolean isNull = true;
            while (temp != null) {
                isNull = false;
                System.out.println(temp);
                temp = temp.next;
            }
            if (isNull){
                throw new RuntimeException("链表为空,无法展示!");
            }
        }

        /**
         * 删除节点
         * @return
         */
        public HeroNode remove(int no){
            HeroNode temp = head;
            while (temp.next != null){
                if (temp.next.no == no){
                    HeroNode p = temp.next;
                    temp.next = p.next;
                    p.next = null;
                    System.out.println("节点删除成功!");
                    return p;
                }
            }
            throw new RuntimeException("找不到该节点,删除失败!");
        }

        /**
         * 查询指定节点
         * @param no
         * @return
         */
        public HeroNode query(int no) {
            HeroNode temp = head.next;
            while (temp != null){
                if (no == temp.no){
                    System.out.println("查询成功!");
                    return temp;
                }
                temp = temp.next;
            }
            throw new RuntimeException("找不到该节点!");
        }
    }
}

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值