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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
| #include "list.h"
bool Initialize(List *list, int capacity) { (*list) = (ListStruct *)malloc(sizeof(ListStruct)); (*list)->capacity = capacity; (*list)->size = 0; (*list)->items = (Item *)malloc(capacity * sizeof(Item)); if ((*list)->items != NULL) { printf("初始化成功!!! 表的可用容量为%d\n", (*list)->capacity); return true; } else { printf("初始化失败!!!\n"); return false; } }
bool Destroy(List *list) { free((*list)->items); free(*list); printf("销毁顺序表成功!!!\n"); return true; }
bool Clear(List *list) { (*list)->size = 0; printf("已清空表,当前表中元素个数为%d,表的可用容量为%d\n", (*list)->size, (*list)->capacity - (*list)->size); return true; }
bool Empty(List *list) { if ((*list)->size == 0) { printf("空表!!!\n"); return true; } else { printf("不是空表!!!\n"); return false; } }
bool Full(List *list) { if ((*list)->size == (*list)->capacity) { printf("满了!!!\n"); return true; } else { printf("还没有满!!!\n"); return false; } }
int Count(List *list) { printf("当前表中元素个数为%d\n", (*list)->size); return (*list)->size; }
int Capacity(List *list) { printf("表的容量为%d ,表的可用容量为%d\n", (*list)->capacity, (*list)->capacity - (*list)->size); return (*list)->capacity; }
bool AddCapacityTo(List *list, int addcapacity) { int curcapacity = (*list)->capacity; if (addcapacity >= curcapacity) { (*list)->items = (Item *)realloc((*list)->items, addcapacity * sizeof(Item)); (*list)->capacity = addcapacity; printf("容量增加成功!!!\n"); printf("表的容量为%d ,表的可用容量为%d\n", (*list)->capacity, (*list)->capacity - (*list)->size); } else { printf("容量增加函数不可以减小容量\n"); printf("表的容量为%d ,表的可用容量为%d\n", (*list)->capacity, (*list)->capacity - (*list)->size); return false; } }
bool SubCapacityTo(List *list, int subcapacity) { int curcapacity = (*list)->capacity; if (subcapacity <= curcapacity && subcapacity > 0) { (*list)->items = (Item *)realloc((*list)->items, subcapacity * sizeof(Item)); (*list)->capacity = subcapacity; if ((*list)->size > subcapacity) { (*list)->size = subcapacity; } printf("容量减少成功!!!\n"); printf("表的容量为%d ,表的可用容量为%d\n", (*list)->capacity, (*list)->capacity - (*list)->size); } else if (subcapacity <= 0) { printf("容量只可以为正数\n"); printf("表的容量为%d ,表的可用容量为%d\n", (*list)->capacity, (*list)->capacity - (*list)->size); return false; } else { printf("容量减少函数不可以增加容量\n"); printf("表的容量为%d ,表的可用容量为%d\n", (*list)->capacity, (*list)->capacity - (*list)->size); return false; } }
bool Shrink_To_Fit(List *list) { (*list)->items = (Item *)realloc((*list)->items, (*list)->size * sizeof(Item)); if ((*list)->items != NULL || ((*list)->items == NULL && (*list)->size == 0)) { (*list)->capacity = (*list)->size; printf("fit成功!!!表的容量为%d ,表的可用容量为%d\n", (*list)->capacity, (*list)->capacity - (*list)->size); return true; } else { printf("fit失败!!!表的容量为%d ,表的可用容量为%d\n", (*list)->capacity, (*list)->capacity - (*list)->size); return false; } }
bool At(List *list, int location, Item *item_to_save) { if (location < 0 || location >= (*list)->size) { printf("AT超限!!!\n"); return false; } *item_to_save = (*list)->items[location]; printf("这就是你AT的元素\n"); return true; }
int LocateFirst(List *list, Item *item_to_save, bool (*fun)(Item item)) { int location = -1; for (int i = 0; i <= (*list)->size - 1; i++) { if (fun((*list)->items[i])) { location = i; *item_to_save = (*list)->items[i]; break; } } if (location != -1) { printf("找到了,第%d个元素满足条件\n", location); } else { printf("没有找到满足条件的元素\n"); } return location; }
int LocateLast(List *list, Item *item_to_save, bool (*fun)(Item item)) { int location = -1; for (int i = (*list)->size - 1; i >= 0; i--) { if (fun((*list)->items[i])) { location = i; *item_to_save = (*list)->items[i]; break; } } if (location != -1) { printf("找到了,第%d个元素满足条件\n", location); } else { printf("没有找到满足条件的元素\n"); } return location; }
bool Previous(List *list, Node *node, Node *previous) { if (node->location >= 1 && node->location <= (*list)->size - 1) { previous->location = node->location - 1; printf("找到了前驱!!!\n"); return true; } else { printf("没有前驱!!!\n"); return false; } }
bool Success(List *list, Node *node, Node *success) { if (node->location >= 0 && node->location <= (*list)->size - 2) { success->location = node->location + 1; printf("找到了后继!!!\n"); return true; } else { printf("没有后继!!!\n"); return false; } }
bool Insert(List *list, int location, Item item) { if ((*list)->size == (*list)->capacity) { printf("满了,不能插入!!!\n"); return false; } else if (location < 0 || location > (*list)->size) { printf("插入位置不合法!!!\n"); return false; } else if ((*list)->size < (*list)->capacity) { for (int i = (*list)->size; i != location; i--) { (*list)->items[i] = (*list)->items[i - 1]; } (*list)->items[location] = item; (*list)->size++; printf("插入成功!!!\n"); printf("表的大小为%d ,表的容量为%d ,表的可用容量为%d\n", (*list)->size, (*list)->capacity, (*list)->capacity - (*list)->size); return true; } }
bool Delete(List *list, int location) { if (location < 0 || location >= (*list)->size) { printf("插入位置不合法!!!\n"); return false; } else { for (int i = location; i <= (*list)->size - 2; i++) { (*list)->items[i] = (*list)->items[i + 1]; } (*list)->size--; printf("删除成功!!!\n"); printf("表的大小为%d ,表的容量为%d ,表的可用容量为%d\n", (*list)->size, (*list)->capacity, (*list)->capacity - (*list)->size); return true; } }
void Traverse(List *list, void (*fun)(Item item)) { for (int i = 0; i <= (*list)->size - 1; i++) { fun((*list)->items[i]); } }
void Sort_Up(List *list) { int n = (*list)->size; for (int i = 0; i <= n - 1; i++) { for (int j = 0; j <= n - 1 - i; j++) { if ((*list)->items[j].value > (*list)->items[j - 1].value) { Item temp = (*list)->items[j]; (*list)->items[j] = (*list)->items[j - 1]; (*list)->items[j - 1] = temp; } } } }
void Sort_Down(List *list) { int n = (*list)->size; for (int i = 0; i <= n - 1; i++) { for (int j = 0; j <= n - 1 - i; j++) { if ((*list)->items[j].value < (*list)->items[j - 1].value) { Item temp = (*list)->items[j]; (*list)->items[j] = (*list)->items[j - 1]; (*list)->items[j - 1] = temp; } } } }
void Sort_DIY(List *list, bool (*fun)(Item cur, Item next)) { int n = (*list)->size; for (int i = 0; i <= n - 1; i++) { for (int j = 0; j <= n - 2; j++) { if (fun((*list)->items[j], (*list)->items[j + 1])) { Item temp = (*list)->items[j]; (*list)->items[j] = (*list)->items[j + 1]; (*list)->items[j + 1] = temp; } } } }
bool Merge(List *list1, List *list2, List *list) { int size = (*list)->size; int size1 = (*list1)->size; int size2 = (*list2)->size; int capacity = (*list)->capacity; int capacity1 = (*list1)->capacity; int capacity2 = (*list2)->capacity; if (capacity < size1 + size2) { printf("目的表太小了!!!\n"); return false; } else { int cur = 0; (*list)->size = 0; for (int i = 0; i <= size1 - 1; i++) { (*list)->items[cur++] = (*list1)->items[i]; (*list)->size++; } for (int i = 0; i <= size2 - 1; i++) { (*list)->items[cur++] = (*list2)->items[i]; (*list)->size++; } return true; } }
bool Copy(List *list_tobe_copied, List *List_copied_to) { int size1 = (*list_tobe_copied)->size; int size2 = (*List_copied_to)->size; int capacity1 = (*list_tobe_copied)->capacity; int capacity2 = (*List_copied_to)->capacity; if (capacity2 < size1) { printf("目标顺序表容量太小了,装不下!!!\n"); } else { Clear(List_copied_to); for (int i = 0; i <= size1 - 1; i++) { (*List_copied_to)->size++; (*List_copied_to)->items[i] = (*list_tobe_copied)->items[i]; } } }
|