In: Computer Science
Assuming:
struct node
{
int data;
node * next;
};
and
addList(node * head);
Write a function to recursively add to the end of a linear linked list.
Here is the c code for
you:
#include <stdio.h>
#include <stdlib.h>
struct Node{
int data;
struct Node * next;
};
struct Node* addList(struct Node* head,int value){
if(head==NULL){
struct Node* newNode=(struct Node
*)malloc(sizeof(struct Node));
newNode->data=value;
newNode->next=NULL;
return newNode;
}
else{
head->next=addList(head->next,value);
}
return head;
}
void print_Nodes(struct Node * head){
if(head == NULL){
printf("Linked list is
empty");
}
else{
struct Node * temp = head;
while(temp!=NULL){
printf("%d
",temp->data);
temp=temp->next;
}
}
}
int main(){
struct Node *head=NULL;
int i;
for(i=0;i<5;i++){
head= addList(head,i);
}
print_Nodes(head);
return 0;
}
Here is the snap shots of code and its sample output:


sample output:

If you have any doubts please comment.
If you like answer please give THUMBS UP.