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
| #include <stdlib.h>
#include <stdio.h>
typedef struct node{
int data;
struct node* next;
}node, *linklist;
void InitLinklist(linklist* head){
if(*head != NULL){
free(*head);
}
*head = (node*)malloc(sizeof(node));
(*head)->next = NULL;
}
void InsertNode(linklist* head, int d){
node* newNode = (node*)malloc(sizeof(node));
newNode->data = d;
newNode->next = (*head)->next;
(*head)->next = newNode;
}
//using recursion to get the length of the linklist;
int GetLengthRecursive(linklist head){
if(head->next == NULL){
return 0;
}
return (GetLengthRecursive(head->next) + 1);
}
//using tail recursive to get the length of a linklist,
//with the help of the variable "acc" to store the length of the current linklist, increment it
int GetLengthTailRecursive(linklist head, int *acc){
if(head->next == NULL){
return *acc;
}
*acc = *acc + 1;
return GetLengthTailRecursive(head->next, acc);
}
void PrintLinklist(linklist head){
node* pnode = head->next;
while(pnode){
printf("%d->", pnode->data);
pnode = pnode->next;
}
printf("->NULL\n");
}
int main(){
linklist head = NULL;
int len = 0;
InitLinklist(&head);
InsertNode(&head, 10);
InsertNode(&head, 21);
InsertNode(&head, 14);
InsertNode(&head, 19);
InsertNode(&head, 132);
InsertNode(&head, 192);
PrintLinklist(head);
printf("The length of linklist is: %d\n", GetLengthRecursive(head));
GetLengthTailRecursive(head, &len);
printf("the length of linklist is: %d\n", len);
}
|