数据结构——链式结构
2021/10/25 6:12:20
本文主要是介绍数据结构——链式结构,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
数据结构——链式结构
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
- 数据结构——链式结构
- 一、创建空链表
- 二、单链表
- 1.节点的插入方法(头插法和尾插法)
- 2.单链表代码
- 三、单链表的反转
- 反转方法一:
- 总结
一、创建空链表
typedef struct Node { int data; struct Node* next; }Node; //创建空链表 Node* creatList() { Node* list = (Node*)malloc(sizeof(struct Node)); if (list == NULL) return NULL; list->next = NULL; return list; }
二、单链表
1.节点的插入方法(头插法和尾插法)
代码如下(示例):
//插入节点(头插法) void insertNode_head(Node** list,int data) { Node* newNode = creatNode(data); newNode->next = (*list)->next; (*list)->next = newNode; } //插入节点(尾插法) void insertNode_trail(Node** list, int data) { Node* newNode = creatNode(data); Node* pMove = (*list); while (pMove->next) { pMove = pMove->next; } pMove->next = newNode; }
2.单链表代码
代码如下(示例):
#include<stdio.h> #include<malloc.h> typedef struct Node { int data; struct Node* next; }Node; //创建空链表 Node* creatList() { Node* list = (Node*)malloc(sizeof(struct Node)); if (list == NULL) return NULL; list->next = NULL; return list; } //创建节点 Node* creatNode(int data) { Node* list = (Node*)malloc(sizeof(Node)); if (list == NULL) return NULL; list->data = data; list->next = NULL; return list; } //插入节点(头插法) void insertNode_head(Node** list,int data) { Node* newNode = creatNode(data); newNode->next = (*list)->next; (*list)->next = newNode; } //插入节点(尾插法) void insertNode_trail(Node** list, int data) { Node* newNode = creatNode(data); Node* pMove = (*list); while (pMove->next) { pMove = pMove->next; } pMove->next = newNode; } //打印链表 void printList(Node* list) { Node* pMove = list->next; while (pMove) { Node* q = pMove; printf("%d ", pMove->data); pMove = pMove->next; free(q);//释放 } } int main() { Node* list1 = creatList(); Node* list2 = creatList(); for (int i = 0; i <= 5; i++) { insertNode_head(&list1, i); insertNode_trail(&list2, i); } printList(list1); putchar('\n'); printList(list2); return 0; }
三、单链表的反转
反转方法一:
代码如下(示例):
在这里插入代码片
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。
3. 循环链表
这篇关于数据结构——链式结构的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-06小米11i印度快充版ROM合集:极致体验,超越期待
- 2024-10-06【ROM下载】小米11i 5G 印度版系统, 疾速跃迁,定义新速度
- 2024-10-06【ROM下载】小米 11 青春活力版,青春无极限,活力全开
- 2024-10-05小米13T Pro系统合集:性能与摄影的极致融合,值得你升级的系统ROM
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求