无法在C中创建正确的链表

时间:2013-03-20 06:36:04

标签: c

我想构建只有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;
    }
}

3 个答案:

答案 0 :(得分:2)

你写的代码完全混乱。我不知道为什么你需要在那里使用变量item。找到下面的修改代码并尝试执行它。在main函数中,您声明指向列表头部的指针,将其设置为NULL,然后将NULL值作为参数发送到AddEnd函数。那样不行。您需要将&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;
    }
}

答案 1 :(得分:1)

问题是这个

AddEnd(List * item,List* head)

这时你像这样调用AddEnd

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

malloc'd地址被复制到AddEnd中的局部变量head,而不是head的局部变量main()。主()中的头部没有变化。

溶液:

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

致电AddEnd(&item,&head);

答案 2 :(得分: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;
  }
}