当前位置:网站首页>C language makes it easy to add, delete, modify and check the linked list "suggested collection"
C language makes it easy to add, delete, modify and check the linked list "suggested collection"
2022-07-05 18:39:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
notes : Simple pursuit of code simplicity , So the writing may not be standard .
// Take it for the first time c Start writing data structures , Because of what I wrote , Pursue less code , And College ppt Not quite the same. . Please point out the mistakes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node// Define the node
{
int data;
struct node * next;
}Node;// Function introduction
void printlist(Node * head)// Print linked list
int lenlist(Node * head)// Return the length of the list
void insertlist(Node ** list,int data,int index)// Insert elements
void pushback(Node ** head,int data)// Tail insertion
void freelist(Node ** head)// Empty the list
void deletelist(Node ** list,int data)// Remove elements
Node * findnode(Node ** list,int data)// lookup
void change(Node ** list,int data,int temp)// Change the value void printlist(Node * head)// Print linked list
{
for(;head!=NULL;head=head->next) printf("%d ",head->data);
printf("\n");// Print for other functions , Last line break
}Chain length
int lenlist(Node * head)// Return the length of the list
{
int len;
Node * temp = head;
for(len=0; temp!=NULL; len++) temp=temp->next;
return len;
}Insert elements
void insertlist(Node ** list,int data,int index)// Insert elements , use *list take head Pointers and next Unified expression
{
if(index<0 || index>lenlist(*list))return;// Judge illegal input
Node * newnode=(Node *)malloc(sizeof(Node));// establish
newnode->data=data;
newnode->next=NULL;
while(index--)list=&((*list)->next);// Insert
newnode->next=*list;
*list=newnode;
}Add element to tail
void pushback(Node ** head,int data)// Tail insertion , ditto
{
Node * newnode=(Node *)malloc(sizeof(Node));// establish
newnode->data=data;
newnode->next=NULL;
while(*head!=NULL)head=&((*head)->next);// Insert
*head=newnode;
}Empty the list
void freelist(Node ** head)// Empty the list
{
Node * temp=*head;
Node * ttemp;
*head=NULL;// The pointer is set to null
while(temp!=NULL)// Release
{
ttemp=temp;
temp=temp->next;
free(ttemp);
}
}Delete
void deletelist(Node ** list,int data)// Delete the linked list node
{
Node * temp;// It's just convenient free
while((*list)->data!=data && (*list)->next!=NULL)list=&((*list)->next);
if((*list)->data==data){
temp=*list;
*list=(*list)->next;
free(temp);
}
}lookup
Node * findnode(Node ** list,int data)// lookup , Return the pointer to the node , If no return is empty
{
while((*list)->data!=data && (*list)!=NULL) list=&((*list)->next);
return *list;
}Change value
void change(Node ** list,int data,int temp)// change
{
while((*list)->data!=data && (*list)->next!=NULL)list=&((*list)->next);
if((*list)->data==data)(*list)->data=temp;
}Final test
int main(void)// test
{
Node * head=NULL;
Node ** gg=&head;
int i;
for(i=0;i<10;i++)pushback(gg,i);
printf(" The list elements are in turn : ");
printlist(head);
printf(" The length is %d\n",lenlist(head));
freelist(gg);
printf(" The length after release is %d\n",lenlist(head));
for(i=0;i<10;i++)pushback(gg,i);
deletelist(gg,0);// head
deletelist(gg,9);// tail
deletelist(gg,5);
deletelist(gg,100);// non-existent
printf(" Create the list again , After deleting a node \n");
printlist(head);
freelist(gg);
for(i=0;i<5;i++)pushback(gg,i);
insertlist(gg,5,0);// head
insertlist(gg,5,5);
insertlist(gg,5,7);// tail
insertlist(gg,5,10);// non-existent
printlist(head);
printf(" find %d\n hold 3 Turn into 100",*findnode(gg,5));
change(gg,3,100);
change(gg,11111,1);// non-existent
printlist(head);
}Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/149821.html Link to the original text :https://javaforall.cn
边栏推荐
- Crontab 日志:如何记录我的 Cron 脚本的输出
- Maximum artificial island [how to make all nodes of a connected component record the total number of nodes? + number the connected component]
- c期末复习
- Record a case of using WinDbg to analyze memory "leakage"
- 视频自监督学习综述
- 技术分享 | 接口测试价值与体系
- Memory management chapter of Kobayashi coding
- MYSQL中 find_in_set() 函数用法详解
- 【pm2详解】
- 关于服装ERP,你想知道的都在这里了
猜你喜欢

Memory leak of viewpager + recyclerview

The 11th China cloud computing standards and Applications Conference | China cloud data has become the deputy leader unit of the cloud migration special group of the cloud computing standards working

技术分享 | 常见接口协议解析

U-Net: Convolutional Networks for Biomedical Images Segmentation

第十届全球云计算大会 | 华云数据荣获“2013-2022十周年特别贡献奖”

ViewPager + RecyclerView的内存泄漏

《ClickHouse原理解析与应用实践》读书笔记(5)

进程间通信(IPC):共享内存

node_exporter内存使用率不显示

让更多港澳青年了解南沙特色文创产品!“南沙麒麟”正式亮相
随机推荐
爱因斯坦求和einsum
jdbc读大量数据导致内存溢出
一文读懂简单查询代价估算
第十一届中国云计算标准和应用大会 | 云计算国家标准及白皮书系列发布 华云数据全面参与编制
[paddlepaddle] paddedetection face recognition custom data set
About statistical power
Pytorch yolov5 training custom data
进程间通信(IPC):共享内存
Take a look at semaphore, the current limiting tool provided by JUC
瀚升优品app翰林优商系统开发功能介绍
《力扣刷题计划》复制带随机指针的链表
rust统计文件中单词出现的次数
Crontab 日志:如何记录我的 Cron 脚本的输出
Maximum artificial island [how to make all nodes of a connected component record the total number of nodes? + number the connected component]
sample_rate(采樣率),sample(采樣),duration(時長)是什麼關系
爬虫01-爬虫基本原理讲解
Case sharing | integrated construction of data operation and maintenance in the financial industry
瞅一瞅JUC提供的限流工具Semaphore
[QNX hypervisor 2.2 user manual]6.3.2 configuring VM
Nacos distributed transactions Seata * * install JDK on Linux, mysql5.7 start Nacos configure ideal call interface coordination (nanny level detail tutorial)