Hi i have a bug in my program that i have been trying to find but i cant find it.I am trying to sort my data as i pass it in into a double linked list.My test data is 7,5,1. it works when there are only two numbers but when i increased it to three nodes this is what happens
1,7. my five gets overwritten.
Code:struct node{ char key; struct node *next; struct node *previous; }; struct listset{ int length; struct node *head; struct node * tail; struct node *iterator; }; struct listset * listset_new(){ struct listset *p =(struct listset*) malloc(sizeof(*p)); p->head=NULL; p->tail=NULL; p->iterator=NULL; p->iterator=NULL; return p; } /* add new item before current item. if the iterator ia at end of the 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)){ //at end of list // if(p->iterator->previous!=NULL&& p->iterator->next==NULL){ temp->next=NULL; p->tail->next=temp; temp->key=item; temp->previous=p->tail; p->tail=temp; // } } //if only one element in the list else if(p->iterator->previous==NULL&&p->iterator!=NULL){ p->head=temp; temp->key=item; temp->next=p->iterator; } //anywhere in the list else { temp->key=item; p->iterator->previous->next=temp; //pointing my former previous node next to my new node p->iterator->previous=temp; //iterator previous now points to my new node 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 } } void list_move_to_start(struct listset *p){ while(p->iterator->previous!=NULL){ p->iterator=p->iterator->previous; } } void listset_add(struct listset * t, char item){ struct listset *a=t; int found=0; struct node *temp= (struct node*)malloc(sizeof(*temp)); if(t->head==NULL) { a->head=temp; a->tail=temp; temp->key=item; temp->previous=NULL; temp->next=NULL; a->iterator=temp; //when first element is added .iterator becomes my temp } else { while(a->iterator!=NULL && found!=1){ if(item<a->iterator->key){ list_add_before_current(a, item); found=1; list_move_to_start(a);} else a->iterator=a->iterator->next; }//iner while if(a->iterator==NULL){ list_add_before_current(a, item); list_move_to_start(a); } }//else } //checking if a list is empty int isEmpty(struct listset *p){ if(p->head==NULL) return 1; else return 0; } //removing an item from the end of the list void list_popBack(struct listset *p){ if(p->tail->previous==NULL) { free(p->tail); p->head=NULL; } else { p->tail->previous->next=NULL; free(p->tail); } } void list_write_out(struct listset *p){ struct node *current=p->head; while(current!=NULL){ printf("%d ",current->key); current=current->next; } } /* move the iterator to the next item in the list */ void list_move_to_next(struct listset * p){ p->iterator=p->iterator->next; } int list_get_current(struct listset * p){ int c=0; c= p->iterator->key; return c; } /* set the value of the current value of the list */ void list_set_current(struct listset * p, char item){ p->iterator->key=item; } /* delete the current item in the list */ void list_delete_current(struct listset * p){ if(p->iterator->previous==NULL) { free(p->iterator); p->head=NULL; } else { p->iterator->previous->next=p->iterator->next; free(p->iterator); } } /* check has the iterator moved past last item in list; always true if list is empty */ int list_at_end(struct listset * p){ if( (p->iterator==NULL && !isEmpty(p) )){ return 1; //printf("at end of list"); } else { // printf(" notat end of list"); return 0;} } int main(int argc, char** argv) { struct listset *src1; struct listset *src2; struct listset *destination; src1=listset_new(); src2=listset_new(); destination=listset_new(); char buffer[100]; char x; int choice; listset_add(src1, 7); listset_add(src1, 5); listset_add(src1, 1);



LinkBack URL
About LinkBacks



