C在使用内核链表时遇到问题

时间:2016-02-25 12:01:15

标签: c linked-list

我正在尝试在我的航班信息系统程序中使用此文件list.h

但是,当我尝试将新节点链接到链表的第一个节点时,我的程序似乎有些麻烦。

这是我的代码

#include somefiles balabala


typedef struct flight {
        char ID[10];
        char departure[20];
        char arrival[20];
        char dep_date[8];
        char dep_time[4];
        char arr_time[4];
        float price;
        struct list_head list;
} flight;

typedef struct node {
        flight info;
        struct list_head list;
} flight_node, *p_flight;

p_flight init_list () {
        p_flight head = malloc (sizeof (flight_node));
        INIT_LIST_HEAD (&(head->list));
        return head;
}

void add_flight (p_flight node) {
        p_flight new_node;
        new_node = malloc (sizeof(flight_node));

        system ("clear");
        printf (" \e[1;36m>\33[0m Importing new flight info:\n");
        printf (" Please input the \e[1;33mID\33[0m of the filght: ");
        scanf ("%s", new_node->info.ID);
        printf (" Please input the \e[1;33mdeparture\33[0m of the filght: ");
        scanf ("%s", new_node->info.departure);
        printf (" Please input the \e[1;33marrival\33[0m of the filght: ");
        scanf ("%s", new_node->info.arrival);
        printf (" Please input the \e[1;33mdeparture date\33[0m of the filght: ");
        scanf ("%s", new_node->info.dep_date);
        printf (" Please input the \e[1;33mdeparture time\33[0m of the filght: ");
        scanf ("%s", new_node->info.dep_time);
        printf (" Please input the \e[1;33marrival time\33[0m of the filght: ");
        scanf ("%s", new_node->info.arr_time);
        printf (" Please input the \e[1;33mprice\33[0m of the filght: ");
        scanf ("%f", new_node->info.price);
        list_add (&(new_node->list), &(node->list));    // Seems the problem will have a **segment fault** here
}

int main (void) {
        p_flight list = init_list();
        add_flight (list);
        return 0;
}

我不确定是什么原因引起的。 是否我访问内容 new_node->列表导致此或其他任何内容?

谢谢!

0 个答案:

没有答案