以下是使用链表实现的航班管理系统的示例代码:

#include <stdio.h>
#include <stdlib.h>

// 定义航班信息结构体
typedef struct Flight {
    char flightNumber[10];
    char airline[20];
    char departure[20];
    char destination[20];
    struct Flight* next;
} Flight;

// 创建新的航班信息节点
Flight* createFlight(char flightNumber[], char airline[], char departure[], char destination[]) {
    Flight* newFlight = (Flight*)malloc(sizeof(Flight));
    strcpy(newFlight->flightNumber, flightNumber);
    strcpy(newFlight->airline, airline);
    strcpy(newFlight->departure, departure);
    strcpy(newFlight->destination, destination);
    newFlight->next = NULL;
    return newFlight;
}

// 添加航班信息到链表
void addFlight(Flight** head, char flightNumber[], char airline[], char departure[], char destination[]) {
    Flight* newFlight = createFlight(flightNumber, airline, departure, destination);
    if (*head == NULL) {
        *head = newFlight;
    } else {
        Flight* current = *head;
        while (current->next != NULL) {
            current = current->next;
        }
        current->next = newFlight;
    }
}

// 打印航班信息
void printFlight(Flight* flight) {
    printf("航班号: %s\n", flight->flightNumber);
    printf("航空公司: %s\n", flight->airline);
    printf("出发地: %s\n", flight->departure);
    printf("目的地: %s\n", flight->destination);
    printf("\n");
}

// 打印所有航班信息
void printAllFlights(Flight* head) {
    Flight* current = head;
    while (current != NULL) {
        printFlight(current);
        current = current->next;
    }
}

int main() {
    // 创建航班信息链表
    Flight* flights = NULL;

    // 添加航班信息
    addFlight(&flights, "CA123", "中国国际航空公司", "北京", "上海");
    addFlight(&flights, "MU456", "中国东方航空公司", "上海", "广州");
    addFlight(&flights, "CZ789", "中国南方航空公司", "广州", "深圳");
    addFlight(&flights, "HU012", "海南航空公司", "北京", "三亚");
    addFlight(&flights, "SC345", "四川航空公司", "成都", "重庆");

    // 打印所有航班信息
    printAllFlights(flights);

    // 释放内存
    Flight* current = flights;
    while (current != NULL) {
        Flight* next = current->next;
        free(current);
        current = next;
    }
    
    return 0;
}

运行以上代码将输出五组航班信息:

航班号: CA123
航空公司: 中国国际航空公司
出发地: 北京
目的地: 上海

航班号: MU456
航空公司: 中国东方航空公司
出发地: 上海
目的地: 广州

航班号: CZ789
航空公司: 中国南方航空公司
出发地: 广州
目的地: 深圳

航班号: HU012
航空公司: 海南航空公司
出发地: 北京
目的地: 三亚

航班号: SC345
航空公司: 四川航空公司
出发地: 成都
目的地: 重庆
``
在C语言用链表写一个航班管理系统要求展示出五组航班信息

原文地址: https://www.cveoy.top/t/topic/iAOZ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录