单链表的增删改查

链表是有序的列表,但是它在内存中是存储如下

1 链表是以节点的方式来存储,是链式存储

2 每个节点包含 data 域, next 域:指向下一个节点.

3 如图:发现链表的各个节点不一定是连续存储.

4 链表分带头节点的链表和没有头节点的链表,根据实际的需求来确定

package com.wang;

public class SingleList {
	public static void main(String[] args) {
		HeroNode hero1 = new HeroNode(1,"宋江","及时雨");
		HeroNode hero2 = new HeroNode(2,"卢俊义","玉麒麟");
		HeroNode hero3 = new HeroNode(3,"吴用","智多星");
		
		SingleList1 singleList = new SingleList1();
		singleList.add1(hero1);
		singleList.add1(hero2);
		singleList.add1(hero3);
		singleList.list();
		
		System.out.println("修改后");
		HeroNode hero2_1 = new HeroNode(2,"卢本伟","赌怪");
		singleList.update(hero2_1);
		singleList.del(1);
		singleList.list();
	}
}


/*
 * 该类用于管理我们的英雄
 */
class SingleList1{
	//先初始化一个头结点,该节点不要动,只是起到标识作用
	private HeroNode head=new HeroNode(0,"","");
	
	/**
	 * 添加节点
	 * 暂不考虑排名,只是把新节点放到末尾
	 */
	public void add(HeroNode heroNode) {
		//辅助遍历节点temp
		HeroNode temp=head;
		while (true) {
			//找到最后一个节点,结束循环
			if (temp.next==null) {
				break;
			}
			temp = temp.next;
		}
		//循环结束后,temp就到达了尾结点,然后将信息赋给它的下一个节点
		temp.next=heroNode;
	}
	
	
	/**
	 * 添加节点
	 * 根据排名添加
	 * 找到最后一个节点,结束循环
	 * 找到要插入的位置,结束循环
	 * 找到排名相同的,结束循环
	 */
	public void add1(HeroNode heroNode) {
		//辅助遍历节点temp
		HeroNode temp=head;
		boolean flag=false;
		while (true) {
			if (temp.next==null) {
				break;
			}
			if (temp.next.no>heroNode.no) {
				break;
			}else if(temp.next.no==heroNode.no){
				flag = true;
				break;
			}
			temp = temp.next;
		}
		//根据flag的值判断能否插入
		if (flag) {
			System.out.println(heroNode.no+"已存在,不能重复添加");
		}else {
			heroNode.next=temp.next;
			temp.next=heroNode;
		}
	}
	
	
	/**
	 * 修改节点信息,根据编号进行查找
	 */
	public void update(HeroNode newHeroNode) {
		HeroNode temp=head;
		boolean flag=false;
		while(true) {
			if (temp.next==null) {
				break;
			}
			if (temp.no==newHeroNode.no) {
				flag = true;
				break;
			}
			temp = temp.next;
		}
		if (flag) {
			temp.name =newHeroNode.name;
			temp.nickname=newHeroNode.nickname;
		}else {
			System.out.println("未找到"+newHeroNode.no);
		}
	}
	
	
	/**
	 * 删除节点
	 */
	public void del(int no) {
		HeroNode temp=head;
		boolean flag=false;
		while(true) {
			if (temp.next==null) {
				break;
			}
			if (temp.next.no==no) {
				flag = true;
				break;
			}
			temp = temp.next;
		}
		if (flag) {
			temp.next=temp.next.next;
		}else {
			System.out.println("未找到"+no);
		}
	}
	
	//显示链表
	public void list() {
		//判断链表是否为空
		if (head.next == null) {
			System.out.println("链表为空");
			return;
		}
		//辅助遍历
		HeroNode temp = head.next;
		while (true) {
			if (temp== null) {
				break;
			}
			//输出节点信息
			System.out.println(temp);
			temp = temp.next;
		}
	}
}


/**
 * 该类用于定义及初始化
 */
class HeroNode{
	/**
	 * 排名
	 * 名字
	 * 称号
	 * 指向下一个节点
	 */
	public int no;
	public String name;
	public String nickname;
	public HeroNode next;

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


	@Override
	public String toString() {
		return "HeroNode [no=" + no + ", name=" + name + ", nickname=" + nickname + "]";
	}
}

  • 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
发出的红包

打赏作者

1while(true){learn}

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值