/
testLinkedLists.c
104 lines (72 loc) · 1.59 KB
/
testLinkedLists.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# include <stdio.h>
# include <stdlib.h>
struct node{
int value;
struct node * next;
}* root;
void addNode(struct node ** root, int value){
struct node * temp = (struct node *)malloc(sizeof(struct node *));
temp->value = value;
temp->next = NULL;
if(*root == NULL){
*root = temp;
}
else{
struct node * runner = *root;
while(runner->next!=NULL)
runner = runner->next;
runner->next = temp;
}
}
void printNode(struct node* root){
struct node * runner = root;
while(runner!=NULL){
printf("%d\n", runner->value);
runner = runner->next;
}
}
int main(){
root = NULL;
addNode(&root, 10);
addNode(&root, 20);
addNode(&root, 30);
addNode(&root, 40);
printNode(root);
//addNode(&root, 50);
//printNode(root);
}
//updating the root every time a function call returns
// struct node{
// int value;
// struct node * next;
// }* root;
// struct node * addnode(struct node* root, int value){
// struct node* temp = (struct node*)malloc(sizeof(struct node *));
// temp -> value = value;
// temp -> next = NULL;
// if(root == NULL){
// root = temp;
// return root;
// }
// struct node * runner = root;
// while(runner->next!=NULL)
// runner=runner->next;
// runner->next = temp;
// return root;
// }
// void print(struct node* root){
// struct node* runner;
// runner = root;
// while(runner!=NULL){
// printf(" %d\n ", runner->value);
// runner = runner->next;
// }
// }
// int main(){
// root = NULL;
// root = addnode(root, 10);
// root = addnode(root, 20);
// root = addnode(root, 30);
// root = addnode(root, 40);
// print(root);
// }