recent

Write a function/Program to delete a Linked List(Recursive Method)

Recursive Method to delete a Linked List

#include<stdio.h>
#include<stdlib.h>

struct node{
int data;
struct node *next;
};
void insert(struct node **head,int new_data)
{

struct node *new_node;
new_node=(struct node*)malloc(sizeof(struct node));

new_node->data=new_data;
new_node->next=*head;
*head=new_node;

}

void DeleteLinkListRecursive(struct node *head)
{

DeleteLinkListRecursive(head);
free(head);
}

void traverse(struct node *head)
{
while(head!=NULL)
{
printf("%d\t",head->data);
head=head->next;
}
printf("\n");
}
main()

{

struct node *head;

insert(&head,1);
insert(&head,2);
insert(&head,3);
insert(&head,4);

traverse(head);


DeleteLinkListRecursive(head);

traverse(head);
}
Powered by Blogger.