How is this Implementation of a list in C?
Please tell me whether it is good or please mention any problems. It is a singly linked List and I tried to do it recursively.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct node{
int value;
struct node *next;
};
void add(struct node *lst, int num){
if(lst->next == NULL){
lst->value = num;
lst->next = (struct node*)malloc(sizeof(struct node*));
lst->next->next = NULL;
}else{
add(lst->next, num);
}
}
void print_lst(struct node *lst){
if(lst->next == NULL){
}else{
printf("\n%d", lst->value);
print_lst(lst->next);
}
}
int length(struct node *lst){
if(lst->next == NULL){
return 0;
}else{
return 1 + length(lst->next);
}
}
int get(struct node *lst, int pos){
if(pos < 0 || pos >= length(lst)){
fprintf(stderr ,"\nIndexOutOfBoundsException\n");
}else if(pos > 0 && pos <length(lst)){
pos += -1;
get(lst->next, pos);
} else if (pos == 0){
return lst->value;
}
}
int main ( ) {
struct node lst;
lst.next = NULL;
add(&lst, 13);
add(&lst, 12);
add(&lst, 1);
add(&lst, 10);
add(&lst, 10);
add(&lst, 4);
//print_lst(&lst);
printf("\n%d", get(&lst, 2));
printf("\n%d", get(&lst, 5));
printf("\n%d", get(&lst, 13));
printf("\n\n%d", length(&lst));
return 0;
}