This repository has been archived by the owner on Oct 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 121
/
llist.c
137 lines (114 loc) · 2.83 KB
/
llist.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
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "llist.h"
linklist create_null_list_link(void)
{
linklist llist = (linklist)malloc(sizeof(struct node));
if (llist != NULL)
llist->next = NULL;
else
fprintf(stderr, "out of space!\n");
return llist;
} /* linklist create_null_list_link(void) */
int is_nulllist_link(linklist llist)
{
return llist->next == NULL;
} /* int is_nulllist_link(linklist llist) */
linklist insert_link(linklist llist, const char *ipaddr)
{
pnode q = (pnode)malloc(sizeof(struct node));
if (q == NULL) {
fprintf(stderr, "out of space!\n");
} else {
strcpy(q->node_info.ipaddr, ipaddr);
q->next = llist->next;
llist->next = q;
}
return llist;
} /* linklist insert_link(linklist llist, const char *ipaddr) */
linklist delete_node(linklist llist, const char *ipaddr)
{
if (llist->next == NULL)
return llist;
pnode q = llist->next;
pnode p = llist;
do {
if (!strcmp(q->node_info.ipaddr, ipaddr)) {
p->next = q->next;
free(q);
q = NULL;
break;
}
p = q;
q = q->next;
} while (q != NULL);
return llist;
} /* linklist delete_node(linklist llist, const char *ipaddr) */
pnode delete_this_node(linklist llist, pnode this_pnode)
{
if (llist->next == NULL)
return llist;
pnode q = llist->next;
pnode p = llist;
do {
if (q == this_pnode) {
p->next = q->next;
free(q);
q = NULL;
break;
}
p = q;
q = q->next;
} while (q != NULL);
return p;
} /* linklist delete_this_node(linklist llist, pnode this_pnode) */
/*
* return 0 if no find; else return 1.
*/
pnode search_node(linklist llist, const char *ipaddr)
{
pnode q = llist->next;
if (is_nulllist_link(llist))
return NULL;
do {
if (!strcmp(q->node_info.ipaddr, ipaddr))
return q;
q = q->next;
} while (q != NULL);
return NULL;
} /* int search_node(linklist llist, const char *ipaddr) */
int num_node(linklist llist)
{
int n = 0;
while (llist->next) {
n++;
llist = llist->next;
}
return n;
} /* int num_node(linklist llist) */
/*
* return 0: fail
* return 1: succeed
*/
int insert_nodulp_node(linklist llist, const char *ipaddr)
{
int ret = 0;
if (search_node(llist, ipaddr) == NULL) {
insert_link(llist, ipaddr);
ret = 1;
}
return ret;
} /* linklist insert_nodulp_node(linklist llist, const char *ipaddr) */
/*
* return 0: empty linklist
*/
int free_linklist(linklist llist)
{
if (llist->next == NULL)
return 0;
pnode q = llist->next->next;
do {
free(llist->next);
llist->next = q;
q = q->next;
} while (q != NULL);
return 1;
} /* int free_linklist(linklist llist) */