单链表的创建及基本操作函数(Java日记Ⅳ)

本文展示了如何使用Java实现链表数据结构,包括添加元素、显示链表、查找指定ID、修改元素和删除元素等基本操作。通过实例代码详细阐述了链表节点的定义和链表类的方法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前写过C语言的文章,Java思想不变只是换种写法,因此不做过多解释说明,代码里已经很清晰


public class LinkListDemo {
    public static void main(String[] args) {
        LinkList lst=new LinkList();
        lst.add(1,"Error");
        lst.add(2,"Starry");
        lst.insert(3,3,"lian");
        lst.findId(1);
        lst.revise(1,"haha");
        lst.delId(3);
        lst.show();

    }
}
class LinkNode{
    int id;
    String name;
    LinkNode next;
    public LinkNode(int id,String name){
        this.id=id;
        this.name=name;

    }
    public void sayHello(){
        System.out.println("my name is "+name+",and my id is "+id+".");
    }
}
class LinkList{
    LinkNode head=new LinkNode(0,"");
    public void add(int id,String name){
        LinkNode node=new LinkNode(id,name);
        LinkNode temp=head;
        while (temp.next!=null){
            temp=temp.next;
        }
        temp.next=node;
    }
    public void show(){
        LinkNode temp=head;
        while (temp.next!=null){
            temp=temp.next;
            temp.sayHello();
        }
    }
    public void findId(int id){
        LinkNode temp=head;
        while (temp.next!=null){
            temp=temp.next;
            if(temp.id==id){
                temp.sayHello();
                return;
            }

        }
        System.out.println("没有此id哦~");
    }
    public void delId(int id){
        LinkNode temp=head;

        while (temp.next!=null){
            LinkNode cur=temp;
            temp=temp.next;
            if(temp.id==id){
                cur.next=temp.next;
                System.out.println("删除"+temp.name);
                return;
            }
        }
        System.out.println("没有此id哦~");

    }
    public int length(){
        LinkNode temp=head;
        int count=0;
        while (temp.next!=null){
            temp=temp.next;
            count++;
        }
        return count;
    }
    public void insert(int location,int id,String name){
        int len=length();
        if(location<1||location>len+1){
            System.out.println("插入的位置有误,当前链表长度为"+len);
            return;
        }
        LinkNode temp=head;
        LinkNode node=new LinkNode(id,name);
        LinkNode cur=head;
        while (location!=0){
            cur=temp;
            temp=temp.next;
            location--;
        }
        cur.next=node;
        node.next=temp;

    }
    public void revise(int id,String newname){
        LinkNode temp=head;

        while (temp.next!=null){
            temp=temp.next;
            if(temp.id==id){
                temp.name=newname;
                return;
            }
        }
        System.out.println("没有此id哦~");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值