数据结构--单链表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++右移运算符的一个小坑及解决

《C++右移运算符的一个小坑及解决》文章指出右移运算符处理负数时左侧补1导致死循环,与除法行为不同,强调需注意补码机制以正确统计二进制1的个数... 目录我遇到了这么一个www.chinasem.cn函数由此可以看到也很好理解总结我遇到了这么一个函数template<typename T>unsigned

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

深入解析C++ 中std::map内存管理

《深入解析C++中std::map内存管理》文章详解C++std::map内存管理,指出clear()仅删除元素可能不释放底层内存,建议用swap()与空map交换以彻底释放,针对指针类型需手动de... 目录1️、基本清空std::map2️、使用 swap 彻底释放内存3️、map 中存储指针类型的对象

C++ STL-string类底层实现过程

《C++STL-string类底层实现过程》本文实现了一个简易的string类,涵盖动态数组存储、深拷贝机制、迭代器支持、容量调整、字符串修改、运算符重载等功能,模拟标准string核心特性,重点强... 目录实现框架一、默认成员函数1.默认构造函数2.构造函数3.拷贝构造函数(重点)4.赋值运算符重载函数

C++ vector越界问题的完整解决方案

《C++vector越界问题的完整解决方案》在C++开发中,std::vector作为最常用的动态数组容器,其便捷性与性能优势使其成为处理可变长度数据的首选,然而,数组越界访问始终是威胁程序稳定性的... 目录引言一、vector越界的底层原理与危害1.1 越界访问的本质原因1.2 越界访问的实际危害二、基

redis数据结构之String详解

《redis数据结构之String详解》Redis以String为基础类型,因C字符串效率低、非二进制安全等问题,采用SDS动态字符串实现高效存储,通过RedisObject封装,支持多种编码方式(如... 目录一、为什么Redis选String作为基础类型?二、SDS底层数据结构三、RedisObject

c++日志库log4cplus快速入门小结

《c++日志库log4cplus快速入门小结》文章浏览阅读1.1w次,点赞9次,收藏44次。本文介绍Log4cplus,一种适用于C++的线程安全日志记录API,提供灵活的日志管理和配置控制。文章涵盖... 目录简介日志等级配置文件使用关于初始化使用示例总结参考资料简介log4j 用于Java,log4c

C++归并排序代码实现示例代码

《C++归并排序代码实现示例代码》归并排序将待排序数组分成两个子数组,分别对这两个子数组进行排序,然后将排序好的子数组合并,得到排序后的数组,:本文主要介绍C++归并排序代码实现的相关资料,需要的... 目录1 算法核心思想2 代码实现3 算法时间复杂度1 算法核心思想归并排序是一种高效的排序方式,需要用

C++11范围for初始化列表auto decltype详解

《C++11范围for初始化列表autodecltype详解》C++11引入auto类型推导、decltype类型推断、统一列表初始化、范围for循环及智能指针,提升代码简洁性、类型安全与资源管理效... 目录C++11新特性1. 自动类型推导auto1.1 基本语法2. decltype3. 列表初始化3

C++11右值引用与Lambda表达式的使用

《C++11右值引用与Lambda表达式的使用》C++11引入右值引用,实现移动语义提升性能,支持资源转移与完美转发;同时引入Lambda表达式,简化匿名函数定义,通过捕获列表和参数列表灵活处理变量... 目录C++11新特性右值引用和移动语义左值 / 右值常见的左值和右值移动语义移动构造函数移动复制运算符