01-单链表

#include <iostream>
using namespace std;
typedef struct LNode{
    int data;
    struct LNode *next;
}LNode,*LinkList;

//带头节点的单链表的初始化
bool InitList(LinkList &L){
    L=(LNode *)malloc(sizeof(LNode));
    if(L==NULL){
        return false;
    }
    L->next=NULL;
    return true;
}
//按位序插入(带头结点)
bool ListInsert(LinkList &L,int i,int e){
    if(i<1){
        return false;//不合法
    }
    LNode *p=L;
    int j=0;
    for(j=0;j<i-1&&p!=NULL;j++){
        p=p->next;
    }
    if(p==NULL){
        return false;
    }
    LNode *s=(LNode *)malloc(sizeof(LNode));
    s->data=e;
    s->next=p->next;
    p->next=s;
    return true;
}
//指定结点的后插,在p结点后插入元素e
bool ListInsert2(LNode *p,int e){
    if(p==NULL){
        return false;
    }
    LNode *s=(LNode *)malloc(sizeof(LNode));
    s->data=e;
    s->next=p->next;
    p->next=s;
    return true;
}
//打印链表中的元素
void printList(LinkList L){
    LNode *p=L->next;
    while(p!=NULL){
        cout<<p->data<<",";
        p=p->next;
    }
}
int main()
{
    LinkList L;
    InitList(L);
    ListInsert(L,1,1);
    ListInsert2(L->next,2);
    printList(L);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个基于单链表的火车票信息管理系统的实现,包括增加、删除、修改和查询火车票信息的功能: ```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 ----------------------- ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值