0

我想构建只有 add_to_end 和 show_list 函数的链表,但是当我想显示时我的列表崩溃了,head虽然item有效(查看代码)。

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

typedef struct Data
{
    int x;
    int y;
    struct Data * next;   
}List;

void AddEnd(List * item, List * head);
void Show(List * head);

int main(void)
{
    int choice;
    List item;
    List * head;
    List * temp;
    head = NULL;
    while(printf("q - quit - Enter 1 add end, 2 show: "), scanf("%d", &choice))
    {
        switch(choice)
        {
            case 1:
                AddEnd(&item, head);
                break;
            case 2:
                printf("X = %d y= %d\n", item.x, item.y);               /*prints 1 2*/
                printf("x = %d y = %d\n", head->x, head->y);            /*crash of program...should print 1 2*/
                Show(head);
                break;
            default:
                printf("TRY AGAIN\n");
                break;           
        }
    }
    temp = head;
    while(temp)
    {
        free(temp);
        temp = head->next;
        head = temp->next;
    }
    return 0;
}

void AddEnd(List * item, List * head)
{
    List * node;
    node = (List *)malloc(sizeof(List));
    printf("Enter x and y: ");
    scanf("%d %d", &node->x, &node->y);
    if(head == NULL)
    {
        node->next = NULL;
        head = node;
        * item = * head;

    }
    else
    {
        item->next = node;
        node->next = NULL;
    }
}

void Show(List * head)
{
    List * node;
    node = head;
    while(node)
    {
        printf("x = %d y = %d\n", node->x, node->y);
        node = node->next;
    }
}
4

3 回答 3

2

你写的代码是一团糟。我不确定你为什么需要在item那里使用变量。在下面找到修改后的代码并尝试执行它。在主函数中,您要声明指向列表头部的指针,将其设置为NULL,然后将NULLAddEnd作为参数发送给函数。那不管用。您需要将 发送&head到函数作为参数,以便将更改反映回调用函数。

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

typedef struct Data
{
    int x;
    int y;
    struct Data * next;   
}List;

void AddEnd(List * item, List ** head);
void Show(List * head);

int main(void)
{
    int choice;
    List item;
    List * head;
    List * temp;
    head = NULL;
    while(printf("q - quit - Enter 1 add end, 2 show: "), scanf("%d", &choice))
    {
        switch(choice)
        {
            case 1:
                AddEnd(&item, &head);
                break;
            case 2:
              //  printf("X = %d y= %d\n", item.x, item.y);               /*prints 1 2*/
              //  printf("x = %d y = %d\n", head->x, head->y);            /*crash of program...should print 1 2*/
                Show(head);
                break;
            default:
                printf("TRY AGAIN\n");
                break;           
        }
    }
    temp = head;
    while(temp)
    {
        free(temp);
        temp = head->next;
        head = temp->next;
    }
    return 0;
}

void AddEnd(List * item, List ** head)
{
    List * node,*first,*second;
    node = (List *)malloc(sizeof(List));
    printf("Enter x and y: ");
    scanf("%d %d", &node->x, &node->y);
    if(*head == NULL)
    {
        node->next = NULL;
        *head = node;
       // * item = **head;

    }
    else
    {
        for(first=*head;first!=NULL;first=first->next)//traverse to the end of the list
            second=first;
        node->next = NULL;
        second->next=node;
    }
}

void Show(List * head)
{
    List * node;
    node = head;
    while(node)
    {
        printf("x = %d y = %d\n", node->x, node->y);
        node = node->next;
    }
}
于 2013-03-20T07:32:51.370 回答
1

问题是这个

AddEnd(List * item,List* head)

当您像这样调用 AddEnd 时

head = null; addEnd(&item,head);

malloc 的地址被复制到headAddEnd 中的局部变量,而head不是main(). main() 中的 head 保持不变。

解决方案:

像这样更改 AddEnd AddEnd(List * item,List ** head)并在 AddEnd 中类似地更改代码。

称呼AddEnd(&item,&head);

于 2013-03-20T07:42:06.737 回答
0
void AddEnd(List * item, List * head)
{
     List * node;
     node = (List *)malloc(sizeof(List));
     printf("Enter x and y: ");
     scanf("%d %d", &node->x, &node->y);
   if(head == NULL)
   {
      head=node;
      head->next=NULL;
     // head->x=item->x;
     // head->y=item->y; you don'tneed this as you have it in node.
   } 
  else
  {
     node->next = head->next;
     head->next = node;
    // node->x=item->x;
    // node->y=item->y;
  }
}
于 2013-03-20T06:43:05.143 回答