Delete a Node from a Linked List data structure
#include<stdio.h>
struct node
{
int data;
struct node *next;
};
void print(struct node *print_node)
{
struct node *temp;
temp=print_node;
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
printf("\n");
}
struct node *creatnode(int arr[],int siz)
{
struct node *head=NULL,*current=NULL,*temp=NULL;
for(int i=0;i<siz;i++)
{
temp=(struct node *)malloc(sizeof(struct node));
temp->data=arr[i];
temp->next=NULL;
if(head==NULL)
{
head=temp;
current=temp;
}
else
{
current->next=temp;
current=temp;
}
}
return head;
};
struct node *deletenode(struct node *newn,int data)
{
struct node *temp,*newhead;
newhead=(struct node *)malloc(sizeof(struct node));
newhead->next=newn;
temp=newhead;
while(temp->next!=NULL)
{
if(temp->next->data==data)
{
temp->next=temp->next->next;
}
else temp=temp->next;
}
return newhead->next;
};
int main()
{
struct node *head,*head1;
int ar[]={1,2,3,4,6,6,6,7,7};
head=creatnode(ar,9);
print(head);
head1=deletenode(head,7);
print(head1);
return 0;
}
0 Comments