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。以下の変更されたコードを見つけて、実行してみてください。main関数では、リストの先頭へのポインターを宣言し、に設定してから、値をパラメーターとして関数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されたアドレスは、のローカル変数headではなく、AddEndのローカル変数に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 に答える