Thread: Adding element before current node

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    118

    Adding element before current node

    my add before current method is not working in the else part .It works when the iterator in the list is at the end but not when its not .Can anyone spot the problem with a little snippet of the code

    Code:
       list then add item to end of list */
    void list_add_before_current(struct listset * p,char item){
        
    
    
      struct node * temp = (struct node*)malloc(sizeof (*temp));
     
        if(list_at_end(p)){
        
            temp->next=NULL;
            p->tail->next=temp;
            temp->key=item;
            temp->previous=p->tail;
            p->tail=temp;
             
    
    
      
        }
        
        else
        {
          
          temp->key=item;
          p->iterator->previous->next=temp;  //pointing my former previous node next  to my new node
          temp->next=p->iterator;
          p->iterator->previous=temp; //iterator previous now points to my new nod
          temp->previous = p->iterator->previous; // new node previous  points tom my currents iterator previous
          temp->next = p->iterator; //new node next points to my iterator
    
    
        }
    
    
    }
    
    int main(int argc, char** argv) {
        struct listset  *src1;
         src1=listset_new();
         
         
    listset_add(src1, 3);
     listset_add(src1, 15);
    listset_add(src1, 21);
     listset_add(src1, 11); 
    list_add_before_current(src1,5);

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    i keep losing my head but i dont know why

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by sigur47 View Post
    i keep losing my head but i dont know why
    Y'know what... I know people who would *pay* for straight lines like that one

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Executing these two lines in this order is probably a mistake:
    Code:
          p->iterator->previous=temp; //iterator previous now points to my new nod
          temp->previous = p->iterator->previous; // new node previous  points tom my currents iterator previous
    temp->previous now points at temp!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by iMalc View Post
    Executing these two lines in this order is probably a mistake:temp->previous now points at temp!
    hi thanks for the tip, i checked it and changed this to this but i am still having the same problem
    Code:
    void list_add_before_current(struct listset * p,char item){
        
        struct node *e;
      struct node * temp = (struct node*)malloc(sizeof (*temp));
     
        if(list_at_end(p)){
        
            temp->next=NULL; 
            p->tail->next=temp;
            temp->key=item;
            temp->previous=p->tail;
            p->tail=temp;
             
    
    
      
        }
       // else if(p->iterator->previous==NULL){
       //     temp->key=item;
       //     p->iterator->previous=temp;
        //    temp->next=p->iterator;
        //    p->head=temp;
       //     temp->previous=NULL;
       // }
        else
        {
          
        //  temp->key=item;
          //p->iterator->previous->next=temp;  //pointing my former previous node next  to my new node
         // temp->next=p->iterator;
         // p->iterator->previous=temp; //iterator previous now points to my new nod
          //temp->previous = p->iterator->previous; // new node previous  points tom my currents iterator previous
          
           // e=p->iterator->previous;
            temp->previous=p->iterator->previous;//de previous  points tom my currents iterator previous
            temp->key=item;
            p->iterator->previous->next=temp;  //pointing my former previous node next  to my new node
            temp->next=p->iterator; //new node next points to my iterator
            p->iterator->previous=temp; //iterator previous now points to my new node
            
            
        }
    
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-22-2011, 11:50 PM
  2. Replies: 19
    Last Post: 05-11-2011, 09:41 AM
  3. Adding a node to linked list
    By laurenlb in forum C Programming
    Replies: 1
    Last Post: 04-14-2011, 07:05 PM
  4. Adding a node of struct
    By zyphirr in forum C Programming
    Replies: 1
    Last Post: 12-03-2008, 06:08 PM
  5. Replies: 22
    Last Post: 10-22-2005, 08:42 AM