-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoule_lst_c_i_d_l.c
123 lines (107 loc) · 2.19 KB
/
doule_lst_c_i_d_l.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
112
113
114
115
116
117
118
119
120
121
122
123
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUF_LEN 100
typedef struct node {
struct node *next;
struct node *pre;
char string[BUF_LEN];
}node_t;
node_t *g_head = NULL;
void insert_node (node_t **head, char *str)
{
node_t *tmp = NULL;
tmp = (node_t*)malloc(sizeof(node_t));
if (!tmp)
{
printf("dddd\n");
return;
}
strncpy(tmp->string, str, BUF_LEN);
tmp->next = NULL;
tmp->pre = NULL;
if (*head == NULL)
{
*head = tmp;
}
else
{
node_t *pHead = *head;
while(pHead->next != NULL)
pHead = pHead->next;
pHead->next = tmp;
tmp->pre = pHead;
}
}
void del_node (node_t **head, char *str)
{
node_t* tmp = NULL;
if (!*head)
{
printf("....\n");
return;
}
node_t* pHead = *head;
while(pHead != NULL)
{
if (!strncmp(pHead->string, str, BUF_LEN))
{
tmp = pHead;
break;
}
pHead = pHead->next;
}
if (!tmp)
{
printf("cannot find the string");
return;
}
else if (tmp == *head) //head
{
printf("delete %s\n", tmp->string);
*head = tmp->next;
(*head)->pre = NULL;
free(tmp);
}
else if (tmp->next == NULL) //tail
{
printf("delete %s\n", tmp->string);
tmp->pre->next = NULL;
free(tmp);
}
else
{
printf("delete %s\n", tmp->string);
tmp->pre->next = tmp->next;
tmp->next->pre = tmp->pre;
free(tmp);
}
}
void show_list(node_t *head)
{
if (!head)
{
printf("NULL double link list\n");
return;
}
while(head != NULL)
{
printf("%s\n", head->string);
head = head->next;
}
}
int main()
{
insert_node(&g_head, "China");
insert_node(&g_head, "Nanjing");
insert_node(&g_head, "Guangdong");
insert_node(&g_head, "Shenzhen");
insert_node(&g_head, "Sichuan");
insert_node(&g_head, "Wuhan");
insert_node(&g_head, "Hunan");
show_list(g_head);
// del_node(&g_head, "xxxx");
del_node(&g_head, "Shenzhen");
show_list(g_head);
return 0;
}