My delete method is not workingCode:struct node{ int key; struct node *next; }; struct listset{ int length; struct node *head; }; struct listset * litset_new(){ struct listset *p = malloc(sizeof(*p)); p->head=malloc(sizeof(p->head)); //creating space for my node head in the list p->head->next=NULL; p->head->key=0; return p; } void listset_add(struct listset * t, int item){ struct listset *a=t; struct node *p= malloc(sizeof(*p)); p->key=item; p->next =a->head->next; a->head->next=p; } /*look up fucntion checks to see if a list contains a certain item it returns * a 1 if it does and a zero if it dosent * */ int listset_lookup(struct listset * p , int item){ struct listset *a=p; struct node*current=a->head->next; while(current!=NULL){ if(current->key==item) return 1; current=current->next; //printf("i am here"); } return 0; } void listset_remove(struct listset * p, int item){ struct listset *a=p; struct node * next; struct node *previous; previous=a->head; // first previous is my head struct node*current=a->head->next;//first node in list if(listset_lookup(a,item)){ while(current!=NULL){ if(current->key==item){ //printf("%d\n",item); previous=current->next; printf("%d\n",previous->key); break; } printf("i am in"); previous=previous->next; current=current->next; } } } /* Given a linke list head pointer * this function to counts the number of nodes in a list */ int Length(struct node *head) { struct node* current =head; int count = 0; while (current != NULL) { count++; current = current->next; } return count; } int main(int argc, char** argv) { struct listset *s; int b=0;int c=0; s=litset_new(); listset_add(s,5); listset_add(s,6); listset_add(s,7); listset_add(s,4); b= listset_lookup(s,4); // printf("%d\n",b); printf("%d\n",s->head->next->key); printf("%d\n",s->head->next->next->key); printf("%d\n",s->head->next->next->next->key); printf("%d\n",s->head->next->next->next->next->key); listset_remove(s,4); c= listset_lookup(s,4); printf("%d\n",c); return (EXIT_SUCCESS); }



LinkBack URL
About LinkBacks




