forked from Hunterhal/C-Programming-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list_recursive.c
53 lines (43 loc) · 996 Bytes
/
linked_list_recursive.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node{
int key;
struct node *next;
}node_t;
void add_end(node_t **node_ref, int key)
{
if(*node_ref == NULL)
{
*node_ref = (node_t *) malloc(sizeof(node_t));
(*node_ref)->key = key;
(*node_ref)->next = NULL;
return;
}
add_end(&((*node_ref)->next), key);
}
void print_list(node_t **node_ref)
{
if(*node_ref == NULL)
{
return;
}
printf("Node Key: %d\n", (*node_ref)->key);
print_list(&((*node_ref)->next));
}
void func(node_t **ptr_ref, node_t *ptr)
{
printf("Pointer is: %X, pointer adress is %X\n", ptr, &ptr);
printf("Pointer ref: %X, pointer ref holds: %X\n", ptr_ref, *ptr_ref);
}
void main()
{
node_t *head = NULL;
printf("Head: %X\n", &head);
func(&head, head);
for(int i=0; i<5; i++)
{
add_end(&head, i);
}
print_list(&head);
}