将结构添加到链表似乎不起作用

时间:2018-04-14 13:31:30

标签: c struct linked-list

所以我在C中练习,学习操纵数据结构。我在函数中使用结构/指针进行了一些努力。我试图逐步完成,我可以看到我正在创建新的结构,但我要么在相同的内存位置创建它们,要么创建一个包含多个链接列表的链接列表相同的结构!我无法解决发生的事情。

我试图按照要求简化我的代码,现在这包含了重现我的问题所需的所有代码(我相信最小?)。

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

typedef struct aircraft {
    char FlightNumber[9]; // Unique aircraft ID
    struct aircraft * next; // A pointer to the next aircraft in the current queue
} AIRCRAFT;

AIRCRAFT *AirQueue = NULL;
AIRCRAFT *Current = NULL;

void GenerateFlightNumber(AIRCRAFT* node) {

    char FlightNumber[10] = ""; // Stores the flight number for the duration of this function
    int random = 0; // Stores the random number used to generate the flight number prefix and suffix

    // Generates the prefix
    srand((unsigned)time(NULL)); // Uses current time as seed for random generator
    random = rand() % 10; // Generates a random number between 0 and 9
    char prefix[10][5] = { "BA","ELAL","SHT","EXS","EZY","TOM","RYR","MON","UAE","TFL" }; // Array of airline prefixes
    strcpy_s(FlightNumber, sizeof(FlightNumber), prefix[random]); // Copies a prefix to the FlightNumber variable by selecting one using a random index number

    // Generates the suffix
    char suffix[5]; // Stores the flight number suffix
    srand((unsigned)time(NULL)); // Uses current time as seed for random generator
    random = (rand() % 8888) + 1111; // Generate a random number between 1111 and 9999
    _itoa_s(random, suffix, 5, 10); // Converts the randomly generated suffix to a string, and stores it in the suffix variable
    strcat_s(FlightNumber, sizeof(FlightNumber), suffix); // Concatenates the prefix and suffix to the FlightNumber variable

    strcpy_s(node->FlightNumber, sizeof(node->FlightNumber), FlightNumber); // Assign the final flight number to the new aircraft
}

void setUpAircraft(AIRCRAFT * node, bool ground) {
    GenerateFlightNumber(node);
}

AIRCRAFT* StartAirQueue()
{
    printf("\nCreating Air Queue...");
    AIRCRAFT *Temporary = (AIRCRAFT*)malloc(sizeof(AIRCRAFT));
    if (Temporary == NULL)
    {
        printf("\nFailed to allocate memory\n");
        return NULL;
    }
    setUpAircraft(Temporary, false);
    Temporary->next = NULL;

    AirQueue = Current = Temporary;
    return Temporary;
}

AIRCRAFT* AddToAirQueue(bool end)
{
    if (NULL == AirQueue)
    {
        return (StartAirQueue());
    }

    if (end)
        printf("\nAdding node to end of queue...");
    else
        printf("\n Adding node to beginning of queue...");

    AIRCRAFT *Temporary = (AIRCRAFT*)malloc(sizeof(AIRCRAFT));
    if (NULL == Temporary)
    {
        printf("\n Node creation failed \n");
        return NULL;
    }
    setUpAircraft(Temporary, false);
    Temporary->next = NULL;

    if (end)
    {
        Current->next = Temporary;
        Current = Temporary;
    }
    else
    {
        Temporary->next = AirQueue;
        AirQueue = Temporary;
    }
    return Temporary;
}

void print_list(void)
{
    AIRCRAFT *ptr = AirQueue;

    printf("\n -------Printing list Start------- \n");
    while (ptr != NULL)
    {
        printf("\n [%s] \n", ptr->FlightNumber);
        ptr = ptr->next;
    }
    printf("\n -------Printing list End------- \n");

    return;
}

int main(void)
{
    int i = 0, result = 0;
    AIRCRAFT *ptr = NULL;

    print_list();

    for (i = 5; i < 10; i++)
        AddToAirQueue(true);

    print_list();

    for (i = 4; i > 0; i--)
        AddToAirQueue(false);

    print_list();

    getchar();
    return 0;
}

我想制作一份飞机清单,每个飞机都有不同的航班号。从结果中可以看出,列表中的每个节点似乎都包含相同的航班号。

------Printing list Start-------
------Printing list End------
Creating Air Queue...
Adding node to end of queue...
Adding node to end of queue...
Adding node to end of queue...
Adding node to end of queue...
------Printing list Start-------
[RYR5769]
[RYR5769]
[RYR5769]
[RYR5769]
[RYR5769]
------Printing list End------
Adding node to end of queue...
Adding node to end of queue...
Adding node to end of queue...
Adding node to end of queue...
------Printing list Start-------
[RYR5769]
[RYR5769]
[RYR5769]
[RYR5769]
[RYR5769]
[RYR5769]
[RYR5769]
[RYR5769]
[RYR5769]
[RYR5769]
------Printing list End------

有没有人有任何有用的建议,为什么我的列表似乎重复包含相同的节点?

1 个答案:

答案 0 :(得分:0)

我把它移植到Archlinux上运行,但它不需要花很多钱来转换它。

我添加了几个print语句来显示每个节点的指针地址,你会看到它们相同。

#include<stdio.h>
#include <unistd.h>
#include <time.h>
#include<stdlib.h>
#include<stdbool.h>
#include <string.h>

typedef struct aircraft {
    char FlightNumber[9]; // Unique aircraft ID
    struct aircraft * next; // A pointer to the next aircraft in the current queue
} AIRCRAFT;

AIRCRAFT *AirQueue = NULL;
AIRCRAFT *Current = NULL;

void GenerateFlightNumber(AIRCRAFT* node) {

    char FlightNumber[10] = ""; // Stores the flight number for the duration of this function
    int random = 0; // Stores the random number used to generate the flight number prefix and suffix

    // Generates the prefix
    random = rand() % 10; // Generates a random number between 0 and 9
    printf("Random: %d\n", random);
    char prefix[10][5] = { "BA","ELAL","SHT","EXS","EZY","TOM","RYR","MON","UAE","TFL" }; // Array of airline prefixes
    strncpy(FlightNumber, prefix[random], sizeof(FlightNumber)); // Copies a prefix to the FlightNumber variable by selecting one using a random index number

    // Generates the suffix
    char suffix[5]; // Stores the flight number suffix

    random = (rand() % 8888) + 1111; // Generate a random number between 1111 and 9999
    snprintf(suffix, sizeof(FlightNumber), "%d", random);
    strncat(FlightNumber, suffix, sizeof(FlightNumber)); // Concatenates the prefix and suffix to the FlightNumber variable

    strncpy(node->FlightNumber, FlightNumber, sizeof(node->FlightNumber)); // Assign the final flight number to the new aircraft
}

AIRCRAFT* StartAirQueue()
{
    printf("\nCreating Air Queue...");
    AIRCRAFT *Temporary = (AIRCRAFT*)malloc(sizeof(AIRCRAFT));
    if (Temporary == NULL)
    {
        printf("\nFailed to allocate memory\n");
        return NULL;
    }
    GenerateFlightNumber(Temporary);
    Temporary->next = NULL;
    AirQueue = Current = Temporary;
    return Temporary;
}

AIRCRAFT* AddToAirQueue(bool end)
{
    if (NULL == AirQueue)
    {
        return (StartAirQueue());
    }

    if (end)
        printf("\nAdding node to end of queue...");
    else
        printf("\n Adding node to beginning of queue...");

    AIRCRAFT *Temporary = (AIRCRAFT*)malloc(sizeof(AIRCRAFT));
    if (NULL == Temporary)
    {
        printf("\n Node creation failed \n");
        return NULL;
    }
    GenerateFlightNumber(Temporary); 
    Temporary->next = NULL;

    if (end)
    {
        Current->next = Temporary;
        Current = Temporary;
    }
    else
    {
        Temporary->next = AirQueue;
        AirQueue = Temporary;
    }
    return Temporary;
}

void print_list(void)
{
    AIRCRAFT *ptr = AirQueue;

    printf("\n -------Printing list Start------- \n");
    while (ptr != NULL)
    {
        printf("\n [%s] [%p]\n", ptr->FlightNumber, ptr);
        ptr = ptr->next;
    }
    printf("\n -------Printing list End------- \n");

    return;
}

int main(void)
{
    int i = 0;

    print_list();
    srand((unsigned)time(NULL));
    for (i = 5; i < 10; i++) {
        AddToAirQueue(true);
        sleep(1);
    }
    print_list();

    for (i = 4; i > 0; i--) {
        sleep(1);
        AddToAirQueue(false);
    }

    print_list();

    getchar();
    return 0;
}

正如我从你第一次发布时所说的那样,问题出在你的&#34;随机&#34;功能

这一行特别是:

srand((unsigned)time(NULL)); // Uses current time as seed for random generator

将它移到你的主要部位,因为你只需要调用一次。

您可以在之前的问题中阅读有关seeding的更多信息。

通过使用time(NULL)播种秒数(自纪元以来),您的呼叫连续发生,每次重复给出相同的数字,种子是相同的。