-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_list.c
More file actions
53 lines (49 loc) · 939 Bytes
/
Copy pathdelete_list.c
File metadata and controls
53 lines (49 loc) · 939 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
void remove_element(list *li, int target) {
if (!li) {
return;
}
node *head = li->head;
if (!head) {
return;
}
node *pre = NULL;
node *cur = head;
node *next = head->next;
while (cur) {
if (cur->val == target) {
if (!pre) {
li->head = next;
} else {
pre->next = next;
}
return;
}
pre = cur;
cur = next;
if (next) {
next = next->next;
}
}
}
int main(int argc, char **argv) {
list *li = (list *)malloc(sizeof(list));
node *head = (node *)malloc(sizeof(node));
head->val = 1;
node *second = (node *)malloc(sizeof(node));
second->val = 2;
node *third = (node *)malloc(sizeof(node));
third->val = 3;
head->next = second;
second->next = third;
li->head = head;
remove_element(li, 3);
head = li->head;
while (head) {
printf("%d\n", head->val);
head = head->next;
}
}