当前位置:网站首页>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
边栏推荐
- The origin of PTS, DTS and duration of audio and video packages
- English sentence pattern reference
- 7-2 keep the linked list in order
- 视频自监督学习综述
- 彻底理解为什么网络 I/O 会被阻塞?
- Personal understanding of convolutional neural network
- ConvMAE(2022-05)
- 7-2 保持链表有序
- Use of websocket tool
- LeetCode 6109. Number of people who know the secret
猜你喜欢
About Estimation with Cross-Validation
Introduction to the development function of Hanlin Youshang system of Hansheng Youpin app
ICML2022 | 长尾识别中分布外检测的部分和非对称对比学习
爬虫01-爬虫基本原理讲解
分享:中兴 远航 30 pro root 解锁BL magisk ZTE 7532N 8040N 9041N 刷机 刷面具原厂刷机包 root方法下载
FCN: Fully Convolutional Networks for Semantic Segmentation
@Extension, @spi annotation principle
node_exporter内存使用率不显示
Use of websocket tool
Reptile 01 basic principles of reptile
随机推荐
Problems encountered in the project u-parse component rendering problems
视频自监督学习综述
Nacos distributed transactions Seata * * install JDK on Linux, mysql5.7 start Nacos configure ideal call interface coordination (nanny level detail tutorial)
c期末复习
ConvMAE(2022-05)
How to choose the most formal and safe external futures platform?
[paddlepaddle] paddedetection face recognition custom data set
vulnhub之darkhole_2
New words new words new words new words [2]
使用JMeter录制脚本并调试
生词生词生词生词[2]
7-1 链表也简单fina
分享:中兴 远航 30 pro root 解锁BL magisk ZTE 7532N 8040N 9041N 刷机 刷面具原厂刷机包 root方法下载
Lombok @builder annotation
小程序 修改样式 ( placeholder、checkbox的样式)
如何写出好代码 - 防御式编程
Multithreading (I) processes and threads
Exemple Quelle est la relation entre le taux d'échantillonnage, l'échantillon et la durée?
How to obtain the coordinates of the aircraft passing through both ends of the radar
AI金榜题名时,MLPerf榜单的份量究竟有多重?