4

我正在用 C 语言为“单链表”编写代码。在这段代码中,我想在列表的末尾插入元素。它编译得很好。但是在运行时,预期的输出不会出现。我正在gcc用作编译器。每当我./a.out在终端上做事时,它都会被绞死。
这是代码:

#include<stdio.h>
#include<stdlib.h>
struct list
{
    int node;
    struct list *next; 
};

void insert(struct list *, int);
void print(struct list *);

int main()
{
    struct list *mylist;

    insert(mylist, 10);
    insert(mylist, 20);
    insert(mylist, 30);
    insert(mylist, 40);
    insert(mylist, 50);
    insert(mylist, 60);

    print(mylist);
    return 0;
}

void print(struct list *head)
{
    if(head==NULL)
        return;
    else
    {
            while(head->next!=NULL)
            {
             printf("%d\t",head->node);
             head=head->next;       
        }
    }
}


void insert(struct list *head, int value)
{   
    struct list *new_node;
    new_node = (struct list *)malloc(sizeof(struct list));

//node Creation
    new_node->node=value;
    new_node->next=NULL;

//Adding Node to list
    if(head==NULL)
    {
        head=new_node;  

    }
    else
    {
        while(head->next!=NULL);
        {   
            head=head->next;

        }
        head->next=new_node;

    }

}

insert()是在链接列表中插入元素的函数,mylist并且print()是打印链接列表中所有值的函数。请帮忙。我无法理解我犯了什么错误。

4

5 回答 5

5

我建议再做一个改变,即函数的原型应该像

void insert(struct list **, int);
void print(struct list **);

并且身体应该相应地改变。由于您在插入中完成了新的内存分配,因此您不应该按值传递,而应该按地址传递,然后它才会按预期工作。

此外,在打印函数中,循环终止应该是 while(*head != NULL) 而不是 while((*head)->next != NULL) 否则它将跳过最后一个节点。

此外,您应该在第一次调用 insert 函数后将第一个节点存储到 tmp 指针中,并且应该在最后将 tmp 指针传递给 print 函数。在您的代码中,您将指针传递给错误的最后一个节点。所以,应该是这样的。

int main()
{
    struct list *mylist=NULL, *tmp = NULL;

    insert(&mylist, 10);

    tmp = mylist;   /* here */

    insert(&mylist, 20);
    insert(&mylist, 30);
    insert(&mylist, 40);
    insert(&mylist, 50);
    insert(&mylist, 60);

    /* At this point mylist is pointing to last node, so pass tmp which stores the first node */
    print(&tmp);    
    return 0;
}
于 2013-08-01T08:07:45.930 回答
4

问题在于以下行,

 while(head->next!=NULL);

它应该是,

 while(head->next!=NULL)

删除分号。

于 2013-08-01T07:56:20.607 回答
2

There are a few errors:

1) your code writers struct list *head;. Do you know that in C and C++ such a variable is not initialized? You cannot count on it being NULL unless it's at global scope; local variables instead must always be initialized before being used.

2) your insert function receives head by value so when it changes it (in the case the list is empty) it's only modifying its local copy and not the head variable of main. You must either pass head as a struct list ** or you must return the new head value to main. In C++ an alternative is to pass it as a list *& (reference to a pointer).

3) Your while loop has an extra semicolon at before the body so it's an empty loop and the body part will be executed always (no matter what is the condition) and exactly once because it's just a nested {...} block.

于 2013-08-01T08:04:34.307 回答
1

虽然它不能立即解决您的问题,但还要注意动态分配的内存不会在您的应用程序中释放:您的程序泄漏。

强烈建议添加另一个功能destroy

void destroy(list * head)
{
    while(head != NULL)
    {
        struct list * tmp = head->next;
        free(head);
        head = tmp;
    }
}

这是一个完整的工作示例,不会泄漏:http: //ideone.com/y2Fl7i

于 2013-08-01T08:21:43.020 回答
1

Here is a good infinite loop. If you use a debugger, you could have found it yourself ^^.

while(head->next!=NULL);

After you also need to initialize your list in your main function.

struct list *mylist = NULL;

youalso need to changeyour insert argument to be a double pointer (the main head list value change should will not occur if you pass the its pointer only since it just copy the address value)

void print(struct list *head)
{
  while(head!=NULL)
  {
    printf("%d\t",head->node);
    head=head->next;       
  }
}

void insert(struct list **head, int value)
{   
  struct list *new_node;
  new_node = (struct list *)malloc(sizeof(struct list));

  //node Creation
  new_node->node=value;
  new_node->next=NULL;

  //Get the end of the list
  while((*head)->next!=NULL)
  {   
    (*head)=(*head)->next;
  }

  // Add the node at the end of the list
  (*head)->next=new_node;
}

int main()
{
  struct list *mylist = NULL;

  insert(&mylist, 10);
  insert(&mylist, 20);
  insert(&mylist, 30);
  insert(&mylist, 40);
  insert(&mylist, 50);
  insert(&mylist, 60);

  print(mylist);
  return 0;
}
于 2013-08-01T08:03:46.343 回答