2013-03-20 74 views
0

我想构建仅具有add_to_end和show_list函数的链接列表,但当我想要显示head时,尽管item工作(查看代码),但列表崩溃。无法在C中创建正确的链接列表

#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; 
    } 
} 
+0

尝试改变&节点 - >的x,&节点 - >在加数函数y到node.x&,&node.y 也头戴式> x至&head.x,头戴式> y以&head.y – 2013-03-20 06:38:56

+0

请求成员x的东西不是一个结构或联盟 – balky 2013-03-20 06:44:49

+0

但你有头=节点的功能,对吧,如果头== null – 2013-03-20 07:00:07

回答

2

你写的代码是一个完整的混乱。我不确定为什么你需要在那里使用变量item。找到下面修改的代码并尝试执行它。在主函数中,声明了一个指向列表头的指针,将其设置为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; 
    } 
} 
+0

如果*头== NULL是这样,谢谢你最后 – balky 2013-03-20 07:38:45

+0

欢迎你... :) – 2013-03-20 07:40:23

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; 
    } 
} 
+0

请求成员x的东西不是结构或联盟 – balky 2013-03-20 06:45:18

+0

对不起,head-> x不是&head.x但只有head.x. – 2013-03-20 06:46:48

+0

@balky没有帮助? – 2013-03-20 06:50:59

1

的问题是这样的

AddEnd(List * item,List* head)

在这里,当你调用加数像这样

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

的malloc分配地址将被复制到本地变量head在AddEnd中,而不是局部变量headmain()。在main()中的头不变。

溶液:

变化加数作为这样 AddEnd(List * item,List ** head)和在加数改变代码相若方式也。

呼叫AddEnd(&item,&head);

+0

是的双指针,我知道我没有改变调用函数变量thx – balky 2013-03-20 07:50:03