Commit 5d74f043 authored by a-j.towse's avatar a-j.towse

Working NewNode function

parent 4f92faac
No preview for this file type
...@@ -30,8 +30,8 @@ int main(void) { ...@@ -30,8 +30,8 @@ int main(void) {
AddNode(&head,5); AddNode(&head,5);
printf("List Length: %d\n",ListLength(head)); PrintLinkedList(head);
return 0; return 0;
} }
...@@ -112,31 +112,51 @@ int ListLength(node *start) { ...@@ -112,31 +112,51 @@ 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 *current = *ptr_to_start;
node *new_node = NewNode(value); node *new_node = NewNode(value);
node *before;
node *after;
if (*ptr_to_start == NULL) { // special case if the list is empty if (*ptr_to_start == NULL) // special case if the list is empty
{
*ptr_to_start = new_node; *ptr_to_start = new_node;
return; return;
} }
while (temp->next != NULL && temp->next->val < value) { while (current != NULL )
{
if (ptr_to_start == temp) {
AddNodeToStartOfList(value);
return;
}
else { if (current->next->val > value)
temp = temp->next; {
if (ptr_to_start == current) //if it is smaller than the first value, add to start
{
AddNodeToStartOfList(ptr_to_start,value);
return;
}
else if (current->next == NULL) //if it hits the end of the list/ add to end
{
AddNodeToEndOfList(ptr_to_start,value);
return;
}
else
{
node *temp = current->next;
current-> next = new_node;
new_node->next=temp;
return;
}
} }
} else
NewNode(value); {
temp->next = current = current->next;
}
}
}
......
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