c语言数据结构航班信息的录入

//main.c
#include "fligh.h"

int main()
{
    while(1)
    {
        Welcome();
        char ch = getch();// 读取字符

        switch(ch)
        {
            case '1':// 录入
                Import_fligh();
                break;
            case '2':// 显示
                break;
            case '3':// 按要求查找
                break;
            case '4':// 信息排序
                break;
            case '0':// 退出
                break;
        }
    }

    return 0;
}




```c

```c
//fligh.c
#include "fligh.h"

// 欢迎
void Welcome()
{
    printf("\tWelcome to Flight System\t\n");
    printf("==========================================\n");
    printf("[1]Import [2]Print [3]Find [4]Sort [0]Quit\n");
    printf("==========================================\n");
}

// 录入航班信息
void Import_fligh()
{
    // 创建一个新的节点
    fligh_node *head = malloc(sizeof(fligh_node));
    fligh *pnew = malloc(sizeof(fligh));
    head->next = NULL;

    // 头插法
    if(head == NULL)
    {
        head = pnew;
    }
    else
    {
        head = head->next;
    }
    
    printf("输入航班号:\n");
    scanf("%s",head->data.number);
    printf("输入起点站:\n");
    scanf("%s",head->data.staddress);
    printf("输入终点站:\n");
    scanf("%s",head->data.arraddress);
    printf("输入班期:\n");
    scanf("%s",head->data.date);
    printf("输入机型:\n");
    scanf("%s",head->data.type);
    printf("输入起飞时间:\n");
    scanf("%s",head->data.stime);
    printf("输入到达时间:\n");
    scanf("%s",head->data.atime);
    printf("输入航票价:\n");
    scanf("%s",head->data.price);

    printf("信息录入成功\n");
    system("pause");// 暂停
    system("cls");// 清屏
}

```c
fligh.h
#ifndef __FLIGHT_H
#define __FLIGHT_H
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

typedef struct fligh
{
    char number[10];// 航班号
    char staddress[10];// 起点站
    char arraddress[10];// 终点站
    char date[10];// 班期
    char type;// 机型
    int stime;// 起飞时间
    int atime;// 到达时间
    float price;// 票价
}fligh;

typedef struct fligh_node
{
    fligh data;
    struct fligh_node *next;
}fligh_node;

//欢迎
void Welcome();
// 录入航班信息
void Import_fligh();

#endif

求大佬解答,应该是创建节点有问题,但是本人找不出来


```

整体修改完善如下,改动处见注释,供参考:

//fligh.h
#ifndef __FLIGHT_H
#define __FLIGHT_H
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

typedef struct fligh
{
    char number[10];// 航班号
    char staddress[10];// 起点站
    char arraddress[10];// 终点站
    char date[10];// 班期
    char type[16];// 机型         char type; 修改
    char stime[10];// 起飞时间    int  stime;修改
    char atime[10];// 到达时间    int  atime;修改
    float price;// 票价
}fligh;

typedef struct fligh_node
{
    fligh  *data;           // 修改     
    struct fligh_node* next;
}fligh_node;

fligh_node* Head = NULL;    // 修改

//欢迎
void Welcome();
// 录入航班信息
void Import_fligh();
//显示航班信息
void print();              // 修改

#endif

//---------------------------------------------


//main.c
#include "fligh.h"

int main()
{
    while (1)
    {
        Welcome();
        char ch = getch();// 读取字符

        switch (ch)
        {
        case '1':// 录入
            Import_fligh();
            break;
        case '2':// 显示
            print();
            break;
        case '3':// 按要求查找
            break;
        case '4':// 信息排序
            break;
        case '0':// 退出
            break;
        }
    }

    return 0;
}


//----------------------------------------------


//fligh.c
#include "fligh.h"

// 欢迎
void Welcome()
{
    printf("\tWelcome to Flight System\t\n");
    printf("==========================================\n");
    printf("[1]Import [2]Print [3]Find [4]Sort [0]Quit\n");
    printf("==========================================\n");
}

// 录入航班信息
void Import_fligh()
{
    // 创建一个新的节点
    if (!Head) {                                       // 修改
        Head = (fligh_node*)malloc(sizeof(fligh_node));// 修改
        Head->data = NULL;                             // 修改
        Head->next = NULL;
    }
    
    fligh_node* pnew = (fligh_node*)malloc(sizeof(fligh_node));// 修改
    pnew->data = (fligh*)malloc(sizeof(fligh));                // 修改   
    pnew->next = NULL;

    // 头插法
    pnew->next = Head->next;   // 修改
    Head->next = pnew;         // 修改

    printf("输入航班号:\n");
    scanf("%s", pnew->data->number);
    printf("输入起点站:\n");
    scanf("%s", pnew->data->staddress);
    printf("输入终点站:\n");
    scanf("%s", pnew->data->arraddress);
    printf("输入班期:\n");
    scanf("%s", pnew->data->date);
    printf("输入机型:\n");
    scanf("%s", pnew->data->type);
    printf("输入起飞时间:\n");
    scanf("%s", pnew->data->stime);
    printf("输入到达时间:\n");
    scanf("%s", pnew->data->atime);
    printf("输入航票价:\n");
    scanf("%f", &pnew->data->price);  //scanf("%s", pnew->data->price); 修改

    printf("信息录入成功\n");
    system("pause");// 暂停
    system("cls");// 清屏
}

void print()                          // 修改
{
    fligh_node* pt = NULL;
    if (!Head || !Head->next)
        printf("NULL\n");
    else {
        pt = Head->next;
        printf("%-10s %-10s %-10s %-10s %-16s %-10s %-10s %-10s\n",
            "航班号", "起点站", "终点站", "班期", "机型", "起飞时间", "到达时间", "票价");
        while (pt) {
            printf("%-10s %-10s %-10s %-10s %-16s %-10s %-10s %-10.2f\n", 
                pt->data->number,pt->data->staddress, pt->data->arraddress, pt->data->date, 
                pt->data->type,  pt->data->stime, pt->data->atime, pt->data->price);
            pt = pt->next;
        }
    }
}

// Import_fligh
if (head == NULL)
{
    head->data = pnew; // 不是 head = pnew;
}

【以下回答由 GPT 生成】

// main.c
#include "fligh.h"

int main()
{
    fligh_node *head = NULL; //定义一个头指针,初始为空

    while(1)
    {
        Welcome();
        char ch = getch(); // 读取字符

        switch(ch)
        {
            case '1': // 录入
                head = Import_fligh(head);
                break;
            case '2': // 显示
                Print_fligh(head);
                break;
            case '3': // 按要求查找
                Find_fligh(head);
                break;
            case '4': // 信息排序
                head = Sort_fligh(head);
                break;
            case '0': // 退出
                Free_fligh(head); // 释放内存
                exit(0); // 退出程序
                break;
            default:
                printf("Invalid input. Please try again.\n");
                break;
        }
    }

    return 0;
}


// fligh.c
#include "fligh.h"

// 欢迎
void Welcome()
{
    printf("\tWelcome to Flight System\n");
    printf("=================================\n");
    printf("[1] Import [2] Print [3] Find [4] Sort [0] Quit\n");
    printf("=================================\n");
}

// 录入航班信息
fligh_node* Import_fligh(fligh_node *head)
{
    // 创建一个新的节点
    fligh_node *pnew = malloc(sizeof(fligh_node));
    pnew->fligh = malloc(sizeof(fligh));
    pnew->next = NULL;

    // 输入航班信息
    printf("Please enter the flight number: ");
    scanf("%s", pnew->fligh->flight_number);
    printf("Please enter the departure airport: ");
    scanf("%s", pnew->fligh->departure_airport);
    printf("Please enter the destination airport: ");
    scanf("%s", pnew->fligh->destination_airport);
    printf("Please enter the departure time: ");
    scanf("%s", pnew->fligh->departure_time);
    printf("Please enter the arrival time: ");
    scanf("%s", pnew->fligh->arrival_time);

    // 头插法插入新节点
    if(head == NULL)
    {
        head = pnew;
    }
    else
    {
        pnew->next = head;
        head = pnew;
    }

    return head;
}

// 显示航班信息
void Print_fligh(fligh_node *head)
{
    if(head == NULL)
    {
        printf("No flight information.\n");
    }
    else
    {
        fligh_node *p = head;
        while(p != NULL)
        {
            printf("Flight Number: %s\n", p->fligh->flight_number);
            printf("Departure Airport: %s\n", p->fligh->departure_airport);
            printf("Destination Airport: %s\n", p->fligh->destination_airport);
            printf("Departure Time: %s\n", p->fligh->departure_time);
            printf("Arrival Time: %s\n", p->fligh->arrival_time);
            printf("=================================\n");
            p = p->next;
        }
    }
}

// 按要求查找航班信息
void Find_fligh(fligh_node *head)
{
    if(head == NULL)
    {
        printf("No flight information.\n");
    }
    else
    {
        char search_flight_number[100];
        printf("Please enter the flight number you want to find: ");
        scanf("%s", search_flight_number);

        fligh_node *p = head;
        int find_count = 0; // 记录找到的次数

        while(p != NULL)
        {
            if(strcmp(p->fligh->flight_number, search_flight_number) == 0)
            {
                printf("Flight Number: %s\n", p->fligh->flight_number);
                printf("Departure Airport: %s\n", p->fligh->departure_airport);
                printf("Destination Airport: %s\n", p->fligh->destination_airport);
                printf("Departure Time: %s\n", p->fligh->departure_time);
                printf("Arrival Time: %s\n", p->fligh->arrival_time);
                printf("=================================\n");
                find_count++;
            }
            p = p->next;
        }

        if(find_count == 0)
        {
            printf("No flight with the given flight number.\n");
        }
    }
}

// 排序航班信息
fligh_node* Sort_fligh(fligh_node *head)
{
    if(head == NULL)
    {
        printf("No flight information.\n");
        return NULL;
    }

    int count = 0; // 记录节点个数

    // 统计节点个数
    fligh_node *p = head;
    while(p != NULL)
    {
        count++;
        p = p->next;
    }

    // 将所有节点存储到一个数组中
    fligh_node **array = malloc(count * sizeof(fligh_node*));
    p = head;

    for(int i=0; i<count; i++)
    {
        array[i] = p;
        p = p->next;
    }

    // 对数组中的节点进行排序(这里按航班号的字典序进行排序)
    for(int i=0; i<count-1; i++)
    {
        for(int j=0; j<count-i-1; j++)
        {
            if(strcmp(array[j]->fligh->flight_number, array[j+1]->fligh->flight_number) > 0)
            {
                fligh_node *temp = array[j];
                array[j] = array[j+1];
                array[j+1] = temp;
            }
        }
    }

    // 更新链表节点的指向关系
    for(int i=0; i<count-1; i++)
    {
        array[i]->next = array[i+1];
    }
    array[count-1]->next = NULL;

    // 更新头指针
    head = array[0];

    free(array);

    return head;
}

// 释放链表内存
void Free_fligh(fligh_node *head)
{
    fligh_node *p = head;

    while(p != NULL)
    {
        fligh_node *temp = p;
        p = p->next;
        free(temp->fligh);
        free(temp);
    }
}


【相关推荐】



如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^