-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist2.c
56 lines (49 loc) · 979 Bytes
/
list2.c
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
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int number;
struct node *next;
//struct node *left
//struct node *right
}
node;
//creates an alias 'node' of 'struct node'
int main(void)
{
node *list = NULL;
node *n = malloc(sizeof(node));
if (n == NULL)
{
free(list);
return 1;
}
n->number = 2;
n->next = NULL;
list->next = n;
//Add a number to list
n = malloc(sizeof(node));
if (n == NULL)
{
free(list->next);
free(list);
return 1;
}
n->number = 3;
n->next = NULL;
list->next->next = n;
// print number
for (node *tmp = list; tmp != NULL; tmp = tmp->next)
{
printf("%i\n",tmp->number);
}
// free list
while (list != NULL)
{
//point at same thing that list is pointing to (aka node 1 at first)
node *tmp = list->next;
free(list);
list = tmp;
}
return 0;
}