0
#include<stdio.h>
#include<stdlib.h>

//double linked list
struct node {
    int data;
    struct node *rnext;
    struct node *lnext;
}*first=NULL,*last=NULL;
//double linked list

void insertion() {
    struct node *nn=malloc(sizeof(*nn));
    printf("enter data to be inserted\n");
    scanf("%d",&nn->data);
    nn->rnext=NULL;
    nn->lnext=last;
    if(first == NULL) {
        first = nn;
        last = nn;
    }
    else{
        last->rnext=nn;
    } 
    last=nn;
}

void display() {
    struct node *temp;
    if(first==NULL) {
        printf("list is empty\n");
        return;
    }
    temp=first;
    while(temp!=NULL) {
        printf("%d \n",temp->data);
        temp=temp->rnext;
    }
}

void deletion() {
    struct  node  *temp;
    if(first==NULL) {
        printf("list is empty\n");
        return;
    }
    temp=first;
    first=first->rnext;
    first->lnext=NULL;
    free(temp);
}

int main() {
    int  option;
    do {
        printf("enter option 1.insert\n  2.display\n  3.delete\n  4.exit\n");
        scanf("%d",&option);
        switch(option) {
        case 1:
            insertion();
            break;
        case 2:
            display();
            break;
        case 3:
            deletion();
            break;
        }
    } while(option!=4);
}

This is a program written for deleting and inserting a node in double linked list. The program compiles without error, but it fails at run-time with a segmentation fault error while deleting a node when there is only one node in the list. Can anyone please help with the solution for this segmentation fault?

Here is some sample output from the program:

./out
enter option 1.insertion
  2.display
  3.deletion
  4.exit
1
enter data to be inserted
11
enter option 1.insertion
  2.display
  3.deletion
  4.exit
2
11 
enter option 1.insertion
  2.display
  3.deletion
  4.exit
3
Segmentation fault
3
  • Please format your output so it's more readable. Commented Jun 9, 2013 at 14:27
  • possible duplicate of Why am I getting a "Segmentation Fault" error when I try to run the tests? Commented Jun 9, 2013 at 14:28
  • @user2458408 You should really aim for consistent indentation and general code style in your programs. Consistent indentation makes it easier for anyone who would answer your questions to understand your code, and you will also find trivial errors a lot faster yourself. Commented Jun 9, 2013 at 14:54

3 Answers 3

0

The absolute easiest way to solve this one is run it in a debugger. You probably won't even need to learn how to step through your code or anything - just fire up, run, and read the line.

If you are on a *nix as your tag indicated:

  1. Compile your code with -g flag.
  2. Load as, e.g. gdb a.out.
  3. Run now that it's loaded - (gdb) run.
  4. Do whatever you need to reproduce the segfault.
  5. bt or where should give you a stack trace - and an exact line that is causing your problem.

I'm sure enough you can solve it from there to post this as an answer; but if not, knowing the exact line will make it very much easier to research and solve.

Sign up to request clarification or add additional context in comments.

Comments

0

At least two bugs:

On one hand, in the insertion function, the memory allocation is incorrect:

struct node *nn=malloc(sizeof(*nn));

It should be :

struct node *nn= (struct node *) malloc(sizeof(struct node));

On the other hand, in the deletion function. If there is only one node. After the statement

first=first->rnext;

the pointer first become NULL. Then you try to use it as:

first->lnext=NULL;  // first is NULL

Then segment fails.

Comments

0
temp=first;
first=first->rnext;//when only one, first is NULL(first->rnext)
first->lnext=NULL;//(NULL)->lnext , Segmentation fault!!
free(temp);

maybe fix to

temp=first;
if(first == last){//if only one
    first = last = NULL;
} else {
    first=first->rnext;
    first->lnext=NULL;
}
free(temp);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.