#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int x;
struct Node *next;
} Node;
int count_elements_recursive(Node *root) {
if (root == NULL) {
return 0;
}
return 1 + count_elements_recursive(root->next);
}
void add_node(Node **root, int value) {
Node *new_node = malloc(sizeof(Node));
if (new_node == NULL) {
puts("malloc error");
exit(1);
}
new_node->next = NULL;
new_node->x = value;
if (*root == NULL) {
*root = new_node;
return;
}
Node *current = *root;
while (current->next != NULL) {
current = current->next;
}
current->next = new_node;
}
void free_LinkedList(Node *root) {
Node *next;
while (root != NULL) {
next = root->next;
free(root);
root = next;
}
}
int main() {
Node *root = NULL;
add_node(&root, 5);
add_node(&root, 300);
add_node(&root, 88);
add_node(&root, 9900);
add_node(&root, 20);
add_node(&root, 4);
add_node(&root, -3);
printf("%d\n", count_elements_recursive(root));
free_LinkedList(root);
return 0;
}
/*
run:
7
*/