Commit 4f92faac authored by a-j.towse's avatar a-j.towse

WIP insertion AddNode

parent 0b8a11f3
...@@ -111,34 +111,50 @@ int ListLength(node *start) { ...@@ -111,34 +111,50 @@ int ListLength(node *start) {
} }
void AddNode(node **ptr_to_start,int value) { void AddNode(node **ptr_to_start,int value) {
node *temp = *ptr_to_start; node *temp = *ptr_to_start;
node *new_node = NewNode(value);
node *before; node *before;
node *after; node *after;
if (temp == NULL) { if (*ptr_to_start == NULL) { // special case if the list is empty
AddNodeToStartOfList(ptr_to_start,value); *ptr_to_start = new_node;
return;
} }
else {
while (temp !=NULL) {
if (temp->val > value && temp->val < temp->next->val){ while (temp->next != NULL && temp->next->val < value) {
before = temp;
after = temp->next; if (ptr_to_start == temp) {
AddNodeToStartOfList(value);
temp = NewNode(value); return;
before->next = temp; }
temp->next = after;
} else {
else if (temp->next == NULL){ temp = temp->next;
AddNodeToEndOfList(ptr_to_start,value);
}
else {
temp = temp->next;
}
} }
}
} }
NewNode(value);
temp->next =
/* Exercise Tasks /* Exercise Tasks
1. Write a function int ListLength(*start) which traverses the list, counts the number of nodes and returns that count. 1. Write a function int ListLength(*start) which traverses the list, counts the number of nodes and returns that count.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment