当前位置:网站首页>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
边栏推荐
- Problems encountered in the project u-parse component rendering problems
- 第十届全球云计算大会 | 华云数据荣获“2013-2022十周年特别贡献奖”
- 记录Pytorch中的eval()和no_grad()
- 【在优麒麟上使用Electron开发桌面应】
- 7-1 linked list is also simple fina
- 技术分享 | 常见接口协议解析
- 小程序 修改样式 ( placeholder、checkbox的样式)
- Vulnhub's darkhole_ two
- Writing writing writing
- Cronab log: how to record the output of my cron script
猜你喜欢

Introduction to the development function of Hanlin Youshang system of Hansheng Youpin app

Failed to virtualize table with JMeter

使用JMeter录制脚本并调试

Solutions contents have differences only in line separators

如何写出好代码 - 防御式编程

AI Open2022|基于异质信息网络的推荐系统综述:概念,方法,应用与资源

FCN: Fully Convolutional Networks for Semantic Segmentation

解决 contents have differences only in line separators

Simulate the hundred prisoner problem

AI金榜题名时,MLPerf榜单的份量究竟有多重?
随机推荐
Lombok @builder annotation
RPC协议详解
@Extension, @spi annotation principle
7-2 保持链表有序
How to choose the most formal and safe external futures platform?
[PM2 details]
【在优麒麟上使用Electron开发桌面应】
LeetCode 6111. 螺旋矩阵 IV
How to write good code defensive programming
Memory management chapter of Kobayashi coding
ICML2022 | 长尾识别中分布外检测的部分和非对称对比学习
vulnhub之darkhole_2
[QNX Hypervisor 2.2用户手册]6.3.2 配置VM
AI金榜题名时,MLPerf榜单的份量究竟有多重?
7-1 链表也简单fina
Copy the linked list with random pointer in the "Li Kou brush question plan"
U-Net: Convolutional Networks for Biomedical Images Segmentation
Pytorch yolov5 training custom data
node_exporter内存使用率不显示
ViewPager + RecyclerView的内存泄漏