当前位置:网站首页>[basics of C language] 17 preliminary study of linked list
[basics of C language] 17 preliminary study of linked list
2022-07-26 22:17:00 【One reed to sail FP】
One 、 Linked list (linked list)
1.1 Create header file
node —— node
//node.h
#ifndef _NODE_H_
#define _NODE_H_
typedef struct _node{
int value; // Value of this cell
struct _node * next; // The first address of the next unit
} Node;
#endif
1.2 Program examples : Create a linked list
#include <stdio.h>
#include <stdlib.h>
#include "node.h"
typedef struct _list{
// Store the head and tail of the linked list
Node* head;
Node* tail;
} List;
void add( Node* head, int number);
int main(){
List list;
list.head = list.tail = NULL;
int number;
while( (scanf("%d", &number)) != EOF ){
add(&list, number);
}
return 0;
}
Two 、 Functions of linked lists
// The parameters are the head and tail of the original linked list , And the value to be stored
void add( List* pList, int number)
{
// Create a new cell
Node* p = (Node*)malloc(sizeof(Node));
p->value = number;
p->next = NULL; // The pointer member of the last structural unit is null
// Judge pList Whether it points to NULL
if(pList){
pList->tail = p;
}
else{
pList->head = p;
}
}
3、 ... and 、 Search of linked list
3.1 Traversal of the list
// Traversal of the list
Node *p;
for(p = list.head; p; p = p->next){
printf("%d\t", p->value);
}
printf("\n");
// Linked list traversal function
void print(List* pList){
Node *p;
for(p = pList->head; p; p = p->next){
printf("%d\t", p->value);
}
printf("\n");
}
3.2 Search for linked list elements
// Search whether there is a certain number in the linked list
int number, isFound = 0;
scanf("%d", &number);
Node *p;
for( p=list.head; p; p = p->next){
if(p->value == number){
printf(" Call me !\n");
isFound = 1;
break;
}
}
if(!isFound) printf(" Did not find .\n");
// Function form
void search(List* pList, int number)
{
int isFound = 0;
Node *p;
for( p=pList->head; p; p = p->next){
if(p->value == number){
printf(" Call me !\n");
isFound = 1;
break;
}
}
if(!isFound) printf(" Did not find .\n");
}
Four 、 Deletion of linked list units
// Delete a cell in the linked list
void delete(List* pList, int number)
{
Node *p, *q; // Record the unit p, and p Last unit of
for(q=NULL, p=pList->head; p; q=p, p=p->next){
// Link two adjacent cells of the target cell ( Skip target unit )
if(p->value == number){
if( q ){
q->next = p->next;
}
else{
pList->head = p->next;
}
free(p); // Free the memory space of the target unit
break
}
}
}
When using a pointer to a linked list unit , You need to consider whether the pointer is a null pointer .
5、 ... and 、 Clearing the linked list
void flush(List* pList)
{
Node*p, *q;
for(p=pList->head; p; p=q){
q = p->next;
free(p);
}
}
边栏推荐
- JS verify complex password
- 现货黄金操作指南与建议(上)
- 七月集训(第26天) —— 并查集
- Task04 | classification analysis
- ZABBIX calls API retrieval method
- 07 df 命令
- Method overloading and method rewriting
- Operating guidelines and suggestions for spot gold (Part 1)
- Schematic diagram of MOS tube
- Circular progress bar animation based on cashapelayer and Bezier curve
猜你喜欢

yolov1

想让照片中的云飘起来?视频编辑服务一键动效3步就能实现

Join method in JS

开发转测试:从零开始的6年自动化之路

My SQL is OK. Why is it still so slow? MySQL locking rules

光源控制器拨码开关使用说明

xshell7个人免费下载,使用

Just one dependency to give swagger a new skin, which is simple and cool~

Implementation of MATLAB short-time autocorrelation

JDBC operation and entry case of MySQL
随机推荐
Unity对资源管理器操作 打开资源管理器选择文件并筛选文件
day07-
想让照片中的云飘起来?视频编辑服务一键动效3步就能实现
开发转测试:从零开始的6年自动化之路
: could not determine a constructor for the tag !RootAdmin
SQL注入 Less24(二次注入)
Difference between D and C
[waiting and wakeup of QT multithreaded threads]
jmeter -- response中文乱码处理
Unity installation failed: operation not allowed, MKDIR
yolov1
梦里的一碗面
Knowledge base tools | wechat, document center, image display page can be generated by dragging (with template, directly used)
Pytoch squeeze() unsqueeze() usage
什么是 Base64 ?
Go --- identifiers and keywords in go language
unity 获取网络时间
Go----Go语言中的变量使用方法
学了那么久的用例设计,知不知道为什么设计测试用例
Determine the dimension of numpy array array