当前位置: 首页 > news >正文

数据结构 2.2 单循环链表

2.单循环链表
data|next——>data|next——>data|next——>头节点
1.初始化链表
2.增加节点(头插法、尾插法)
3.删除节点
4.遍历链表

定义一个结构体,存放data域和指针域:

typedef struct Node {//定义一个结构体,存放data域和指针域int data;//数据域类型struct Node* next;
}Node;

初始化链表:

Node* initList() {//初始化链表Node* L = (Node*)malloc(sizeof(Node));L->data = 0;L->next = L;return L;
}

头插法:

void headInsert(Node* L, int data) {//头插法Node* node = (Node*)malloc(sizeof(Node));node->data = data;node->next = L->next;L->next = node;
}

尾插法 :

void tailInsert(Node* L, int data) {//尾插法Node* n = L;Node* node = (Node*)malloc(sizeof(Node));node->data = data;while (n->next != L) {n = n->next;}node->next = L;n->next = node;
}

删除:

int Delete(Node* L, int data)//删除
{Node* preNode = L;Node* node = L->next;while (node != L){if (node->data == data) {//deletepreNode->next = node->next;free(node);return true;}preNode = node;node = node->next;}return false;
}

遍历链表:

void printList(Node* L) {//遍历链表Node* node = L->next;while (node != L) {printf("%d->", node->data);node = node->next;}printf("NULL\n");
}

main函数:

int main()
{Node* L = initList();headInsert(L, 1);headInsert(L, 2);headInsert(L, 3);headInsert(L, 4);headInsert(L, 5);tailInsert(L, 6);tailInsert(L, 7);tailInsert(L, 8);tailInsert(L, 9);tailInsert(L, 10);printList(L);Delete(L, 4);Delete(L, 5);printList(L);return 0;
}

单循环链表函数

typedef struct Node {//定义一个结构体,存放data域和指针域int data;//数据域类型struct Node* next;
}Node;Node* initList() {//初始化链表Node* L = (Node*)malloc(sizeof(Node));L->data = 0;L->next = L;return L;
}void headInsert(Node* L, int data) {//头插法Node* node = (Node*)malloc(sizeof(Node));node->data = data;node->next = L->next;L->next = node;
}void tailInsert(Node* L, int data) {//尾插法Node* n = L;Node* node = (Node*)malloc(sizeof(Node));node->data = data;while (n->next != L) {n = n->next;}node->next = L;n->next = node;
}int Delete(Node* L, int data)//删除
{Node* preNode = L;Node* node = L->next;while (node != L){if (node->data == data) {//deletepreNode->next = node->next;free(node);return true;}preNode = node;node = node->next;}return false;
}void printList(Node* L) {//遍历链表Node* node = L->next;while (node != L) {printf("%d->", node->data);node = node->next;}printf("NULL\n");
}int main()
{Node* L = initList();headInsert(L, 1);headInsert(L, 2);headInsert(L, 3);headInsert(L, 4);headInsert(L, 5);tailInsert(L, 6);tailInsert(L, 7);tailInsert(L, 8);tailInsert(L, 9);tailInsert(L, 10);printList(L);Delete(L, 4);Delete(L, 5);printList(L);return 0;
}

http://www.lryc.cn/news/183953.html

相关文章:

  • 矩阵距离——多源BFS
  • 关于在 Notion 中使用 Markdown 语法
  • sigmoid和softmax函数有什么区别
  • 第五章:最新版零基础学习 PYTHON 教程—Python 字符串操作指南(第七节 - Python 中使用 % 进行字符串格式化)
  • 【网络安全 --- 工具安装】VMware 16.0 详细安装过程(提供资源)
  • Eclipse MAT解析headp dump,total size小于file size
  • 【数据挖掘】2022年 Quiz 1-3 整理 带答案
  • AcWing 288. 休息时间,《算法竞赛进阶指南》,环形与后效性处理
  • 一文掌握Linux系统信息查看命令(CPU、内存、进程、网口、磁盘、硬件)
  • UE5.1编辑器拓展【三、脚本化资产行为,删除无引用资产】
  • 防抖和节流的实现
  • alsa pcm接口之阻塞和非阻塞打开和异步通知模式
  • Python Random模块详解
  • Vue3 模糊搜索筛选
  • 【MVC】C# MVC基础知识点、原理以及容器和管道
  • 【kubernetes】基于prometheus的监控
  • Gmail 将停止支持基本 HTML 视图
  • 电影大师杂记
  • 聊聊分布式架构——RPC通信原理
  • Android:实现手机前后摄像头预览同开
  • 2.2.4 yocto poky openembedded bitbake关系
  • 开源后台管理系统 (go-vue-admin)
  • 想升级macOS Big Sur,但是MacBook内存空间不够该怎么办?
  • 结构化面试 --- 介绍 + 人际关系
  • 李沐深度学习记录5:13.Dropout
  • 计算机竞赛 题目:基于大数据的用户画像分析系统 数据分析 开题
  • MFC ExtTextOut函数学习
  • Java中阻塞队列原理、特点、适用场景
  • PHP之linux、apache和nginx与安全优化面试题
  • 算法笔记:0-1背包问题