-
Notifications
You must be signed in to change notification settings - Fork 32
/
deletion.c
70 lines (55 loc) · 1.02 KB
/
deletion.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
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *next;
} node;
node *head = NULL;
void insert(int data) {
node *new_node = malloc(sizeof(node));
new_node->data = data;
new_node->next = NULL;
if (head == NULL) {
head = new_node;
} else {
node *current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = new_node;
}
}
void delete(int data) {
node *current = head;
node *previous = NULL;
while (current != NULL && current->data != data) {
previous = current;
current = current->next;
}
if (current == NULL) {
return;
}
if (previous == NULL) {
head = current->next;
} else {
previous->next = current->next;
}
free(current);
}
void print() {
node *current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main() {
insert(10);
insert(20);
insert(30);
print();
delete(10);
print();
return 0;
}