用C语言链表设计一个航班管理系统实现航班的增删改查和排序
时间: 2024-05-01 12:22:54 浏览: 83
宿舍查询系统C语言版
好的,下面是一个简单的航班管理系统的实现,包括链表的设计、航班的增删改查和排序:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义航班结构体
typedef struct Flight {
char flight_no[20]; // 航班号
char departure[50]; // 出发地
char destination[50]; // 目的地
char date[20]; // 日期
int price; // 价格
struct Flight *next; // 指向下一个节点的指针
} Flight;
// 创建一个新的航班节点
Flight *new_flight(char *flight_no, char *departure, char *destination, char *date, int price) {
Flight *flight = (Flight *)malloc(sizeof(Flight));
strcpy(flight->flight_no, flight_no);
strcpy(flight->departure, departure);
strcpy(flight->destination, destination);
strcpy(flight->date, date);
flight->price = price;
flight->next = NULL;
return flight;
}
// 在链表末尾添加一个航班
void add_flight(Flight **head, Flight *flight) {
if (*head == NULL) {
*head = flight;
} else {
Flight *cur = *head;
while (cur->next != NULL) {
cur = cur->next;
}
cur->next = flight;
}
}
// 删除指定航班号的航班
void delete_flight(Flight **head, char *flight_no) {
Flight *cur = *head;
Flight *prev = NULL;
while (cur != NULL) {
if (strcmp(cur->flight_no, flight_no) == 0) {
if (prev == NULL) {
*head = cur->next;
} else {
prev->next = cur->next;
}
free(cur);
return;
}
prev = cur;
cur = cur->next;
}
}
// 修改指定航班号的航班信息
void update_flight(Flight *head, char *flight_no) {
Flight *cur = head;
while (cur != NULL) {
if (strcmp(cur->flight_no, flight_no) == 0) {
printf("请输入新的出发地: ");
scanf("%s", cur->departure);
printf("请输入新的目的地: ");
scanf("%s", cur->destination);
printf("请输入新的日期: ");
scanf("%s", cur->date);
printf("请输入新的价格: ");
scanf("%d", &cur->price);
return;
}
cur = cur->next;
}
}
// 根据价格从小到大对航班进行排序
void sort_flights(Flight **head) {
Flight *cur, *cur2;
char temp[20];
int temp_price;
for (cur = *head; cur != NULL; cur = cur->next) {
for (cur2 = cur->next; cur2 != NULL; cur2 = cur2->next) {
if (cur->price > cur2->price) {
strcpy(temp, cur->flight_no);
strcpy(cur->flight_no, cur2->flight_no);
strcpy(cur2->flight_no, temp);
strcpy(temp, cur->departure);
strcpy(cur->departure, cur2->departure);
strcpy(cur2->departure, temp);
strcpy(temp, cur->destination);
strcpy(cur->destination, cur2->destination);
strcpy(cur2->destination, temp);
strcpy(temp, cur->date);
strcpy(cur->date, cur2->date);
strcpy(cur2->date, temp);
temp_price = cur->price;
cur->price = cur2->price;
cur2->price = temp_price;
}
}
}
}
// 输出所有航班信息
void print_flights(Flight *head) {
if (head == NULL) {
printf("没有航班信息!\n");
} else {
Flight *cur = head;
printf("航班号\t出发地\t目的地\t日期\t价格\n");
while (cur != NULL) {
printf("%s\t%s\t%s\t%s\t%d\n", cur->flight_no, cur->departure, cur->destination, cur->date, cur->price);
cur = cur->next;
}
}
}
int main() {
Flight *head = NULL;
int choice;
char flight_no[20];
char departure[50];
char destination[50];
char date[20];
int price;
while (1) {
printf("\n请选择操作:\n");
printf("1. 添加航班\n");
printf("2. 删除航班\n");
printf("3. 修改航班信息\n");
printf("4. 排序航班\n");
printf("5. 查看航班信息\n");
printf("6. 退出程序\n");
printf("请输入操作编号: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("请输入航班号: ");
scanf("%s", flight_no);
printf("请输入出发地: ");
scanf("%s", departure);
printf("请输入目的地: ");
scanf("%s", destination);
printf("请输入日期: ");
scanf("%s", date);
printf("请输入价格: ");
scanf("%d", &price);
add_flight(&head, new_flight(flight_no, departure, destination, date, price));
break;
case 2:
printf("请输入要删除的航班号: ");
scanf("%s", flight_no);
delete_flight(&head, flight_no);
break;
case 3:
printf("请输入要修改的航班号: ");
scanf("%s", flight_no);
update_flight(head, flight_no);
break;
case 4:
sort_flights(&head);
break;
case 5:
print_flights(head);
break;
case 6:
printf("谢谢使用!\n");
return 0;
default:
printf("无效操作!\n");
break;
}
}
}
```
这个程序中使用了链表来存储航班信息,每个节点表示一个航班。通过 add_flight 函数可以添加航班,delete_flight 函数可以删除航班,update_flight 函数可以修改航班信息,print_flights 函数可以输出所有航班信息。
另外,sort_flights 函数可以根据价格从小到大对航班进行排序。这个函数使用了冒泡排序算法来实现。
阅读全文