单链表的实现:增删改查
1 /* 2 *@author ? 3 *@since 2011/07/29 4 *@function 实现链表的常用操作,比较粗糙。 5 * 功能大概是:显示链表结构;查询链表;删除节点;插入结点;修改结点 6 */ 7 #include <stdio.h> 8 #include <stdlib.h> 9 10 struct node 11 { 12 int value; 13 struct node *next; 14 }; 15 /* 16 *插入结点(单个) 17 */ 18 struct node *add_to_list(struct node *list,int n) 19 { 20 struct node *new_node; 21 22 new_node=malloc(sizeof(struct node)); 23 if(new_node==NULL) 24 { 25 printf("Error:malloc failed in add_to_list\n"); 26 exit(EXIT_FAILURE); 27 } 28 new_node->value=n; 29 new_node->next=list; 30 31 return new_node; 32 } 33 /* 34 *添加结点(多个) 35 */ 36 struct node *read_numbers(void) 37 { 38 struct node *first=NULL; 39 int n; 40 41 printf("Enter a series of integers (0 to terminate): "); 42 for(;;) 43 { 44 scanf("%d",&n); 45 if(n==0) 46 { 47 return first; 48 } 49 first=add_to_list(first,n); 50 } 51 } 52 /* 53 *搜索结点 54 */ 55 struct node *search_list(struct node *list,int n) 56 { 57 struct node *p; 58 59 for(p=list;p!=NULL;p=p->next) 60 if(p->value==n) 61 return p; 62 63 return NULL; 64 } 65 /* 66 *删除结点 67 */ 68 struct node *delete_node(struct node *list,int n) 69 { 70 struct node *cur,*prev; 71 72 for(cur=list,prev=NULL; 73 cur!=NULL&&cur->value!=n; 74 prev=cur,cur=cur->next) 75 ; 76 if(cur==NULL) 77 { 78 return list; 79 } 80 if(prev==NULL) 81 { 82 list=list->next; 83 }else 84 { 85 prev->next=cur->next; 86 } 87 free(cur); 88 89 return list; 90 } 91 /* 92 *修改结点 93 */ 94 struct node *update_node(struct node *list,int n) 95 { 96 struct node *cur; 97 int new_value; 98 99 for(cur=list;cur!=NULL&&cur->value!=n;cur=cur->next)100 ;101 if(cur==NULL)102 {103 printf("NO NODE!!!");104 }105 printf("please enter the new value: ");106 scanf("%d",&new_value);107 cur->value=new_value;108 109 return list;110 }111 /*112 *打印链表结点结构113 */114 void print(struct node *list)115 {116 struct node *p;117 118 printf("The current relation of the list is ");119 120 for(p=list;p!=NULL;p=p->next)121 {122 if(p->next!=NULL)123 {124 printf("%d->",p->value);125 }else126 {127 printf("%d\n",p->value);128 }129 }130 }131 132 int main()133 {134 struct node *test,*p;135 int n; 136 137 test=read_numbers();138 139 /*140 *输出当前链表141 */142 print(test);143 144 /*145 *查找链表中结点,并输出结点的值146 */147 printf("Please enter the value to look for: ");148 scanf("%d",&n);149 p=search_list(test,n);150 printf("The value of the node is %d\n",p->value);151 152 /*153 *删除结点并显示更新后链表的结构154 */155 printf("Please choose the value of the node you would like to delete: ");156 scanf("%d",&n);157 p=delete_node(test,n);158 print(p);159 160 /*161 *修改结点并显示更新后链表的结构162 */163 printf("Please choose the value of the node you would like to update: ");164 scanf("%d",&n);165 p=update_node(test,n);166 print(p);167 168 return 0;169 }
结果如下:
转载于:https://www.cnblogs.com/zengge/archive/2011/07/29/2121330.html
单链表的实现:增删改查相关推荐
- 单链表LinkedList的增删改查
数组作为数据存储结构有一定的缺陷.在无序数组中,搜索性能差,在有序数组中,插入效率又很低(插入位置后面的元素需要集体后移),而且这两种数组的删除效率(集体前移)都很低,并且数组在创建后,其大小是固定了 ...
- 详细记录->使用Maven+SSM框架,实现单表简单的增删改查
话不多说,ssm框架整合小栗子 步骤 1.创建web Maven项目 2.创建数据库配置文件:jdbc.properties 3.项目总体目录: 4.添加spring配置文件:applicationC ...
- C语言:构建一个二级链表并完成增删改查
构建一个下图所示的链表,并完成增.删.改.查 示例代码: #include <stdio.h> #include <stdlib.h> #include <string. ...
- c语言实现双链表的基本操作—增删改查
//初始化 Node*InitList() {Node*head=(Node*)malloc(sizeof(Node));if(NULL==head){printf("内存分配失败!&quo ...
- Django---ORM简介丶单表操作丶增删改查
一丶ORM简介 MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人 ...
- day 68 增删改查 语法
1 普通正则 2 分组正则 url(r'/blog/(\d+)/(\d+)',views.blog) blog(request,arq1,arq2) 按照位置传参 3 分组命名 url(r'/ ...
- SpringMVC表单数据增删改查简易梳理(含实例代码)
使用SpringMVC创建表单进行数据的增删改查是javaEE开发的基本功,本人根据自己最近开发的基于jeecms框架的网站平台来梳理数据增删改查的思路. 首先根据所需表单页面设计数据库,定义不同字段 ...
- 带头节点单链表的增删改查
单链表有很多结构循环单链表,有头节点的单链表,无头节点的单链表,双节点单链表,以下源码是以有一个头节点的单链表为例写的增删改查的各种功能,就是下图 然后各个注释也在函数后面写着,这玩意确实还挺难,源码 ...
- 数据结构----单链表增删改查
单链表的增删改查 一.链表(Linked List) 链表是有序列表,以节点的方式来存储的,链式存储: 每个节点包含data域,next域:指向下一节点: 链表的各个节点不一定是连续存储: 链表分为带 ...
- 三、单链表增删改查原理和代码实现
单链表 1.简单介绍 (1)单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素.链表中的数据是以结点来表示的,每个结点的构成:元素(数据元素的映象) + 指针(指示后继元素 ...
最新文章
- ●Joyoi Dotp 驱逐猪猡
- 【学习笔记】6、标准数据类型—数字类型
- Java Learning Path(四) 方法篇
- Caffe学习:Loss
- Keli Linux与网络安全(1)——在VMWare中安装Keli系统
- linux nfc驱动程序,USB NFC读卡器ACR122 Linux程序编译
- Eucalyptus的结构
- 提升办公效率——工具篇
- html 抓取 post 请求,自动向网页Post信息并提取返回的信息(一)
- 台架控制器-AVL_ISAC学习
- 微信指纹支付提示java6_苹果6微信指纹支付每次提示请验证已有的指纹用于支付,怎样才能支付?...
- python爬虫采集财经数据
- 生活片段(1)-短暂的深圳实习时光
- 4g全网通SMD贴片内置天线怎么选择?
- 服务器之间如何传输数据
- 人教版三年级计算机教学计划,2017人教版三年级信息技术教学计划范文
- 中国大数据分析行业研究报告
- 打开桌面计算机投屏到扩展屏,电脑扩展显示器投屏方案
- es如何提升写入性能
- 操作系统第五章笔记---线程
热门文章
- (JAVA学习笔记) 异常处理
- ubuntu20.0.4如何更新软件源?快速更新阿里源步骤具体实现
- 使用netty搭建一个简单的聊天室
- mysql逻辑删除的问题_数据库表涉及-逻辑删除
- 【技术】交换机上如何对流量拦截
- Service Mesh 在中国工商银行的探索与实践
- 说出你和「云原生」的故事,获得年度云原生顶级盛会通行证
- 中小企业如何实现在家研发软件?看这个就够了
- Python中的线程、进程、协程以及区别
- linux中mysql回滚重演_DM7 达梦 数据库 数据守护(Data Watch) (1) -- 基本概念