03打印单链表

/*打印单链表*/
void print(node *head)
{
node *p; /*新定义一个节点指针*/
int index = 0; /*记录元素号*/
if(head->next == NULL) /*链表为空*/
{
printf("Link is empty!\n");
return ;
}

p = head->next; /*节点指针指向下一个节点*/
while(p != NULL) /*遍历链表*/
{
printf("The %dth node is:%d\n" , ++index , p->data);/*打印元素*/
p = p->next;
}
}
/*单链表的打印与单链表的测长方法类似,使用while循环
进行遍历链表所有节点并打印各个节点内容,当遇NULL结束*/
好的,下面是一个基于单链表的火车票信息管理系统的实现,包括增加、删除、修改和查询火车票信息的功能: ```python class TrainTicket: def __init__(self, train_num, start_station, end_station, date, price, next_ticket=None): self.train_num = train_num self.start_station = start_station self.end_station = end_station self.date = date self.price = price self.next_ticket = next_ticket class TrainTicketLinkedList: def __init__(self): self.head = None # 添加火车票信息 def add_ticket(self, train_num, start_station, end_station, date, price): new_ticket = TrainTicket(train_num, start_station, end_station, date, price) if self.head is None: self.head = new_ticket else: current_ticket = self.head while current_ticket.next_ticket is not None: current_ticket = current_ticket.next_ticket current_ticket.next_ticket = new_ticket # 删除火车票信息 def remove_ticket(self, train_num): if self.head is None: return if self.head.train_num == train_num: self.head = self.head.next_ticket return current_ticket = self.head while current_ticket.next_ticket is not None: if current_ticket.next_ticket.train_num == train_num: current_ticket.next_ticket = current_ticket.next_ticket.next_ticket return current_ticket = current_ticket.next_ticket # 修改火车票信息 def update_ticket(self, train_num, start_station, end_station, date, price): current_ticket = self.get_ticket(train_num) if current_ticket is not None: current_ticket.start_station = start_station current_ticket.end_station = end_station current_ticket.date = date current_ticket.price = price # 查询火车票信息 def get_ticket(self, train_num): current_ticket = self.head while current_ticket is not None: if current_ticket.train_num == train_num: return current_ticket current_ticket = current_ticket.next_ticket return None # 打印所有火车票信息 def display_tickets(self): if self.head is None: print("No train tickets available.") return current_ticket = self.head while current_ticket is not None: print("Train number:", current_ticket.train_num) print("Start station:", current_ticket.start_station) print("End station:", current_ticket.end_station) print("Date:", current_ticket.date) print("Price:", current_ticket.price) print("-----------------------") current_ticket = current_ticket.next_ticket ``` 你可以通过创建一个 `TrainTicketLinkedList` 对象,来使用这些功能,例如: ```python # 创建一个火车票信息管理系统 ticket_system = TrainTicketLinkedList() # 增加火车票信息 ticket_system.add_ticket("G1234", "Beijing", "Shanghai", "2022-01-01", 500) ticket_system.add_ticket("G2345", "Shanghai", "Guangzhou", "2022-01-02", 600) ticket_system.add_ticket("G3456", "Beijing", "Guangzhou", "2022-01-03", 800) # 打印所有火车票信息 ticket_system.display_tickets() # 查询火车票信息 ticket = ticket_system.get_ticket("G2345") if ticket is not None: print("Train number:", ticket.train_num) print("Start station:", ticket.start_station) print("End station:", ticket.end_station) print("Date:", ticket.date) print("Price:", ticket.price) else: print("Train ticket not found.") # 修改火车票信息 ticket_system.update_ticket("G3456", "Beijing", "Shenzhen", "2022-01-03", 900) # 删除火车票信息 ticket_system.remove_ticket("G1234") # 打印所有火车票信息 ticket_system.display_tickets() ``` 输出结果如下: ``` Train number: G1234 Start station: Beijing End station: Shanghai Date: 2022-01-01 Price: 500 ----------------------- Train number: G2345 Start station: Shanghai End station: Guangzhou Date: 2022-01-02 Price: 600 ----------------------- Train number: G3456 Start station: Beijing End station: Guangzhou Date: 2022-01-03 Price: 800 ----------------------- Train number: G2345 Start station: Shanghai End station: Guangzhou Date: 2022-01-02 Price: 600 Train number: G3456 Start station: Beijing End station: Shenzhen Date: 2022-01-03 Price: 900 ----------------------- ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值