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

Working NewNode function

parent 4f92faac
No preview for this file type
...@@ -30,7 +30,7 @@ int main(void) { ...@@ -30,7 +30,7 @@ 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 (current->next->val > value)
{
if (ptr_to_start == current) //if it is smaller than the first value, add to start
{
AddNodeToStartOfList(ptr_to_start,value);
return;
}
if (ptr_to_start == temp) { else if (current->next == NULL) //if it hits the end of the list/ add to end
AddNodeToStartOfList(value); {
AddNodeToEndOfList(ptr_to_start,value);
return; return;
} }
else { else
temp = temp->next; {
node *temp = current->next;
current-> next = new_node;
new_node->next=temp;
return;
}
} }
else
{
current = current->next;
}
} }
NewNode(value); }
temp->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