数据结构--单链表C/C++

2024-09-05 07:58
文章标签 c++ 数据结构 单链

本文主要是介绍数据结构--单链表C/C++,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最近在学习数据结构,其中有介绍单链表跟单循环链表的,现在复习一下。首先要定义一下数据结构(节点),如下:

typedef int DataType; //方便后面修改数据类型,有点像C++/JAVA中的泛型
typedef struct Node 
{DataType data;struct Node *next;
}Node;

单链表: 

接下来是定义一个获取链表某个位置节点的函数,如下:
 

Node* getptr(Node* head, int pos)
{Node *p = head;if (p == NULL || pos == 0){return head;}for (int i = 0; p && i < pos; i++){p = p->next;}return p;
}

获取链表有几个节点,如下:

int getSize(Node* head)
{int size = 0;Node *p = head;while(p){size++;p = p->next;}return size;
}

向链表中添加新的节点数据,如下:

bool insert(Node** head, int position, DataType d)
{if (position < 0 || position > getSize(*head)){return false;}Node *node = (Node*)malloc(sizeof(Node));node->data = d;node->next = NULL;if (position == 0){node->next = *head;*head = node;return true;}Node *p = getptr(*head, position - 1);Node *r = p->next;node->next = r;p->next = node;return true;
}

这里head使用了指针的指针,使用指针的指针是因为调用处需要带回head的改变,即实参需要改变;形参head执行完函数insert后是会被释放掉的。来张内存分配图:

 

 将两个链表拼接到一起,如下:

void unionList(Node *a, Node *b)
{Node *p = a;while(p->next)p = p->next;p->next = b;
}

将链表数据打印出来,如下:
 

void print(DataType d)
{printf("%d\t", d);
}void trave(Node *head, void(*fun)(DataType))
{Node *p = head;while(p){fun(p->data);p = p->next;}
}

 删除某个位置的链表节点,如下:

bool erase(Node **head, int pos)
{if (pos < 0 || pos >= getSize(*head)){return false;}Node *p = *head;if (pos == 0){*head = (*head)->next;free(p);p = NULL;return true;}p = getptr(*head, pos - 1);Node *q = p->next;p->next = q->next;free(q);q = NULL;return true;
}

将链表倒置,如下:

bool erase(Node **head, int pos)
{if (pos < 0 || pos >= getSize(*head)){return false;}Node *p = *head;if (pos == 0){*head = (*head)->next;free(p);p = NULL;return true;}p = getptr(*head, pos - 1);Node *q = p->next;p->next = q->next;free(q);q = NULL;return true;
}

 删除链表,如果节点中的data开辟了内存,也需要释放,这个没有开辟内存,所以不需要释放,如下:

void deleteAll(Node **head)
{Node *tmp, *n, *h;if (head == NULL)return;h = *head;for (tmp = h->next; tmp != NULL; tmp = n){n = tmp->next;free(tmp);		}free(h);*head = NULL;
}

 函数测试代码如下:

#include "SingleList.h"
#include <stdio.h>int main()
{Node *head = NULL;insert(&head, 0, 9);	insert(&head, 0, 8);insert(&head, 0, 3);insert(&head, 0, 11);printf("init------------>\n");trave(head, print);int len = getSize(head);printf("\n getSize = %d\n", len);bool b = erase(&head, 2);if (b){printf("erase--------->\n");trave(head, print);}printf("\n reverse------------->\n");reverse(&head);trave(head, print);printf("\n head2-------init-->\n");Node *head2 = NULL;insert(&head2, 0, 20);	insert(&head2, 0, 22);insert(&head2, 0, 14);insert(&head2, 0, 19);trave(head2, print);printf("\n unionList--------->\n");unionList(head, head2);trave(head, print);printf("\n deleteAll--------->\n");deleteAll(&head);trave(head, print);printf("\n----main end -----\n");return 0;
}

 编译运行结果如下:

 

 单循环链表:

获取链表长度,rear是尾指针,指向最后一个节点,如下:

int getSize(Node *rear)
{int size = 0;if (rear){Node *p = rear->next;while (p != rear){size++;p = p->next;}size++;}return size;
}

跟位置获取对应链表节点,如下:

Node *getptr(Node *rear, int pos)
{if (rear == NULL){return rear;}if (pos >= getSize(rear)){return NULL;	}Node *p = rear->next;for(int i = 0; i < pos; i++){p = p->next;}return p;
}

 向链表中添加节点,如下:
 

bool insert(Node **rear, int position, DataType d)
{if (position < 0 || position > getSize(*rear)){return false;}Node *node = (Node *)malloc(sizeof(Node));node->data = d;node->next = NULL;if (position == 0){if (*rear == NULL){node->next = node;*rear = node;}else{node->next = (*rear)->next;(*rear)->next = node;}return true;}	Node *p = getptr(*rear, position - 1);Node *r = p->next;node->next = r;p->next = node;if (*rear == p){*rear = node;	}return true;
}

删除链表中某个节点,如下:

 

bool erase(Node **rear, int pos)
{if (*rear == NULL || pos < 0 || pos >= getSize(*rear)){return false;}Node *p = (*rear)->next;if (pos == 0){(*rear)->next = p->next;free(p);p = NULL;return true;}p = getptr(*rear, pos - 1);Node *q = p->next;p->next = q->next;if (q == *rear){*rear = p;}free(q);q = NULL;return true;
}

 链表数据打印,如下:

void print(DataType d)
{printf("%d\t", d);
}void trave(Node *rear, void(*fun)(DataType))
{if (rear == NULL)return;Node *p = rear->next;while(p != rear){fun(p->data);p = p->next;}fun(p->data);
}

测试代码如下:

#include "cycleList.h"int main()
{Node *rear = NULL;insert(&rear, 0, 8);insert(&rear, 0, 3);insert(&rear, 0, 19);insert(&rear, 0, 6);printf("\n==============main init=============\n");trave(rear, print);printf("\n==============erase init=============\n");erase(&rear, 1);trave(rear, print);printf("\n==============main end=============\n");return 0;
}

测试结果如下:

 

代码参考:

单链表:https://github.com/gunder1129/android-tool/tree/master/dataStructure/singleList

单循环链表: https://github.com/gunder1129/android-tool/tree/master/dataStructure/cycleList

 

 

 

 

 

这篇关于数据结构--单链表C/C++的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1138384

相关文章

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

c++中的set容器介绍及操作大全

《c++中的set容器介绍及操作大全》:本文主要介绍c++中的set容器介绍及操作大全,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录​​一、核心特性​​️ ​​二、基本操作​​​​1. 初始化与赋值​​​​2. 增删查操作​​​​3. 遍历方

解析C++11 static_assert及与Boost库的关联从入门到精通

《解析C++11static_assert及与Boost库的关联从入门到精通》static_assert是C++中强大的编译时验证工具,它能够在编译阶段拦截不符合预期的类型或值,增强代码的健壮性,通... 目录一、背景知识:传统断言方法的局限性1.1 assert宏1.2 #error指令1.3 第三方解决

C++11委托构造函数和继承构造函数的实现

《C++11委托构造函数和继承构造函数的实现》C++引入了委托构造函数和继承构造函数这两个重要的特性,本文主要介绍了C++11委托构造函数和继承构造函数的实现,具有一定的参考价值,感兴趣的可以了解一下... 目录引言一、委托构造函数1.1 委托构造函数的定义与作用1.2 委托构造函数的语法1.3 委托构造函

C++11作用域枚举(Scoped Enums)的实现示例

《C++11作用域枚举(ScopedEnums)的实现示例》枚举类型是一种非常实用的工具,C++11标准引入了作用域枚举,也称为强类型枚举,本文主要介绍了C++11作用域枚举(ScopedEnums... 目录一、引言二、传统枚举类型的局限性2.1 命名空间污染2.2 整型提升问题2.3 类型转换问题三、C

C++链表的虚拟头节点实现细节及注意事项

《C++链表的虚拟头节点实现细节及注意事项》虚拟头节点是链表操作中极为实用的设计技巧,它通过在链表真实头部前添加一个特殊节点,有效简化边界条件处理,:本文主要介绍C++链表的虚拟头节点实现细节及注... 目录C++链表虚拟头节点(Dummy Head)一、虚拟头节点的本质与核心作用1. 定义2. 核心价值二

C++ 检测文件大小和文件传输的方法示例详解

《C++检测文件大小和文件传输的方法示例详解》文章介绍了在C/C++中获取文件大小的三种方法,推荐使用stat()函数,并详细说明了如何设计一次性发送压缩包的结构体及传输流程,包含CRC校验和自动解... 目录检测文件的大小✅ 方法一:使用 stat() 函数(推荐)✅ 用法示例:✅ 方法二:使用 fsee

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

C++中RAII资源获取即初始化

《C++中RAII资源获取即初始化》RAII通过构造/析构自动管理资源生命周期,确保安全释放,本文就来介绍一下C++中的RAII技术及其应用,具有一定的参考价值,感兴趣的可以了解一下... 目录一、核心原理与机制二、标准库中的RAII实现三、自定义RAII类设计原则四、常见应用场景1. 内存管理2. 文件操