/
list_remove_element.c
78 lines (64 loc) · 1.2 KB
/
list_remove_element.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
#include <stdio.h>
typedef struct node{
int data;
struct node *next;
} node;
typedef node * list_head;
node nodes[10];
list_head create_list(void)
{
list_head head = NULL;
head = &nodes[0];
int cnt = sizeof(nodes)/sizeof(nodes[0]);
for(int i=0; i< cnt-1; i++)
{
nodes[i].data = i;
nodes[i].next = &nodes[i+1];
}
nodes[cnt-1].data = cnt-1;
nodes[cnt-1].next = NULL;
return head;
}
void print_list(const list_head head)
{
node *pNode;
pNode = (node *)head;
while(pNode)
{
printf("%d ", pNode->data);
pNode = pNode->next;
}
printf("\n");
}
void list_remove_element(list_head *head, int value)
{
node **pNode = head;
while(*pNode)
{
if((*pNode)->data == value)
{
*pNode = (*pNode)->next;
}
else
{
pNode = &((*pNode)->next);
}
}
}
int main()
{
list_head head;
head = create_list();
printf("Original list : ");
print_list(head);
list_remove_element(&head, 0);
printf("After remove 0 : ");
print_list(head);
list_remove_element(&head, 5);
printf("After remove 5 : ");
print_list(head);
list_remove_element(&head, 9);
printf("After remove 9 : ");
print_list(head);
return 0;
}