【发布时间】:2015-04-15 05:27:44
【问题描述】:
我有下面的 C 程序来实现升序链表。问题出在 buildList() 函数中,因此您可以忽略除 main() 和 buildList() 之外的其他函数。
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void buildList(struct node **, int);
void printList(struct node **);
int main()
{
struct node *head;
head = NULL; /*Empty list*/
int hold, i, j;
printf("How many integers in this list: ");
scanf("%d",&i);
j = 1;
for(i; i > 0; i--)
{
printf("Integer %d: ",j);
scanf("%d",&hold);
buildList(&head, hold);
j++;
}
printList(&head);
return 0;
}
void buildList(struct node **headRef, int data)
{
struct node *newNode;
newNode = malloc(sizeof(struct node));
struct node *current, *current1;
current = *headRef;
//If list is empty, add the number as the first node.
if (current == NULL)
{
newNode->data = data;
newNode->next = *headRef;
*headRef = newNode;
}
//If the list is not empty.
else
{
//If the number is not greater than first number.
if (data <= current->data)
{
newNode->data = data;
newNode->next = current;
*headRef = newNode;
}
//If the number is greater than the first number in list.
else
{
int flag = 0;
while (data > (current->data))
{
current1 = current;
current = current->next;
}
newNode->data = data;
current1->next = newNode;
newNode->next = current;
}
}
}
//Prints nodes and total number of nodes.
void printList(struct node **headRef)
{
int count = 0;
struct node *current;
current = *headRef;
while (current != NULL)
{
count++;
printf("%d ",current->data);
current = current->next;
}
printf("\nTotal nodes: %d\n",count);
}
这个程序可以正常工作,直到我给出一个大于列表中任何数字的数字。在这种情况下,我会遇到分段错误。
案例一(精细正确的输出)
-bash-4.1$ ./a.out
How many integers in this list: 3
Integer 1: 5
Integer 2: 1
Integer 3: 4
1 4 5
Total nodes: 3
-bash-4.1$
案例二(此处代码中断(分段错误))
-bash-4.1$ ./a.out
How many integers in this list: 3
Integer 1: 5
Integer 2: 6
Segmentation fault
-bash-4.1$
经过很长时间试图弄清楚我的代码出了什么问题,我发现当一个数字大于列表中的任何数字时,在这种情况下,它应该被插入到列表的末尾,此语句(在函数 buildList() 的最后一个 else 中)导致了问题:
current = current->next;
我终于想通了,当current->next为NULL时,这条语句会导致seg fault。
我想出了一个像下面这样的解决方法,它可以提供正确的输出。
else
{
int flag = 0;
while (data > (current->data))
{
current1 = current;
if(current->next != NULL)
{
current = current->next;
}
else
{
flag = 1;
break;
}
}
newNode->data = data;
current1->next = newNode;
if (flag == 1)
{
newNode->next = NULL;
}
else
{
newNode->next = current;
}
}
现在我得到了正确的输出。
-bash-4.1$ ./a.out
How many integers in this list: 3
Integer 1: 5
Integer 2: 1
Integer 3: 10
1 5 10
Total nodes: 3
-bash-4.1$
现在我想知道为什么不是 current = current->next;当 current->next 为 NULL 时工作。我期待这条语句将 NULL 分配给 current。
谁能告诉我这是什么原因? 我的解决方法也是一个好方法吗?还是有更好的方法?
对不起,这个问题太长了,但是花了大约 2 个小时来调试这个,我想我快疯了。
谢谢。
【问题讨论】:
-
@lurker 这不是在链表开头添加节点的方式吗?通过覆盖这个列表的头部?我通过网上找到的教程学习了链表,它说这就是在链表开头添加节点的方法。如果这不是标准方式,请告诉我。
-
对不起,这是正确的。我没有仔细查看
next设置。
标签: c debugging null linked-list segmentation-fault