//乘客节点 typedef struct CustomerNode { char name[10];//客户姓名 int clientTickets;//客户订票量 char identification[20];//客户身份证号码 int rank;//座位等级 CustomerNode *next; } CustomerNode, *CusLinkList; //候补队列中的节点 typedef struct WaitPassenger { char name[10];//姓名 char identification[20]; //身份证 int preTickets;//预定的票量 struct WaitPassenger *next; } WaitQNode, *PWait; //候补队列 typedef struct Queue { PWait front;//等候替补客户名单域的头指针 PWait rear;//等候替补客户名单域的尾指针 } LinkQueue; //封装乘客的姓名和订票量和身份证 //用于候补客户出队时把关键字返回 typedef struct NameAndNumAndID { char name[10];//姓名 char identification[20]; //身份证号码 int num;//订票量 } NameAndNumAndID; //车次节点 typedef struct Flight { char startPoint[20];//起点站名 char destination[20];//终点站名 char flightCodeID[20];//车次ID(相当于主键) char planeNum[20];//列车号 char day[20];//出发日期(星期几) int totalTickets;//乘员定额(总票数) int left;//总余票量 int leftEconomicTicket; //二等座剩余量 int leftBusinessTicket; //一等座剩余量 Flight *next; CusLinkList cusLinkList;//乘员名单域,指向乘员名单链表的头指针 LinkQueue waitQueue1;//二等座候补,等候替补的客户名单域,指向一个队列 LinkQueue waitQueue2;//一等座候补,等候替补的客户名单域,指向一个队列 } Flight, FlightNode, *PFlight; //定义全局指针变量pFlight,车次链表的头指针 Flight *pFlight; 用c语言实现该结构体的文件读写操作并给出具体代码
时间: 2023-08-28 20:05:21 浏览: 113
以下是基于C语言的文件读写操作的代码示例:
写入文件:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "your_header_file.h" // 包含定义的结构体
void saveToFile(char *filename, Flight *pFlight) {
FILE *fp = fopen(filename, "wb");
if (fp == NULL) {
printf("Open file failed!\n");
exit(1);
}
// 写入车次节点信息
while (pFlight != NULL) {
fwrite(pFlight, sizeof(Flight), 1, fp);
// 写入乘员名单链表信息
CustomerNode *pCus = pFlight->cusLinkList;
while (pCus != NULL) {
fwrite(pCus, sizeof(CustomerNode), 1, fp);
pCus = pCus->next;
}
// 写入二等座候补名单信息
PWait pWait1 = pFlight->waitQueue1.front;
while (pWait1 != NULL) {
fwrite(pWait1, sizeof(WaitQNode), 1, fp);
pWait1 = pWait1->next;
}
// 写入一等座候补名单信息
PWait pWait2 = pFlight->waitQueue2.front;
while (pWait2 != NULL) {
fwrite(pWait2, sizeof(WaitQNode), 1, fp);
pWait2 = pWait2->next;
}
pFlight = pFlight->next;
}
fclose(fp);
}
```
读取文件:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "your_header_file.h" // 包含定义的结构体
void loadFromFile(char *filename, Flight **ppFlight) {
FILE *fp = fopen(filename, "rb");
if (fp == NULL) {
printf("Open file failed!\n");
exit(1);
}
FlightNode *pHead = NULL, *pTail = NULL;
Flight *pFlight = NULL;
while (1) {
pFlight = (Flight *) malloc(sizeof(Flight));
if (fread(pFlight, sizeof(Flight), 1, fp) <= 0) {
free(pFlight);
break;
}
// 读取乘员名单链表信息
CustomerNode *pCusHead = NULL, *pCusTail = NULL;
for (int i = 0; i < pFlight->totalTickets; i++) {
CustomerNode *pCus = (CustomerNode *) malloc(sizeof(CustomerNode));
if (fread(pCus, sizeof(CustomerNode), 1, fp) <= 0) {
free(pCus);
break;
}
pCus->next = NULL;
if (pCusHead == NULL) {
pCusHead = pCus;
pCusTail = pCus;
} else {
pCusTail->next = pCus;
pCusTail = pCus;
}
}
pFlight->cusLinkList = pCusHead;
// 读取二等座候补名单信息
PWait pWait1Head = NULL, pWait1Tail = NULL;
for (int i = 0; i < pFlight->leftEconomicTicket; i++) {
PWait pWait1 = (PWait) malloc(sizeof(WaitQNode));
if (fread(pWait1, sizeof(WaitQNode), 1, fp) <= 0) {
free(pWait1);
break;
}
pWait1->next = NULL;
if (pWait1Head == NULL) {
pWait1Head = pWait1;
pWait1Tail = pWait1;
} else {
pWait1Tail->next = pWait1;
pWait1Tail = pWait1;
}
}
pFlight->waitQueue1.front = pWait1Head;
// 读取一等座候补名单信息
PWait pWait2Head = NULL, pWait2Tail = NULL;
for (int i = 0; i < pFlight->leftBusinessTicket; i++) {
PWait pWait2 = (PWait) malloc(sizeof(WaitQNode));
if (fread(pWait2, sizeof(WaitQNode), 1, fp) <= 0) {
free(pWait2);
break;
}
pWait2->next = NULL;
if (pWait2Head == NULL) {
pWait2Head = pWait2;
pWait2Tail = pWait2;
} else {
pWait2Tail->next = pWait2;
pWait2Tail = pWait2;
}
}
pFlight->waitQueue2.front = pWait2Head;
// 加入车次链表
FlightNode *pNode = (FlightNode *) malloc(sizeof(FlightNode));
pNode->data = *pFlight;
pNode->next = NULL;
if (pHead == NULL) {
pHead = pNode;
pTail = pNode;
} else {
pTail->next = pNode;
pTail = pNode;
}
}
fclose(fp);
*ppFlight = pHead->data;
free(pHead);
}
```
注意:在写入文件和读取文件时,需要按照链表的顺序一个一个写入或读出节点的信息,而且链表中每个节点的next指针在文件中并不是一个有效的地址,因此需要特殊处理。此外,在写入和读取文件时,需要注意在文件头中记录链表的长度等信息,以便正确地读取和写入数据。
阅读全文