0

我在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);  
}    

一切顺利,但是当我尝试删除一个节点时,控制台只是崩溃了,当我调试它时它显示“访问冲突错误:分段错误”我的程序有什么问题。

4

4 回答 4

1

指针p在 del(..) 中的 else 块之前没有被初始化。

p是空指针时,您不能访问p->next 。

于 2013-10-03T16:26:36.687 回答
1

你的错误在这里

while(x != (p->next)->data && p->next != NULL).

似乎您正在尝试在检查其是否为 null 之前读取可能为 null 元素的下一个或数据,如果它为 null 将导致 seg-fault。

现在我开始使用调试器的惊人之处。让我向您展示使用 gdb 找到该错误是多么容易——我不会确切解释我在做什么,因为那里确实有数百个 GDB 教程,但希望这花了我不到 15 秒的时间来找到您的错误将鼓励您去谷歌并自己完成这些教程!:

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)
于 2013-10-03T16:31:02.410 回答
0

您尚未在删除函数中初始化指针“p”。

只需将此行添加到您的代码中:-

p = (node*)malloc(sizeof(node));

或者由于您需要实现删除功能,我想您需要将“p”指向头指针。做这个-

p = head;

希望这可以帮助。

于 2013-10-04T04:56:40.063 回答
0

指针 p 在 del 函数中被访问之前没有被初始化。

于 2013-10-03T17:04:31.480 回答