c - Search for value and enter before in doubly linked list -


i'm getting segmentation fault after choose option , type char want add in front of. function fyi.

struct:

struct node   {  struct node *previous;  char data;  struct node *next; }*head, *last;  

function:

int before(int value, int loc) {  struct node *temp,*var,*temp1;  var=(struct node *)malloc(sizeof(struct node));  var->data=value;      if(head==null)  {        head=var;        head->previous=null;        head->next=null;  }  else  {        temp=head;        while(temp!=null && temp->data!=loc)        {              temp=temp->next;        }        if(temp==null)        {             printf("\n%c not present in list ",loc);        }        else        {        temp1=temp->next;        temp->next=var;        var->previous=temp;        var->next=temp1;        temp1->previous=var;        }  }  last=head;  while(last->next!=null)  {        last=last->next;  } }    

i thought null work isn't, need clarification try on own.

would still help...

in part of code:

   while(temp!=null && temp->data!=loc)    {      temp=temp->next;    }     if(temp==null)    {      printf("\n%c not present in list ",loc);    }    else    {      temp1=temp->next;      temp->next=var;      var->previous=temp;       var->next=temp1;      temp1->previous=var;    } 

it possible temp not null, temp->next (namely, if temp last item in list). segmentation fault in line temp1->previous = var;...

edit since still struggling work, wrote complete example. uses different structure - have 1 function find out insert, , inserting. trust can figure out in way code not go through same steps code does, , you'll able figure out here.

i inserted couple of printf statements confirm things behaving intended - idea during debugging.

i hope helps!

#include <stdio.h> #include <stdlib.h>  struct node {     struct node *previous;     char data;     struct node *next; }*head, *last;   struct node * insertbetween(struct node * p1, struct node * p2, char value) {     struct node* newitem = (struct node *)malloc(sizeof(struct node));     printf("inserting between %p , %p\n", p1, p2);     newitem->data = value;     newitem->next = p2;     newitem->previous = p1;     if (p1 == null)      {         printf("have new head!\n");         head = newitem;         head->next = p2;         if (p2 != null) p2->previous = head;         else last = newitem;     }     else     {         p1->next = newitem;         p2->previous = newitem;     }     printf("insertbetween completed\n");     return newitem; }  int before(char value, char loc) {     struct node *temp,*var,*temp1, *penultimate=null;     if(head==null)     {         printf("creating head\n");         head = insertbetween(null, null, value);     }     else     {         temp=head;         while(temp!=null && temp->data!=loc)         {             printf("data %c\n", temp->data);             temp=temp->next;         }         if(temp==null)         {             printf("\n%c not present in list \n",loc);         }         else         {         // create new element         insertbetween(temp->previous, temp, value);         }     }      // confirming "last" still last element - should not need this:     // , list integrity intact     temp=head;     while(temp->next!=null)     {         printf("element %p has value %c , points element %p\n", temp, temp->data, temp->next);         temp=temp->next;     }     printf("in end, temp %p , last %p\n", temp, last); }     int main(void) { before('z','a'); before('y','z'); before('x','y'); before('x','y'); before('2', 'z'); printf("inserted everything!\n"); return 0; } 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -