#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
void link_list_Traversal_print(struct Node *ptr)
{
while (ptr != NULL)
{
printf("Element : %d\n", ptr->data);
ptr = ptr->next;
}
}
struct Node *deleteAtFirst(struct Node *head)
{ // Case : 1 : deletionat the first
struct Node *ptr = head;
head=head->next;
free(ptr);
return head;
}
struct Node *deleteAtEnd(struct Node *head)
{ // Case : 2 : deletion at the end
struct Node *p = head;
struct Node *q = head->next;
while (q->next != NULL)
{
p = p->next;
q = q->next;
}
p->next = NULL;
free(q);
return head;
}
struct Node *deleteAtIndex(struct Node *head, int index)
{ // Case : 3 : deletion at any index
struct Node *p = head;
struct Node *q = head->next;
for ( int i = 0 ; i< index-1 ; i++){
p=p->next;
q=q->next;
}
p->next=q->next;
free(q);
return head;
}
struct Node *deleteAtNode(struct Node *head, int Value)
{ // Case : 4 : deletion at a given Node
struct Node* p = head;
struct Node* q = head->next;
while(q->data !=Value && q->next !=NULL){
p = p->next;
q = q->next;
}
if(q->data == Value){
p->next = q->next;
free(q);
}
return head;
}
int main()
{
struct Node *head;
struct Node *secoond;
struct Node *third;
struct Node *fourth;
struct Node *fifth;
head = (struct Node *)malloc(sizeof(struct Node));
secoond = (struct Node *)malloc(sizeof(struct Node));
third = (struct Node *)malloc(sizeof(struct Node));
fourth = (struct Node *)malloc(sizeof(struct Node));
fifth = (struct Node *)malloc(sizeof(struct Node));
head->data = 11;
head->next = secoond;
secoond->data = 85;
secoond->next = third;
third->data = 63;
third->next = fourth;
fourth->data = 25;
fourth->next = fifth;
fifth->data = 45;
fifth->next = NULL;
printf("Before Deletion : \n");
link_list_Traversal_print(head);
printf("\nAfter Deletion : \n");
//head = deleteAtFirst(head);
//head = deleteAtEnd(head);
//head = deleteAtIndex(head,2);
head = deleteAtNode(head,25);
link_list_Traversal_print(head);
return 0;
}

0 Comments