-
Notifications
You must be signed in to change notification settings - Fork 9
/
Deletion_in_linklist.c
111 lines (95 loc) · 2.5 KB
/
Deletion_in_linklist.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
//Deletion of a node in a linked list
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void addLast(struct node **head, int val)
{
//create a new node
struct node *newNode = malloc(sizeof(struct node));
newNode->data = val;
newNode->next = NULL;
//if head is NULL, it is an empty list
if(*head == NULL)
*head = newNode;
//Otherwise, find the last node and add the newNode
else
{
struct node *lastNode = *head;
//last node's next address will be NULL.
while(lastNode->next != NULL)
{
lastNode = lastNode->next;
}
//add the newNode at the end of the linked list
lastNode->next = newNode;
}
}
void deleteNode(struct node **head, int key)
{
//temp is used to freeing the memory
struct node *temp;
//key found on the head node.
//move to head node to the next and free the head.
if((*head)->data == key)
{
temp = *head; //backup to free the memory
*head = (*head)->next;
free(temp);
}
else
{
struct node *current = *head;
while(current->next != NULL)
{
//if yes, we need to delete the current->next node
if(current->next->data == key)
{
temp = current->next;
//node will be disconnected from the linked list.
current->next = current->next->next;
free(temp);
break;
}
//Otherwise, move the current node and proceed
else
current = current->next;
}
}
}
void printList(struct node *head)
{
struct node *temp = head;
//iterate the entire linked list and print the data
while(temp != NULL)
{
printf("%d ->", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main()
{
struct node *head = NULL;
addLast(&head,10);
addLast(&head,20);
addLast(&head,30);
printf("Linked List Elements:\n");
printList(head);
//delete first node
deleteNode(&head,10);
printf("Deleted 10. The New Linked List:\n");
printList(head);
//delete last node
deleteNode(&head,30);
printf("Deleted 30. The New Linked List:\n");
printList(head);
//delete 20
deleteNode(&head,20);
printf("Deleted 20. The New Linked List:\n");
printList(head);
return 0;
}