-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list.c
43 lines (36 loc) · 868 Bytes
/
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
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
void initNode(struct node* list, int n)
{
list->data = n;
list->next = NULL;
}
struct node* addNode(struct node* list, int n)
{
struct node* new_node = (struct node*) malloc(sizeof(struct node));
new_node->data = n;
new_node->next = list; // point to the previous node
return new_node;
}
void printAllNode(struct node* list)
{
while(list != NULL)
{
printf("Found at address %p, n=%d\n", list, list->data);
list = list->next;
}
}
int main()
{
struct node* list = (struct node*) malloc(sizeof(struct node));
initNode(list, 12);
list = addNode(list, 5);
list = addNode(list, 8);
printAllNode(list);
return 0;
}