A pointer Given to Delete a node in a singly linked list

Delete A Node in Singly Linked List:- ->Traverse the linked list until you find the node you want to delete
->Problem in this approch is pointer to the head node

#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 count(struct node *head,int n)
{
struct node *current;
current=head;
int count=1;

while(count!=n)
{
current=current->next;
count++;
}
if(count=n)
{
printf("%dth node is:%d\n",n,current->data);
}

}

void LinkListTraversal(struct node *n)

{
while(n->next!=0)
{
printf("%d\t",n->data);
n=n->next;
}
printf("\n");
}

main()
{

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

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

int n;
printf("Find nth node:\n");
scanf("%d",&n);

count(head,n);

LinkListTraversal(head);

}


Related Posts