当前位置:网站首页>【学习笔记】链表操作
【学习笔记】链表操作
2022-07-28 05:25:00 【踏流星】
#include <stdio.h>
#include <stdlib.h>
struct list_node{
int data;
struct list_node *next;
};
//创建链表
int list_init(struct list_node *link_list)
{
link_list = (struct list_node*)malloc(sizeof(struct list_node));
link_list->data = 0;
link_list->next = NULL;
return 0;
}
//链表在给定的节点后面添加节点
struct list_node *list_insert(struct list_node *link_list, int data, int n)
{
int i;
//printf("data = %d\n", data);
//printf("n = %d\n", n);
if (link_list == NULL) {
return NULL;
}
//添加头节点
if (n == 0) {
struct list_node *link_head = (struct list_node *)malloc(sizeof(struct list_node));
link_head->data = data;
link_head->next = link_list;
//link_list = link_head;
return link_head;
}
//添加尾结点
struct list_node *link_tmp = link_list;
int count = 1;
for (i = 0; i < n; i++) {
link_tmp = link_tmp->next;
}
//添加尾结点
struct list_node *link_tail_tmp = link_tmp->next;
if (link_tail_tmp->next == NULL) {
struct list_node *link_tail = (struct list_node *)malloc(sizeof(struct list_node));
link_tail->data = data;
link_tail->next = NULL;
link_tail_tmp->next = link_tail;
return link_list;
}
//添加中间节点
struct list_node *link_middle = (struct list_node *)malloc(sizeof(struct list_node));
link_middle->data = data;
link_middle->next = link_tail_tmp;
link_tmp->next = link_middle;
return link_list;
}
//删除指定节点
int link_delete(struct list_node *link_list, int n)
{
int i;
int count = 0;
//删除头节点
if (n == 0) {
link_list = link_list->next;
return 0;
}
struct list_node *link_tmp = link_list;
for (i = 0; i < n - 1; i++) {
link_tmp = link_tmp->next;
}
// 删除尾节点
struct list_node *link_tail = link_tmp->next;
if (link_tail->next == NULL) {
link_tmp->next = NULL;
return 0;
}
//删除中间节点
struct list_node *link_middle = link_tail->next;
link_tmp->next = link_middle;
return 0;
}
int link_show(struct list_node *link_list)
{
while (link_list->next != NULL) {
printf("%d\t", link_list->data);
link_list = link_list->next;
}
printf("%d\n", link_list->data);
return 0;
}
边栏推荐
- PT 基于Multi Voltage的Physical Aware
- 雷达成像 Matlab 仿真 4 —— 距离分辨率分析
- VAN(DWConv+DWDilationConv+PWConv)
- Efficient Net_ V2
- EMC experiment practical case ESD electrostatic experiment
- Transformer self attention mechanism and complete code implementation
- 机器学习笔记 5 —— Logistic Regression
- Surge impact immunity experiment (surge) -emc series Hardware Design Notes 6
- ConNeXt
- 一、ffmpeg录制音频为pcm文件
猜你喜欢

qt实现将相关信息输出到日志文件

Weight decay

Bert bidirectional encoder based on transformer

Mae mask self encoding is scalable learning

Cautious speculation about fusion on Apple silicon

雷达成像 Matlab 仿真 1 —— LFM信号及其频谱

Redhawk Dynamic Analysis

Vs code basic configuration and beautification

qt中Qthread线程的使用以及安全关闭

mysql join技巧
随机推荐
Measure computer battery capacity
Matlab 信号处理
Talk about the "hybrid mode" of esxi virtual switch and port group
set_ clock_ groups
Trouble encountered in cable testing -- a case study of a manufacturer?
A NOVEL DEEP PARALLEL TIME-SERIES RELATION NETWORK FOR FAULT DIAGNOSIS
PyTorch 学习笔记 4 —— 自动计算梯度下降 AUTOGRAD
Pycharm2019设置编辑器主题和默认代码
低功耗设计-Power Switch
机器学习笔记 5 —— Logistic Regression
Pytorch learning notes 2 - about tensor
【服务器使用记录】通过跳板机登录远程服务器并进行文件传输
Low power design -power switch
USB network native driver for esxi updated to support esxi7.0.1
RS232 RS485 RS422 communication learning and notes
Pytorch learning notes 3 - datasets & dataloaders & transforms
自定义组件--父子组件之间的通信
测量电脑电池容量
OpenGL的开发环境配置【VS2017】+常见问题
毕业论文 | 文献综述应该怎么写