当前位置:网站首页>c语言简便实现链表增删改查「建议收藏」
c语言简便实现链表增删改查「建议收藏」
2022-07-05 18:33:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
注:单追求代码简洁,所以写法可能有点不标准。
//第一次拿c开始写数据结构,因为自己写的,追求代码量少,和学院ppt不太一样。有错请指出
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node//定义节点
{
int data;
struct node * next;
}Node;
//函数介绍
void printlist(Node * head)//打印链表
int lenlist(Node * head)//返回链表长度
void insertlist(Node ** list,int data,int index)//插入元素
void pushback(Node ** head,int data)//尾部插入
void freelist(Node ** head)//清空链表
void deletelist(Node ** list,int data)//删除元素
Node * findnode(Node ** list,int data)//查找
void change(Node ** list,int data,int temp)//改变值
打印
void printlist(Node * head)//打印链表
{
for(;head!=NULL;head=head->next) printf("%d ",head->data);
printf("\n");//为了其他函数打印,最后换行
}
链表长度
int lenlist(Node * head)//返回链表长度
{
int len;
Node * temp = head;
for(len=0; temp!=NULL; len++) temp=temp->next;
return len;
}
插入元素
void insertlist(Node ** list,int data,int index)//插入元素,用*list将head指针和next统一表示
{
if(index<0 || index>lenlist(*list))return;//判断非法输入
Node * newnode=(Node *)malloc(sizeof(Node));//创建
newnode->data=data;
newnode->next=NULL;
while(index--)list=&((*list)->next);//插入
newnode->next=*list;
*list=newnode;
}
尾部增加元素
void pushback(Node ** head,int data)//尾插,同上
{
Node * newnode=(Node *)malloc(sizeof(Node));//创建
newnode->data=data;
newnode->next=NULL;
while(*head!=NULL)head=&((*head)->next);//插入
*head=newnode;
}
清空链表
void freelist(Node ** head)//清空链表
{
Node * temp=*head;
Node * ttemp;
*head=NULL;//指针设为空
while(temp!=NULL)//释放
{
ttemp=temp;
temp=temp->next;
free(ttemp);
}
}
删除
void deletelist(Node ** list,int data)//删除链表节点
{
Node * temp;//作用只是方便free
while((*list)->data!=data && (*list)->next!=NULL)list=&((*list)->next);
if((*list)->data==data){
temp=*list;
*list=(*list)->next;
free(temp);
}
}
查找
Node * findnode(Node ** list,int data)//查找,返回指向节点的指针,若无返回空
{
while((*list)->data!=data && (*list)!=NULL) list=&((*list)->next);
return *list;
}
改值
void change(Node ** list,int data,int temp)//改变
{
while((*list)->data!=data && (*list)->next!=NULL)list=&((*list)->next);
if((*list)->data==data)(*list)->data=temp;
}
最后测试
int main(void)//测试
{
Node * head=NULL;
Node ** gg=&head;
int i;
for(i=0;i<10;i++)pushback(gg,i);
printf("链表元素依次为: ");
printlist(head);
printf("长度为%d\n",lenlist(head));
freelist(gg);
printf("释放后长度为%d\n",lenlist(head));
for(i=0;i<10;i++)pushback(gg,i);
deletelist(gg,0);//头
deletelist(gg,9);//尾
deletelist(gg,5);
deletelist(gg,100);//不存在
printf("再次创建链表,删除节点后\n");
printlist(head);
freelist(gg);
for(i=0;i<5;i++)pushback(gg,i);
insertlist(gg,5,0);//头
insertlist(gg,5,5);
insertlist(gg,5,7);//尾
insertlist(gg,5,10);//不存在
printlist(head);
printf("找到%d\n把3变为100",*findnode(gg,5));
change(gg,3,100);
change(gg,11111,1);//不存在
printlist(head);
}
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/149821.html原文链接:https://javaforall.cn
边栏推荐
- 关于服装ERP,你想知道的都在这里了
- 苹果手机炒股安全吗?打新债是骗局吗?
- Let more young people from Hong Kong and Macao know about Nansha's characteristic cultural and creative products! "Nansha kylin" officially appeared
- IDEA配置npm启动
- Problems encountered in the project u-parse component rendering problems
- 《力扣刷题计划》复制带随机指针的链表
- SAP 特征 特性 说明
- rust统计文件中单词出现的次数
- ConvMAE(2022-05)
- Notes on common management commands of openshift
猜你喜欢
websocket 工具的使用
分享:中兴 远航 30 pro root 解锁BL magisk ZTE 7532N 8040N 9041N 刷机 刷面具原厂刷机包 root方法下载
吳恩達團隊2022機器學習課程,來啦
Whether to take a duplicate subset with duplicate elements [how to take a subset? How to remove duplicates?]
buuctf-pwn write-ups (9)
技术分享 | 接口测试价值与体系
The 2022 China Xinchuang Ecological Market Research and model selection evaluation report released that Huayun data was selected as the mainstream manufacturer of Xinchuang IT infrastructure!
使用JMeter录制脚本并调试
Reptile 01 basic principles of reptile
【HCIA-cloud】【1】云计算的定义、什么是云计算、云计算的架构与技术说明、华为云计算产品、华为内存DDR配置工具说明
随机推荐
Pytorch yolov5 training custom data
Clickhouse (03) how to install and deploy Clickhouse
Notes on common management commands of openshift
buuctf-pwn write-ups (9)
瀚升优品app翰林优商系统开发功能介绍
rust统计文件中单词出现的次数
Memory leak of viewpager + recyclerview
Memory management chapter of Kobayashi coding
快速生成ipa包
MYSQL中 find_in_set() 函数用法详解
Use QT to traverse JSON documents and search sub objects
[QNX Hypervisor 2.2用户手册]6.3.2 配置VM
让更多港澳青年了解南沙特色文创产品!“南沙麒麟”正式亮相
JDBC reads a large amount of data, resulting in memory overflow
写作写作写作写作
Insufficient picture data? I made a free image enhancement software
【HCIA-cloud】【1】云计算的定义、什么是云计算、云计算的架构与技术说明、华为云计算产品、华为内存DDR配置工具说明
[utiliser Electron pour développer le Bureau sur youkirin devrait]
Simulate the hundred prisoner problem
jdbc读大量数据导致内存溢出