使用链表的航空公司预订系统

时间:2016-10-17 02:02:34

标签: c file pointers visual-studio-2015 linked-list

我在c编程中使用链表进行航空预订系统。它预订座位,查看预订,取消预订。该程序首次正常运行,无需添加任何数据。

但是当我添加数据并想要删除它时,它会在ConsoleApplication1.exe中出现此错误“在0x0FFAFBB3(ucrtbased.dll)处抛出异常:0xC0000005:访问冲突写入位置0x0004B000。”

为了删除数据,我正在比较电话号码数据。当它进行比较时,它会删除记录。但相反,它给出了错误。任何人都可以识别错误区域并提供建议吗?

以下是预订和删除预订的代码。

void new_reservation()
{
    curr = start;
    if (start == NULL)
    {
        //Empty list
        start = curr = (struct passenger *)malloc(sizeof(struct passenger));
        dataentry();
        curr->next = NULL;
        printf("\n\t Reservation successful");
        return;
    }
    //Reach end of list


    while (curr->next)
        curr = curr->next;
    curr->next = (struct passenger *)malloc(sizeof(struct passenger));
    curr = curr->next;
    dataentry();
    curr->next = NULL;
    printf("\n\t Reservation successful");
    printf("\n\t Saved to Reservation list");
}

void del()
{
    struct passenger *temp; // assigning a temporary pointer to struct airplane
    char str[20];
    printf("Enter phone number:");

    gets(str); 
    fflush(stdin);
    curr = start;
    while (curr)
    {
        if (strcmp(start->Phone, str) == 0)
        {
            temp = start;
            start = start->next;
            free(temp);
            return;
        }
        if (strcmp(curr->next->Phone, str) == 0)
        {
            temp = curr->next;
            curr->next = curr->next->next;
            free(temp);
            break;
        }
        else if (strcmp(curr->next->Phone, str) != 0)
        {
            printf("\n\n No reservations found!!");
            break;
        }
    }
    printf("\n\n Deletion successful!!");
}

1 个答案:

答案 0 :(得分:1)

if (strcmp(start->Phone, str) == 0){...}
if (strcmp(curr->next->Phone, str) == 0){...}
else if (strcmp(curr->next->Phone, str) != 0){...}

此方法过于复杂,除非您仔细检查指针和指针的next成员,否则必然会遇到错误。您可以简化此操作并仅进行一次比较。如果要删除的节点是start或者它是另一个节点,请确保跟踪节点的位置。例如:

void del()
{
    char str[20];
    struct passenger *walk = start;
    if (!walk)
        return;

    printf("Enter phone number:");
    scanf("%s", str);

    struct passenger *previous = 0;
    while (walk)
    {
        if (strcmp(walk->Phone, str) == 0)
        {
            struct passenger *next = walk->next;
            free(walk);
            if (previous == 0)
            {
                //deleting the first item
                start = next;
            }
            else
            {
                //deleting an item in middle or end
                previous->next = next;
            }
            return;
        }
        previous = walk;
        walk = walk->next;
    }
}

new_reservation可能不会导致任何错误,但也可以简化。您只需要分配一次,然后在最后插入它,或者将其指定为start。您也可以使用scanf来读取字符串。

void new_reservation()
{
    //create new item
    curr = malloc(sizeof(struct passenger));
    curr->next = NULL;
    //I don't know this part of your code...
    dataentry(); 

    if (start == NULL)
    {
        //Empty list
        start = curr;
    }
    else
    {
        //find the end of the list
        struct passenger *walk = start;
        while (walk->next)
            walk = walk->next;
        walk->next = curr;
    }
}