//乘客节点 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-10 12:07:29 浏览: 117
文件读操作:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct CustomerNode {
char name[10];
int clientTickets;
char identification[20];
int rank;
struct 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];
char planeNum[20];
char day[20];
int totalTickets;
int left;
int leftEconomicTicket;
int leftBusinessTicket;
struct Flight *next;
CusLinkList cusLinkList;
LinkQueue waitQueue1;
LinkQueue waitQueue2;
} Flight, FlightNode, *PFlight;
PFlight pFlight;
void readFromFile() {
FILE *fp = fopen("flights.dat", "rb");
if (fp == NULL) {
return;
}
PFlight currFlight = NULL;
while (!feof(fp)) {
PFlight newFlight = (PFlight)malloc(sizeof(Flight));
memset(newFlight, 0, sizeof(Flight));
fread(newFlight, sizeof(Flight), 1, fp);
if (newFlight->flightCodeID[0] == '\0') {
free(newFlight);
break;
}
newFlight->cusLinkList = NULL;
newFlight->waitQueue1.front = NULL;
newFlight->waitQueue1.rear = NULL;
newFlight->waitQueue2.front = NULL;
newFlight->waitQueue2.rear = NULL;
if (pFlight == NULL) {
pFlight = newFlight;
currFlight = pFlight;
} else {
currFlight->next = newFlight;
currFlight = currFlight->next;
}
}
fclose(fp);
}
```
文件写操作:
```c
void saveToFile() {
FILE *fp = fopen("flights.dat", "wb");
if (fp == NULL) {
return;
}
PFlight currFlight = pFlight;
while (currFlight != NULL) {
fwrite(currFlight, sizeof(Flight), 1, fp);
currFlight = currFlight->next;
}
FlightNode sentinel;
memset(&sentinel, 0, sizeof(FlightNode));
fwrite(&sentinel, sizeof(FlightNode), 1, fp);
fclose(fp);
}
```
以上代码只是一个示例,具体实现可能需要根据实际的需求进行修改。
阅读全文