我在Dev C ++中正在做一个单链表的C程序 其代码如下:
#include<stdio.h>
typedef struct node
{
int data;
struct node *next;
}node;
node *insert(node*,int,int);
node *create(int);
node *del(node*,int);
int print(node*);
int main()
{
node *head;
int n,ch,n1,x,key,k;
head = NULL;
printf("\n Number of nodes:");
scanf("%d",&n);
while(1)
{
printf("\nYour choices are:");
printf("\n1) Create");
printf("\n2) Print");
printf("\n3) Insert");
printf("\n4) Delete");
printf("\n5) Reverse\n");
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
head = create(n);
break;
case 2:
n1 = print(head);
break;
case 3:
printf("Enter the element to be inserted:");
scanf("%d",&x);
printf("Enter the position where it is to be inserted:");
scanf("%d",&key);
head = insert(head,x,key);
break;
case 4:
printf("Enter the element to be deleted:");
scanf("%d",&k);
head = del(head,k);
break;
case 5:
break;
default:
exit(0);
break;
}
}
return(0);
}
node *create(int n)
{
node *head,*p;
int i;
printf("Enter %d data fields",n);
head = (node*)malloc(sizeof(node));
head->next = NULL;
scanf("%d",&(head->data));
p=head;
for(i=1;i<n;i++)
{
p->next = (node*)malloc(sizeof(node));
p=p->next;
scanf("%d",&(p->data));
p->next=NULL;
}
printf("Linked list created.");
return(head);
}
int print(node *p)
{
while(p!=NULL)
{
printf("%d-->",p->data);
p=p->next;
}
printf("NULL");
return(0);
}
node *insert(node *head,int x,int key)
{
node *p,*q;
p = (node*)malloc(sizeof(node));
p->data = x;
if(key==-1)
{
p->next = head;
head=p;
}
else
{
q = head;
while(key != q ->data && q!=NULL)
q=q->next;
if(q!=NULL)
{
p->next = q->next;
q->next = p;
}
}
printf("Element Inserted.");
return(head);
}
node *del(node *head,int x)
{
node *p,*q;
if(x == head->data)
{
p = head;
head = head->next;
free(p);
}
else
{
while(x != (p->next)->data && p->next != NULL)
p=p->next;
if(p->next != NULL)
{
q = p->next;
p->next = (p->next)->next;
free(q);
}
}
return(head);
}
一切顺利,但是当我尝试删除节点时,控制台崩溃,当我调试它时显示“访问冲突错误:分段错误” 我的计划有什么问题。
答案 0 :(得分:1)
指针 p 尚未在del(..)中的else块之前初始化。
当 p 为空指针时,您无法访问 p-&gt; next 。
答案 1 :(得分:1)
您的错误在这里
while(x != (p->next)->data && p->next != NULL).
在检查是否为null之前,您似乎正在尝试读取下一个或者可能的null元素的数据,如果它为null将导致seg-fault。
现在我可以使用调试器来实现这一点。让我告诉你用gdb找到这个错误是多么容易 - 我不会完全解释我在做什么,因为那里有数百个GDB教程,但希望事实上这让我&lt; 15秒找到你的错误将鼓励你去google并自己完成这些教程!:
eos% gcc -g test.c -o a
test.c: In function ‘main’:
test.c:56: warning: incompatible implicit declaration of built-in function ‘exit’
test.c: In function ‘create’:
test.c:67: warning: incompatible implicit declaration of built-in function ‘malloc’
test.c: In function ‘insert’:
test.c:96: warning: incompatible implicit declaration of built-in function ‘malloc’
test.c: In function ‘del’:
test.c:124: warning: incompatible implicit declaration of built-in function ‘free’
test.c:134: warning: incompatible implicit declaration of built-in function ‘free’
eos% gdb a
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1)
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from nonOfYourBussiness...done.
(gdb) run
Starting program: nonOfYourBussiness/a
Number of nodes:5
Your choices are:
1) Create
2) Print
3) Insert
4) Delete
5) Reverse
Enter your choice:1
Enter 5 data fields1 2 3 4 5
Linked list created.
Your choices are:
1) Create
2) Print
3) Insert
4) Delete
5) Reverse
Enter your choice:4
Enter the element to be deleted:3
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400a2b in del (head=0x602010, x=3) at test.c:128
128 while(x != (p->next)->data && p->next != NULL)
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.107.el6_4.4.x86_64
(gdb) l
123 head = head->next;
124 free(p);
125 }
126 else
127 {
128 while(x != (p->next)->data && p->next != NULL)
129 p=p->next;
130 if(p->next != NULL)
131 {
132 q = p->next;
(gdb)
答案 2 :(得分:0)
指针p在del函数中访问之前未初始化。
答案 3 :(得分:0)
您尚未在删除功能中初始化指针'p'。
只需将此行添加到您的代码中: -
p = (node*)malloc(sizeof(node));
或者因为你需要实现删除功能,我想你需要将'p'指向头指针。 这样做 -
p = head;
希望这有帮助。