-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.c
145 lines (123 loc) · 2.91 KB
/
list.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
137
138
139
140
141
142
143
144
145
#define EXT_LIST
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
listStatus list_create(head_t* head, uint32_t n)
{
int i;
node_t* p = NULL;
node_t* r = NULL;
head ->node = (node_t*)malloc(sizeof(node_t));
if(head ->node == NULL)return ERROR;
head ->length = 0;
r = head ->node;
for(i = 0; i < n; i++)
{
p = (node_t*)malloc(sizeof(node_t));
if(p == NULL)return ERROR;
//----------节点数据赋值开始----------
//----------节点数据赋值结束----------
r ->next = p;
r = p;
head ->length++;
}
r ->next = NULL;
return SUCCESS;
}
void list_clear(head_t* head)
{
node_t* p = NULL;
node_t* q = NULL;
p = head ->node ->next;
while(p)
{
q = p ->next;
free(p);
p = q;
head ->length--;
}
head ->node ->next = NULL;
}
listStatus list_is_empty(head_t* head)
{
if(head ->node ->next == NULL)//链表为空
return SUCCESS;
else
return ERROR;
}
uint32_t list_get_length(head_t* head)
{
return head ->length;
}
listStatus list_get_nodedata(head_t* head, uint32_t index, nodedata_t* nodedata)
{
node_t *p = NULL;
uint32_t i;
//传参校验
if( (index > head ->length)||(index < 1) )return ERROR;
p = head ->node;
for(i = 0; i < index; i++){
p = p ->next;
}
//元素赋值
memcpy((void*)nodedata, (const void*)&(p ->data), sizeof(nodedata_t));
return SUCCESS;
}
int32_t list_check_nodedata(head_t* head, int32_t connect_fd, nodedata_t* nodedata)
{
node_t *p = NULL;
uint32_t cnt = 1;
int32_t result;
p = head ->node ->next;
while(p)
{
//判断给出的元素与链表当前节点数据区的值是否相等
result = memcmp((const void*)&nodedata, (const void*)&(p ->data), sizeof(nodedata_t));
if(connect_fd == p ->data.connect_fd){
memcpy((void*)nodedata, (const void*)&(p ->data), sizeof(nodedata_t));
return cnt;
}
p = p ->next;
cnt++;
}
return -1;
}
listStatus list_insert_nodedata(head_t* head, uint32_t index, nodedata_t nodedata)
{
node_t *p = NULL;
node_t *s = NULL;
uint32_t length;
uint32_t i;
//传参校验
if( (index > (head->length + 1))||(index < 1) )return ERROR;
p = head ->node;
for(i = 0; i < (index-1); i++){
p = p ->next;
}
s = (node_t*)malloc(sizeof(node_t));
if(s == NULL)return ERROR;
memcpy((void*)&(s ->data), (const void*)&nodedata, sizeof(nodedata_t));
s ->next = p ->next;
p ->next = s;
head ->length++;
return SUCCESS;
}
listStatus list_delete_nodedata(head_t* head, uint32_t index, nodedata_t* nodedata)
{
node_t *p = NULL;
node_t *q = NULL;
uint32_t length;
uint32_t i;
//传参校验
if( (index > head ->length)||(index < 1) )return ERROR;
p = head ->node;
for(i = 0; i < (index-1); i++){
p = p ->next;
}
q = p ->next;
p ->next = q ->next;
free(q);
head ->length--;
return SUCCESS;
}