0

I am doing a C program of singly linked list in Dev C++ Its code is as follows:

#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);  
}    

Everything goes well, but when I try to delete a node, the console just crashes and when I debugged it it shows "Access Violation error: Segmentation fault" Whats wrong with my program.

2
  • In your while loop, look at x != (p->next)->data. When it first hits that expression, has p been set yet? (HINT: "no").
    – lurker
    Oct 3, 2013 at 16:21
  • Where are you setting p in del() before else is executed. Oct 3, 2013 at 16:22

4 Answers 4

1

The pointer p has not been initialized before the else block in del(..).

You can't access p->next when p is a null pointer.

1
  • You're right, the value of an uninitialized pointer is indeterminate. I shouldn't have made that assumption. Oct 3, 2013 at 18:32
1

Your error is here

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

it seems like you are trying to READ the next or data of a possiby null element before you check if its null, which if it is null will cause a seg-fault.

Now i get to plug the amazingness of using a debugger. Let me show you how easy it was to find that error with gdb -- I wont explain exactly what im doing, since there are literally hundreds of GDB tutorials out there, but hopefully the fact that this took me < 15 sec to find your error will encourage you to go google and go through on of those tutorials yourself!:

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)
0

The pointer p is not initialized before its accessed in the del function.

0

You have not initialized pointer 'p' in your delete function.

Just add this line to your code:-

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

Or since you need to implement the delete function, I guess you need to point 'p' to the head pointer. Do this-

p = head;

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.