Commit 4aa04155 authored by elijah vasquez's avatar elijah vasquez 🦍

SECOND

parent 810c3230
...@@ -49,6 +49,7 @@ void AddNodeToStartOfList(node **ptr_to_start, int value) ...@@ -49,6 +49,7 @@ void AddNodeToStartOfList(node **ptr_to_start, int value)
*ptr_to_start is the contents of the variable head, which is the address of the start of the list. *ptr_to_start is the contents of the variable head, which is the address of the start of the list.
If we change *ptr_to_start (note only one * here) we are changing the contents of head. If we change *ptr_to_start (note only one * here) we are changing the contents of head.
We are changing which node is the first node in the list. We are changing which node is the first node in the list.
*/ */
node *temp = NewNode(value); // Create a new node node *temp = NewNode(value); // Create a new node
...@@ -90,9 +91,40 @@ void AddNodeToEndOfList(node **ptr_to_start, int value) ...@@ -90,9 +91,40 @@ void AddNodeToEndOfList(node **ptr_to_start, int value)
} }
temp->next = new_node; temp->next = new_node;
return; return;
}
int ListLength(node *start) // 1
{
node *temp = NULL;
while (temp->next != NULL)
{
printf("%d\n", temp->val);
temp = temp->next;
}
} }
void AddNode(node **ptr_to_start, int value) // 2
{
}
/*
int main()
{
AddNodeToStartOfList(&head,5);
AddNodeToEndOfList(&head,7);
AddNodeToEndOfList(&head,2);
AddNodeToEndOfList(&head,1);
AddNodeToEndOfList(&head,4);
AddNodeToEndOfList(&head,3);
return 0;
}
*/
/* Exercise Tasks /* Exercise Tasks
1. Write a function int ListLength(node *start) which traverses the list, counts the number of nodes and returns that count. 1. Write a function int ListLength(node *start) which traverses the list, counts the number of nodes and returns that count.
2. Write a function void AddNode(node **ptr_to_start, int value) 2. Write a function void AddNode(node **ptr_to_start, int value)
...@@ -100,7 +132,7 @@ void AddNodeToEndOfList(node **ptr_to_start, int value) ...@@ -100,7 +132,7 @@ void AddNodeToEndOfList(node **ptr_to_start, int value)
in ascending order of value. e.g. 3,7,9,122 etc in ascending order of value. e.g. 3,7,9,122 etc
If the list is empty it should add a new node at the start of the list If the list is empty it should add a new node at the start of the list
If the list is not empty it should If the list is not empty it should
- traverse the list until it finds a value greater than value and insert a new node before the node with the greater value - traverse the list until it finds a value greater than value and insert a new node before the node with the greater value
- if it reaches the end of the list without finding a greater value, - if it reaches the end of the list without finding a greater value,
it should add a new node to the end of the list it should add a new node to the end of the list
3. Test the function from 1 with these values added in this order 5,6,2,7,4 and make sure you 3. Test the function from 1 with these values added in this order 5,6,2,7,4 and make sure you
...@@ -124,6 +156,4 @@ AddNodeToEndOfList(&head,1); ...@@ -124,6 +156,4 @@ AddNodeToEndOfList(&head,1);
AddNodeToEndOfList(&head,4); AddNodeToEndOfList(&head,4);
AddNodeToEndOfList(&head,3); AddNodeToEndOfList(&head,3);
*/ */
No preview for this file type
#include <stdio.h>
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