diff --git a/list.c b/list.c index bd9627c9a7b9a211fe9e6da7be790ea65415329e..cf84578978ef831f949cdb592f9fc68094f1bc32 100644 --- a/list.c +++ b/list.c @@ -8,22 +8,15 @@ void append(struct list_item *first, int x){ struct list_item* temp; temp = (struct list_item*)malloc(sizeof(struct list_item)); - /*if list is empty, let root point to new item, and item tu NULL*/ - if(first->next == NULL){ - first->next = temp; - temp->value = x; - temp->next = NULL; - }else{ - struct list_item* temp1 = first; //for iteration - /*iterate list until next pointer i NULL.*/ - while(temp1->next != NULL){ - temp1 = temp1->next; - } - /*temp1->next == NULL, insert and rearrange pointers*/ - temp1->next = temp; - temp->value = x; - temp->next = NULL; + struct list_item* temp1 = first; //for iteration + /*iterate list until next pointer i NULL.*/ + while(temp1->next != NULL){ + temp1 = temp1->next; } + /*temp1->next == NULL, insert and rearrange pointers*/ + temp1->next = temp; + temp->value = x; + temp->next = NULL; } void prepend(struct list_item *first, int x){ @@ -53,34 +46,24 @@ void input_sorted(struct list_item *first,int x){ struct list_item* temp; temp = (struct list_item*)malloc(sizeof(struct list_item)); - /*See if list is empty*/ - if(first->next == NULL){ - first->next = temp; - temp->value = x; - temp->next = NULL; - - }else{ - /*iterate until x < y or until last node*/ - struct list_item* temp1 = first; - while(temp1->next != NULL){ - /*insert before value higher then x*/ - if(temp1->next->value > x){ - temp->next = temp1->next; - temp1->next = temp; - temp->value = x; - return; - }else{ - /*increment pointer*/ - temp1 = temp1->next; - } + /*iterate until x < y or until last node*/ + struct list_item* temp1 = first; + while(temp1->next != NULL){ + /*insert before value higher then x*/ + if(temp1->next->value > x){ + temp->next = temp1->next; + temp1->next = temp; + temp->value = x; + return; + }else{ + /*increment pointer*/ + temp1 = temp1->next; } + } /*nothing bigger so we put it last*/ temp1->next = temp; temp->value = x; temp->next = NULL; - } - - } void clear(struct list_item *first){ @@ -93,12 +76,11 @@ void clear(struct list_item *first){ return; }else { /*iterate until last node*/ - while (temp->next != NULL) { + while (temp != NULL) { temp1 = temp->next; free(temp); temp = temp1; } - free(temp1); //delete last node first->next = NULL; //make root point to null to restore original property } } diff --git a/main.c b/main.c index 3465ce61784b0465ddfe48fa8121cb049a2e2a9c..ef7e9d880c1da99728850d9a871e94ebcf63bb25 100644 --- a/main.c +++ b/main.c @@ -7,19 +7,10 @@ void main(int argc, char ** argv) { root.value = -1; root.next = NULL; - /*append(&root, 5); - append(&root, 1); - append(&root, 4); - append(&root, 16); - prepend(&root, 3); - append(&root, 20); - prepend(&root, 6); + input_sorted(&root,1); input_sorted(&root, 15); - prepend(&root, 10); - append(&root, 20);*/ - prepend(&root, 24); - input_sorted(&root, 30); - //clear(&root); print(&root); + clear(&root); + }