Note that there are some explanatory texts on larger screens.

plurals
  1. POSegmentation fault when deleting and adding a node in double linked list
    text
    copied!<pre><code>#include&lt;stdio.h&gt; #include&lt;stdlib.h&gt; //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",&amp;nn-&gt;data); nn-&gt;rnext=NULL; nn-&gt;lnext=last; if(first == NULL) { first = nn; last = nn; } else{ last-&gt;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-&gt;data); temp=temp-&gt;rnext; } } void deletion() { struct node *temp; if(first==NULL) { printf("list is empty\n"); return; } temp=first; first=first-&gt;rnext; first-&gt;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",&amp;option); switch(option) { case 1: insertion(); break; case 2: display(); break; case 3: deletion(); break; } } while(option!=4); } </code></pre> <p>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?</p> <p>Here is some sample output from the program:</p> <pre><code>./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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload