蔡子经数据结构1.14

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


#include <stdlib.h>

typedef struct
{
int val;
struct * next;
} node;

node* createNode()
{
node *ret = (node*)malloc(sizeof(node));
ret->val = 0;
ret->next = NULL;
return ret;
}

void del(node *head, int a)
{
if(head->next->val == a) return;
node *pre = head;
node *p = head->next;
while(p->next)
{
if(p->next->val == a)
{
pre->next = p->next;
free(p);
return ;
}
p = p->next;
pre = pre->next;
}
return ;
}

int main()
{
node *head = createNode();
node *tail = head;
for(int i = 0; i < 10; ++i)
{
node *p = createNode();
p->val = i+1;
tail->next = p;
tail = tail->next;
}
del(head, 2);
node *p = head->next;
while(p)
{
printf("%d ",p->val);
p = p->next;
}
return 0;
}