-
Notifications
You must be signed in to change notification settings - Fork 0
/
18_LinkedListDeletion.c
112 lines (94 loc) · 2.34 KB
/
18_LinkedListDeletion.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include<stdio.h>
#include<stdlib.h>
//struct node for containing an element and address of the next structure to it where next element is present
struct Node{
int data;
struct Node *next;
};
//travelsing through list to print the data
void linkedlisttraversal(struct Node *ptr ){
while((ptr)!=NULL){
printf("Element: %d\n",ptr->data);
ptr = ptr->next;}
}
struct Node * deleteFirst(struct Node *head){
struct Node * p;
p = head->next;
free(head);
return p;
}
void delete_at_index(struct Node *head,int index){
struct Node *p = head;
struct Node *q;
int i = 0;
while(i!=index-1){
p =p->next;
i++;
}
q= p->next;
p->next = q->next;
free(q);
}
void delete_at_end(struct Node *head){
struct Node *p = head;
struct Node *q;
q =p->next;
while(q->next != NULL){
p = q;
q=q->next;
}
p->next = NULL;
free(q);
}
//very important case
void delete_the_element(struct Node *head,int element){
struct Node *p = head;
struct Node *q;
q =p->next;
if(p->data == element){
printf("this is the first element try other funciton\n");
exit(0);
}
while(q->data != element && q->next!=NULL){
p = q;
q=q->next;
}
if (q->data == element)
{p->next = q->next;
free(q);}
else{
printf("\n****The element given not found**** \n\n");
}
}
int main()
{ //intializing various struct Node pointers;
struct Node *head;
struct Node *ptr;
struct Node *ptr1;
struct Node *ptr2;
struct Node *second;
struct Node *third;
int index ;
//allocate memory of nodes in the linked list in heap
head=(struct Node *)malloc(sizeof(struct Node));
second=(struct Node *)malloc(sizeof(struct Node));
third=(struct Node *)malloc(sizeof(struct Node));
//link first and second node
head->data=7;
head->next=second;
//link third and second node
second->data=11;
second->next=third;
third->data=66;
third->next=NULL ;//important to close the linked list we pointed last 'next'o NULL
linkedlisttraversal(head);
// head = deleteFirst(head); //for deleting the first element
// index = 1; For deleting the element at given index
// delete_at_index(head,index);
// delete_at_end(head); // for deletion at the end
int element = 643;
delete_the_element(head,element);
printf("list after deletion \n");
linkedlisttraversal(head);
return 0;
}