万金流
以码会友。 吾Q:578751655。 水平有限,轻喷,谢!
随笔 - 189,  文章 - 0,  评论 - 7,  阅读 - 14万

先建立学生模型:

 

 再建立链表业务模型:

复制代码
public class MyLink
{
    public Student s;
    public MyLink nextone;

    public MyLink()
    {
    }

    public MyLink(Student x)
    {
        s = x;
        nextone = null;
    }

    // add
    public void add(Student x)
    {
        MyLink pre=this,t = nextone;
        while (t != null)
        {
            pre=t;
            t = t.nextone;
        }
        pre.nextone = new MyLink(x);
        System.out.println("add success.");
    }

    // delete
    public void del(Student x)
    {
        MyLink pre = this, p = nextone;
        Student t;
        while (p != null)
        {
            t = p.s;
            if (t.name.equals(x.name) && t.age == x.age)
            {
                pre.nextone = p.nextone;
                // p=p.nextone;连续删除应该有这句,无下句
                break;
            }
            else
            {
                pre = p;
                p = pre.nextone;
            }
        }
    }

    // modify--modi age by name
    public void modi(String x, int y)
    {
        MyLink t = nextone;
        while (t != null)
        {
            if (t.s.name.equals(x))
            {
                t.s.age = y;
                break;// 连续修改应该没有这句
            }
            t = t.nextone;
        }
    }

    // get student by xm
    public Student get(String x)
    {
        MyLink t = nextone;
        while (t != null)
        {
            if (t.s.name.equals(x))
            {
                return t.s;
            }
            t = t.nextone;
        }
        return null;
    }
    //display all
    public void displayAll()
    {
        MyLink t = nextone;
        while (t != null)
        {
            t.s.showMe();
            t = t.nextone;
        }
    }
}
复制代码

主程序调用:

 

 运行结果:

 

先理解,然后写一下。

会写了,就用链表改写书上的例子。

posted on   万金流  阅读(233)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
历史上的今天:
2019-10-18 Mvc中模拟模型

点击右上角即可分享
微信分享提示