日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

C语言:构建一个二级链表并完成增删改查

發布時間:2025/3/21 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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语言:构建一个二级链表并完成增删改查的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。