Skip to content
Snippets Groups Projects
Commit f3e52831 authored by Mattias Erlingsson's avatar Mattias Erlingsson
Browse files

changed checking for null, and new test in main

parent 9fc9a384
No related branches found
No related tags found
No related merge requests found
......@@ -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
}
}
......@@ -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);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment