单链表的增删改查

//单链表的创建与遍历的分析实现
//思路:将每一个节点看作是一个对象,而链表就是管理对象的集合,每一个节点就是一个对象的实现,
package com.chanchan.LinkedListTest;
public class LinedList {
    public static void main(String[] args) {
        //先创建节点
        HeroNode node1=new HeroNode( 1,"宋江","及时雨" );
        HeroNode node2=new HeroNode( 2,"李逵","黑旋风" );
        HeroNode node3=new HeroNode( 3,"吴用","智多星" );
        HeroNode node4=new HeroNode( 4,"林冲","豹子头" );
        //创建链表
        SingleLinedList list = new SingleLinedList();
        //添加节点的第一种方式,不按照顺序尾插法
//        list.addList( node1 );
//        list.addList( node2 );
//        list.addList( node3 );
//        list.addList( node4 );
      //  list.addList( node1 );    //当重复添加一个元素时,会无限制的添加

        //添加节点的第二种方式,按照编号的顺序插入节点
        list.addById( node1 );
        list.addById( node4 );
        list.addById( node3 );
        list.addById( node2 );
        list.showList();
        System.out.println();

        //获取指定id的节点
        list.getById( 5 );

//        //删除链表的节点
//        list.remove( 1 );
//        list.remove( 2 );
//        list.remove( 3 );
//        list.remove( 4 );
//        list.showList();

//        HeroNode node5=new HeroNode( 2,"武松","打虎人" ); //创建以一个节点
//        list.SetNode(node5); //修改节点的信息
//        list.showList();    //遍历显示节点
    }
}
/**
 * 管理节点的方法类
 * */
//创建一个类,用来管理这些节点
class SingleLinedList{
    //初始化一个头节点,头节点不要动,用一个辅助节点来指向节点
    private HeroNode head=new HeroNode( 1,"","" );
    //添加节点到单向链表
    //思路:当不考虑节点顺序时,找到当前链表的最后节点
    //将这个最后节点的next指向这个新加入的节点

    //添加一个新的节点,不按照编号的顺序添加
    public void addList(HeroNode heronode){
        HeroNode tmp=head; //将这个临时节点指向头节点
        //遍历链表,找到最后
        while (true){
            //找到节点的最后,
            if(tmp.next==null){
                break;
            }
            //如果没有找到最后,将tmp后移
            tmp=tmp.next;
        }
        //当退出while循环时,链表遍历到了最后
        //将最后这个节点的next指向新的节点
        tmp.next=heronode;
        //如果重新重新定义一个相同的元素,它会找到第一个相同的元素,指针指向的又重新回到旧有的位置。

    }

    //第二种添加节点的方式,按照顺序添加id
    //因为是单链表,所以我们添加元素时需要找到其哪一个元素的位置,否则插入不进去(双链表的话可以实现)
    public void addById(HeroNode heroNode){
        //临时定义一个指针,让它指向头节点
        HeroNode tmp=head;
        //定义一个标识,用来表示添加元素的位置是否编号已经存在,如果已经存在,那么添加不成功
        Boolean flag=false;
        while (true){
            //首先判断链表是否为空,如果为空的,直接添加到链表的最后
            if(tmp.next==null) {//说明链表为空,跳出循环
                break;
            }
            //然后寻找指针添加的位置
            if(tmp.next.id>heroNode.id){
                break;
            }else if(tmp.next.id==heroNode.id) { //说明想要添加的编号已经存在
                flag=true;
                break;
            }
            tmp=tmp.next;  //将tmp向后移动一位,继续遍历
        }
        //循环结束,说明想要找的节点已经找到,继续判断
        if(flag){
            System.out.println("编号已经存在,不能插入成功");
        }else {
            heroNode.next=tmp.next;  //单链表插入节点时需要先插后面的节点
            tmp.next=heroNode;
        }
    }

    //遍历展示链表,将链表输出
    public void showList(){
            //判断链表是否遍历到了最后
            if(head.next==null){
                System.out.println("链表为空");
                return;
            }
            //定义一个临时的变量指向头节点的下一个节点
        HeroNode tmp=head.next;
            while (true){
                //判断链表是否到了最后
                if(tmp==null){
                    break;
                }
                System.out.println(tmp);
                tmp=tmp.next;
            }
            //如果跳出循环,则代表链表循环结束
    }

    //修改链表的节点数据
    public void SetNode(HeroNode heroNode){
        //首先判断我们的链表是否为空
        if (head.next==null){
            System.out.println("链表为空,不能修改数据");
        }
        HeroNode tmp =head.next; //定义一个变量,将第一个节点赋值给这个变量
        boolean flag=false;  //设置一个标识,用来标识这个节点是否已经找到
        while (true){
            if(tmp== null){ //链表遍历完毕
                return;
            }
            if(tmp.id==heroNode.id){  //判断它们的编号是否相同
                flag=true;
               break;
            }
            tmp=tmp.next;
        }
        if(flag){
            tmp.name=heroNode.name;
            tmp.no=heroNode.no;
        }else {
            System.out.println("链表中没有相符合的id编号,不能修改");
        }
    }

    //查询链表的节点
    public HeroNode getById(int id){
        HeroNode tmp=head;
        boolean flag=false;
        while (true){
            if(tmp==null){
                break;
            }
            if(tmp.id==id){
                flag=true;
                break;
            }
            tmp=tmp.next;
        }
        if(flag){
            System.out.println("找到的节点为:"+tmp.next);
            return tmp.next;
        }else {
            System.out.println("找不到输入的id");
            return null;
        }
    }

    /*
    * 思路:我们首先要找到待删除节点的前一个节点tmp,
    * tmp.next=tmp.next.next;
    * 被删除的节点将不会有其他节点指向,会被垃圾收集机制收集
    * */
    //删除链表的节点
    public void remove(int id){
        HeroNode tmp=head;
        boolean flag = false;
        while (true){
            if(tmp.next==null){ //已经到了链表的最后
                break;
            }
            if(tmp.next.id==id){
                flag=true;
                break;
            }
            tmp=tmp.next;
        }
        if(flag){
            tmp.next=tmp.next.next;
        }else {
            System.out.println("没有匹配的的id");
        }
    }

}

/**
 * 节点对象类
 * */
//定义一个节点类,用来存储节点的信息,包括:数据域和指针域
//把节点看作一个对象,把链表当作一个集合,链表存储对象。
class HeroNode{
    public int id;      //英雄的编号
    public String name;   //英雄的名字
    public String no;      //昵称
    public HeroNode next;
    //建立一个构造函数,用来初始化这个节点对象
    public HeroNode(int id,String name,String no){
        this.id=id;
        this.name=name;
        this.no=no;
    }
    @Override
    public String toString() {
        return "Node{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", no='" + no + '\'' +
                '}';
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你解答这个问题。 首先,我们可以定义一个火车售票的节点类,包含以下属性: - 车次号 - 出发站点 - 到达站点 - 出发时间 - 到达时间 - 座位类型 - 票价 - 下一节点指针 接下来,我们可以定义一个火车售票单链表类,实现以下方法: 1. 增加节点:在链表末尾添加节点 2. 删除节点:根据车次号删除指定节点 3. 修改节点:根据车次号修改指定节点 4. 查询节点:根据车次号查询指定节点信息 代码实现如下: ```python class TrainTicketNode: def __init__(self, train_num, start_station, end_station, start_time, end_time, seat_type, ticket_price): self.train_num = train_num self.start_station = start_station self.end_station = end_station self.start_time = start_time self.end_time = end_time self.seat_type = seat_type self.ticket_price = ticket_price self.next = None class TrainTicketLinkedList: def __init__(self): self.head = None # 添加节点 def add_node(self, train_num, start_station, end_station, start_time, end_time, seat_type, ticket_price): new_node = TrainTicketNode(train_num, start_station, end_station, start_time, end_time, seat_type, ticket_price) if self.head is None: self.head = new_node else: current_node = self.head while current_node.next is not None: current_node = current_node.next current_node.next = new_node # 删除节点 def delete_node(self, train_num): if self.head is None: print("链表为空") else: if self.head.train_num == train_num: self.head = self.head.next else: current_node = self.head while current_node.next is not None and current_node.next.train_num != train_num: current_node = current_node.next if current_node.next is None: print("未找到该车次信息") else: current_node.next = current_node.next.next # 修改节点 def modify_node(self, train_num, start_station, end_station, start_time, end_time, seat_type, ticket_price): if self.head is None: print("链表为空") else: current_node = self.head while current_node is not None and current_node.train_num != train_num: current_node = current_node.next if current_node is None: print("未找到该车次信息") else: current_node.start_station = start_station current_node.end_station = end_station current_node.start_time = start_time current_node.end_time = end_time current_node.seat_type = seat_type current_node.ticket_price = ticket_price # 查询节点 def search_node(self, train_num): if self.head is None: print("链表为空") else: current_node = self.head while current_node is not None and current_node.train_num != train_num: current_node = current_node.next if current_node is None: print("未找到该车次信息") else: print("车次号:{}\n出发站点:{}\n到达站点:{}\n出发时间:{}\n到达时间:{}\n座位类型:{}\n票价:{}".format( current_node.train_num, current_node.start_station, current_node.end_station, current_node.start_time, current_node.end_time, current_node.seat_type, current_node.ticket_price)) ``` 这样,我们就实现了火车售票单链表增删改查功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值