-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.c
More file actions
65 lines (58 loc) · 1.19 KB
/
node.c
File metadata and controls
65 lines (58 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# include <stdio.h>
# include <stdlib.h>
# include <cs50.h>
typedef struct node
{
int number;
struct node *next;
}
node;
int main(void)
{
//list of size
node *list = NULL;
//Add a number to list
node *n = malloc(sizeof(node));
if (n == NULL)
{
return 1;
}
n->number = 1;
n->next = NULL;
//Update list to point to new node
list = n;
//Add a number to the list
n = malloc(sizeof(node));
if(n == NULL)
{
free(list);
return 1;
}
n->number = 2;
n->next = NULL;
list->next = n;
//Add another number to list
n = malloc(sizeof(node))
if(n == NULL)
{
free(list->next);
free(list);
return 1;
}
n->number = 3;
n-> = NULL;
list->next->next = n;
//Print numbers
for (node *tmp = list; tmp !=NULL; tmp = tmp->next)
{
printf("%i\n", tmp->number)
}
//Free List (when there are linked lists, the computer does not know how to follow your pointers)
while (list != NULL)
{
node *tmp = list->next;
free(list);
list = tmp;
}
return: 0;
}