-
Notifications
You must be signed in to change notification settings - Fork 1
/
list.c
55 lines (46 loc) · 817 Bytes
/
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Node{
int val;
struct Node* next;
} Node;
Node* new_node(int val){
Node* n = malloc(sizeof(Node));
if(n == NULL){
fprintf(stderr, "Could not allocate enough memory for a new node!\n");
}
n->val = val;
n->next = NULL;
return n;
}
void print(Node* root){
if(root == NULL){
printf("NULL\n");
exit(0);
}
printf("%d ----> ", root->val);
print(root->next);
}
int main(){
Node* root = new_node(1);
Node* n1 = new_node(2);
Node* n2 = new_node(3);
Node* n3 = new_node(4);
Node* n4 = new_node(5);
Node* n5 = new_node(6);
// Add next pointers
root->next = n1;
n1->next = n2;
n2->next = n3;
n3->next = n4;
n4->next = n5;
print(root);
free(root);
free(n1);
free(n2);
free(n3);
free(n4);
free(n5);
return 0;
}