C语言:构建一个二级链表并完成增删改查
生活随笔
收集整理的這篇文章主要介紹了
C语言:构建一个二级链表并完成增删改查
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
構建一個下圖所示的鏈表,并完成增、刪、改、查
示例代碼:
#include <stdio.h> #include <stdlib.h> #include <string.h>#define uint32_t intstruct key_list {char keyid[20];struct key_list *next; };struct app_list {char app_name[20];struct key_list *key_head;struct key_list *key_tail;struct app_list *next; };struct app_list *app_head = NULL; struct app_list *app_tail = NULL;static int insert_id(struct app_list *app_name, char *keyid) {struct key_list *item = NULL;item = (struct key_list *)malloc(sizeof(struct key_list));memcpy(item->keyid, keyid, sizeof(item->keyid));item->next = NULL;if (app_name->key_head == NULL){app_name->key_head = item;app_name->key_tail = item;} else {app_name->key_tail->next = item;app_name->key_tail = item;} }static struct key_list * find_id(struct app_list *app_name, char *keyid, uint32_t len) {struct key_list *item = app_name->key_head;while(item != NULL){if(memcmp(item->keyid, keyid, len) == 0)break;item=item->next;}return item; }static int delete_id(struct app_list *app_name, char *keyid, uint32_t len) {struct key_list *pre_item = app_name->key_head;struct key_list *item = NULL;if (pre_item == NULL){return 0;}else if (memcmp(pre_item->keyid, keyid, len) == 0){app_name->key_head = pre_item->next;app_name->key_tail = pre_item->next;free(pre_item);return 0;}item = pre_item->next;while(item != NULL){if(memcmp(item->keyid, keyid, len) == 0){if(item->next == NULL)app_name->key_tail = pre_item;pre_item->next = item->next;free(item);break;}pre_item = pre_item->next;item = item->next;}return 0; }static int dump_ids(struct app_list *app_name) {struct key_list *item = app_name->key_head;while(item != NULL){printf("\tkeyid : %s\n",item->keyid);item = item->next;}return item; }//----------------------------------static int insert_app(char *app_name) {struct app_list *item = NULL;item = (struct app_list *)malloc(sizeof(struct app_list));memcpy(item->app_name, app_name, sizeof(item->app_name));item->key_head = NULL;item->key_tail = NULL;item->next = NULL;if (app_head == NULL){app_head = item;app_tail = item;} else {app_tail->next = item;app_tail = item;} }static struct app_list * find_app(char *app_name, uint32_t len) {struct app_list *item = app_head;while(item != NULL){if(memcmp(item->app_name, app_name, len) == 0)break;item=item->next;}return item; }static int delete_app(char *app_name, uint32_t len) {struct app_list *pre_item = app_head;struct app_list *item = NULL;if (pre_item == NULL){return 0;}else if (memcmp(pre_item->app_name, app_name, len) == 0){app_head = pre_item->next;app_tail = pre_item->next;free(pre_item);return 0;}item = pre_item->next;while(item != NULL){if(memcmp(item->app_name, app_name, len) == 0){if(item->next == NULL)app_tail = pre_item;pre_item->next = item->next;free(item);break;}pre_item = pre_item->next;item = item->next;}return 0; }static int dump_apps(void) {struct app_list *item = app_head;while(item != NULL){printf("app_name : %s\n",item->app_name);item = item->next;}return item; }//----------------------------------static void test_app() {struct app_list *item = NULL;struct key_list *key_item = NULL;printf("------------- insert_app test ----------------\n");insert_app("app1");insert_app("app2");insert_app("app3");insert_app("app4");dump_apps();printf("------------- find_app test ----------------\n");item = find_app("app4", 4);printf("app_name : %s\n",item->app_name);printf("------------- delete_app test ----------------\n");delete_app("app1", 4);delete_app("app4", 4);dump_apps();insert_app("app1");insert_app("app4");//---------------------------------------printf("------------- insert_id test ----------------\n");item = find_app("app1", 4);insert_id(item,"id1_xxxxxxxx");insert_id(item,"id2_xxxxxxxx");insert_id(item,"id3_xxxxxxxx");insert_id(item,"id4_xxxxxxxx");item = find_app("app2", 4);insert_id(item,"id1_xxxxxxxx");insert_id(item,"id2_xxxxxxxx");insert_id(item,"id3_xxxxxxxx");insert_id(item,"id4_xxxxxxxx");item = find_app("app3", 4);insert_id(item,"id1_xxxxxxxx");insert_id(item,"id2_xxxxxxxx");insert_id(item,"id3_xxxxxxxx");insert_id(item,"id4_xxxxxxxx");item = find_app("app4", 4);insert_id(item,"id1_xxxxxxxx");insert_id(item,"id2_xxxxxxxx");insert_id(item,"id3_xxxxxxxx");insert_id(item,"id4_xxxxxxxx");dump_ids(find_app("app1", 4));dump_ids(find_app("app2", 4));dump_ids(find_app("app3", 4));dump_ids(find_app("app4", 4));printf("------------- find_id test ----------------\n");key_item = find_id(find_app("app4", 4), "id1_xxxxxxxx", 4);printf("keyid : %s\n",key_item->keyid);printf("------------- delete_id test ----------------\n");delete_id(find_app("app4", 4),"id1_xxxxxxxx", 4);delete_id(find_app("app4", 4),"id4_xxxxxxxx", 4);dump_ids(find_app("app4", 4)); }int main() {printf("111\n");test_app();printf("222\n");return 0; }總結
以上是生活随笔為你收集整理的C语言:构建一个二级链表并完成增删改查的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [Issue Fixed]-GCC编译[
- 下一篇: python编写一个压测重启的测试程序