-
Notifications
You must be signed in to change notification settings - Fork 0
/
1-linked_list.c
70 lines (61 loc) · 1.02 KB
/
1-linked_list.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>
#include <stdlib.h>
/**
* main: Implenting a linked list.
*
* Return: no return.
*/
typedef struct node
{
int number;
struct node*next;
}
node;
int main(void)
{
//List of size NULL
node*list=NULL;
//Add number to list
node*n=malloc(sizeof(node));
if(n==NULL)
{
return 1;
}
n->number=1;
n->next=NULL;
//update list to point new node
list=n;
//Add a number to list
n=malloc(sizeof(node));
if(n==NULL)
{
free(list);
return 1;
}
n->number=2;
n->next=NULL;
list->next=n;
//Add 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 numbers
for(node*tmp=list;tmp!=NULL;tmp=tmp->next)
{
printf("%i\n",tmp->number);
}
//free list
while(list!=NULL)
{
node*tmp=list->next;
free(list);
list=tmp;
}
}