构建一个下图所示的链表,并完成增、删、改、查

示例代码:

#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语言:构建一个二级链表并完成增删改查相关推荐

  1. Mysql —— C语言链接mysql数据库,实现可以增删改查的角色权限登录系统

    /******************************************************************** * 标题:C语言链接mysql数据库,实现可以增删改查的角色 ...

  2. mysql用创建的用户登陆并修改表格_MySQL 基础学习二:创建一个用户表,并增删改查...

    MySQL 基础学习二:创建一个用户表,并 增删改查 提示:MySQL 命令建议都用大写,因为小写运行时,还是翻译成大写的. 第一步,创建一个用户表 1,打开控制台,进入数据库 C:\Users\Ad ...

  3. go语言学习第八天==》mysql数据库增删改查、用go语言 客户端(client)发起htttp get请求,post请求,postForm请求,Head请求,Do请求

    go语言学习第八天==>mysql数据库增删改查.用go语言写 客户端(client)发起htttp get请求,post请求,postForm请求,Head请求,Do请求 引包 import的 ...

  4. 开发平台之美:10分钟内实现一个销售订单功能的增删改查

    IT技术发展了这么多年,早就应该抛弃那些copy&paste的工作了,毫无成就,毫无趣味,毫无好感.这直接催生了一大批快速开发平台的崛起,下面的视频讲述的就是通过一个开发平台如何在10分钟内实 ...

  5. 基于SpringBoot开发一个Restful服务,实现增删改查功能

    点击上方"方志朋",选择"置顶公众号" 技术文章第一时间送达! 作者:虚无境 cnblogs.com/xuwujing/p/8260935.html 前言 在去 ...

  6. springboot增删改查案例_大神基于SpringBoot开发一个Restful服务,实现增删改查功能...

    前言 在去年的时候,在各种渠道中略微的了解了SpringBoot,在开发web项目的时候是如何的方便.快捷.但是当时并没有认真的去学习下,毕竟感觉自己在Struts和SpringMVC都用得不太熟练. ...

  7. 单链表LinkedList的增删改查

    数组作为数据存储结构有一定的缺陷.在无序数组中,搜索性能差,在有序数组中,插入效率又很低(插入位置后面的元素需要集体后移),而且这两种数组的删除效率(集体前移)都很低,并且数组在创建后,其大小是固定了 ...

  8. java多数做增删改查_Java 实现一个 能够 进行简单的增删改查的 超市管理系统

    1. 首先编写一个 Fruitltem 的商品类, 描述 商品的基本信息. 代码如下: 保证详细, 运行的起来, 有什么 问题也可以评论留言. /* * 自定义类, 描述商品信息 * * 商品的属性: ...

  9. 使用SpringBoot一小时快速搭建一个简单后台管理(增删改查)(超详细教程)

    最近也是临近期末了,各种的期末大作业,后台管理也是很多地方需要用到的,为了方便大家能快速上手,快速搭建一个简单的后台管理,我花了两天时间整理了一下 我会从0开始介绍,从数据库的设计到前端页面的引入最后 ...

最新文章

  1. python自动化脚本实例100条-自动化运维基础实例解析-Python批量登录到服务器执行任务...
  2. CSDN技术主题月----“深度学习”代码笔记专栏
  3. 解决layui前端动态设置radio、checkbox默认选项的解决方案
  4. 直接排序python实现
  5. 大气波导计算MATLAB,基于抛物方程的大气波导环境下电波传播的研究rbedacv8.ppt
  6. 转:C#数据结构和算法学习系列十三----链表
  7. Centos7.5-文件的归档和压缩
  8. python学完面向对象之后_Python学完基础语法后,再往后应该学什么?
  9. 解决MSChart底部横坐标显示不全的问题
  10. 尴尬!苹果公司首开官方微博 迎接它的却是无穷无尽的吐槽...
  11. Apache Ranger——Hadoop ACL控制工具
  12. python爬虫要安装什么_python爬虫之分布式爬虫和部署
  13. [置顶]mybatis分页插件实现分页...
  14. Python3.2官方文档翻译--迭代器
  15. 厦门大学c语言2017,厦门大学2017年各专业录取分数线
  16. 流程控制的三个练习题的问题,请求解释,谢谢
  17. 侍魂微信第一个服务器,侍魂手游2019年4月12日微信问答试炼答案
  18. 什么是restful?怎样用通俗的语言解释restful?
  19. msys2在windows10系统的安装
  20. 搭建虚拟Web主机(基于域名、IP、端口)

热门文章

  1. 你是否能判断电机损毁风险?
  2. 数据中心怎么关机?光有UPS还不够
  3. ML:MLOps系列讲解之《MLOps原则—迭代增量过程/自动化/持续部署/版本控制/实验跟踪/测试/监控/“ML成绩”系统/可再现性/松散耦合架构(模块化)/基于ML的软件交付指标等》解读
  4. AI:2020年6月24日北京智源大会演讲分享之机器学习前沿青年科学家专题论坛——10:40-11:10金驰《Near-Optimal Reinforcement Learning with Sel》
  5. Paper:《A Unified Approach to Interpreting Model Predictions—解释模型预测的统一方法》论文解读与翻译
  6. 成功解决Docker Desktop requires Windows 10 Pro or Enterprise version 15063 to run.
  7. 成功解决ImportError: Could not find 'msvcp140.dll'. TensorFlow requires that this DLL be installed in a
  8. Graphviz之DT:手把手教你使用可视化工具Graphviz将dot文件转为结构图的png文件
  9. ML之GMM:Gaussian Mixture Model高斯混合模型相关论文、算法步骤相关配图
  10. [最短路]tvvj1031 热浪