【发布时间】:2013-04-23 04:47:33
【问题描述】:
我正在开发一个按排序顺序插入链接列表的程序,但它不断出现段错误,我不知道为什么。我怀疑它与指针有关,但我不能说,因为在我的编程生涯的这一点上,这些对我来说仍然有点混乱。另外,我必须保持插入原型相同。我无法将节点参数更改为双指针。谢谢!
#include <stdio.h>
#include <stdlib.h>
#include<stdio.h>
#include<conio.h>
typedef struct node {
int data;
struct node *next;
};
int main ()
{
struct node* first;
int temp,x,y;
struct node *create (struct node *first);
first = NULL;
void display (struct node *first);
printf ("\n\nCreating a Linked List\n");
printf ("\nEnter Element: ");
scanf ("%d", &x);
y=x;
while(y>0)
{
scanf ("%d", &x);
insert_sorted_linked_list(first,x);
y--;
}
printf ("\nThe list after creation is: ");
display (first);
printf ("\nThe sorted list is: ");
display (first);
return(0);
} /*END OF MAIN*/
insert_sorted_linked_list(struct node* head, int val)
{
struct node* pCur;
struct node* pNew = (struct node*) (malloc(sizeof(struct node)));
pNew -> data = val;
pNew ->next = NULL;
pCur = head;
if( pCur->data == NULL )
{
head->data = pNew->data;
head->next = NULL;
}
else if (pNew->data < pCur->data)
{
pNew ->next = pCur ;
head = pNew;
}
}
void display (struct node *first)
{ struct node *save; /*OR sort *save */
if (first == NULL)
printf ("\nList is empty");
else
{ save = first;
while (save != NULL)
{ printf ("-> %d ", save->data);
save = save->next;
}
getch();
}
return;
}
编辑:将 main 更改为 int。调试器不喜欢这些行:
struct node* pNew = (struct node*) (malloc(sizeof(struct node)));
如果(pCur->数据 == NULL)
但不确定是什么问题。
编辑 2:
我决定在明天早上之前我不会按照他要求的原始方式让它工作,所以我在这里发布了一个修改版本。那个没有段错误,但事实证明也有逻辑错误。
#include <stdio.h>
#include <stdlib.h>
#include<stdio.h>
#include<conio.h>
typedef struct s
{
int data;
struct s *next;
}node;
void insert_sorted_linked_list(node **head, int val);
void display (node **first);
void freeList(node **first);
int main ()
{
node* first;
int x,y;
first = NULL;
printf ("\n\nCreating a Linked List\n");
printf ("\nEnter number of elements: ");
scanf ("%d", &x);
y=x;
while(y>0)
{
scanf ("%d", &x);
insert_sorted_linked_list(&first,x);
y--;
}
printf ("\nThe sorted list is: ");
display (&first);
freeList(&first);
return 0;
}
void insert_sorted_linked_list(node **head, int val)
{
node* pCur;
node* pNew = (node*) (malloc(sizeof(node)));
pNew->data = val;
pNew->next = NULL;
pCur = (*head);
if( pCur == NULL )
{
(*head) = pNew;
}
else if(pNew->data < pCur->data)
{
pNew->next = pCur;
(*head) = pNew;
}
else
{
while(pCur->next!=NULL && pNew->data > pCur->next->data)
pCur = pCur->next;
pNew->next = pCur->next;
pCur->next = pNew;
}
}
void display (node **first)
{
node *lisprint; /*OR sort *lisprint */
if (*first == NULL)
printf ("\nList is empty");
else
{
lisprint = *first;
while (lisprint != NULL)
{
printf ("-> %d ", lisprint->data);
lisprint = lisprint->next;
}
getch();
}
} /*END OF FUNCTION DISPLAY*/
void freeList(node **first)
{
node *i;
i = *first;
while(i !=NULL)
{
(*first) = (*first)->next;
free(i);
i = *first;
}
}
谢谢!
【问题讨论】:
-
我认为格式本身足以解决段错误。
-
学习使用调试器。如果您在 Linux 上,请尝试 GDB。它会告诉你发生段错误的确切行。
-
和
intmain()。 A-voidvoid. -
您的主要错误出现在插入函数中的 19 行代码之间,这是取消引用 NULL 指针的访问冲突。我可以说是哪一个以及为什么,但我已经比你付出了更多的努力。
-
"我必须保持插入原型不变。我不能将节点参数更改为双指针。谢谢!" 我可以吗?说真的,如果你不能改变原型,包括返回类型(这在严格的 C99 中甚至是不合法的,因为它需要声明的返回类型,你的被假定为
int和甚至从未指定),那么除了通过外部变量(读取:全局)之外,没有办法更改或发送头指针。既然您已经知道这一切,并且可能已经拒绝了其他更改原型的尝试(或者您不会提出它),那么究竟有什么可以帮助您?
标签: c linked-list segmentation-fault