Note that there are some explanatory texts on larger screens.

plurals
  1. POReversing the elements of a double linked list in C
    primarykey
    data
    text
    <p>This is my approach-Keep a pointer at the start of the list and a pointer at the end of the list.Advance the head pointer forward and the tail pointer backward until they point the same. Interchange the values before forwarding.The function throws a segmentation fault when it is called.Why is it so? This is my structure for the nodes of the list</p> <pre><code>struct dll { int number; struct dll *next; struct dll *prev; }; </code></pre> <p>This is my function for reversing the list and the main</p> <pre><code> int count=0; void reverse(struct dll **root) { int temp; struct dll *tail=NULL; struct dll *temproot=NULL; temproot =(*root); for(;(temproot)-&gt;next !=NULL;(temproot)=(temproot)-&gt;next); //traversing to the end tail= temproot; while(((*root)-&gt;next != tail-&gt;prev) || ((*root)-&gt;next != tail))//one for even no. of nodes and one for odd no.of nodes { temp=(*root)-&gt;number; //swapping the numbers (*root)-&gt;number=tail-&gt;number; tail-&gt;number=temp; (*root)=(*root)-&gt;next; tail=tail-&gt;prev; } //even after they are same, values need to be changed one last time temp=(*root)-&gt;number; (*root)-&gt;number=tail-&gt;number; tail-&gt;number=temp; (*root)=(*root)-&gt;next; tail=tail-&gt;prev; } void insert(struct dll **root,int num) { struct dll *temp; if(count==0) { if((*root)==NULL) { (*root)=(struct dll *)malloc(sizeof(struct dll)); (*root)-&gt;next=NULL; (*root)-&gt;prev=NULL; (*root)-&gt;number=num; count++; printf("\n%d",count); } } else if((*root)-&gt;next==NULL) { temp=(struct dll *)malloc(sizeof(struct dll)); temp-&gt;next=NULL; temp-&gt;prev=(*root); temp-&gt;number=num; (*root)-&gt;next=temp; count++; printf("\n%d",count); } else { insert(&amp;(*root)-&gt;next,num); } } main() { struct dll *head=NULL; int i,n,num; while(1) { printf("Enter 1 for insert, 3 for reverse, 0 for exit\n"); scanf("%d",&amp;n); switch(n) { case 1:printf("Enter number\n"); scanf("%d",&amp;num); insert(&amp;head,num); break; case 3:reverse(&amp;head);break; case 0:exit(0); default:printf("Enter correct value\n"); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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