数据结构--单链表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++判断水仙花数并输出示例代码》水仙花数是指一个三位数,其各位数字的立方和恰好等于该数本身,:本文主要介绍利用c++判断水仙花数并输出的相关资料,文中通过代码介绍的非常详细,需要的朋友可以... 以下是使用C++实现的相同逻辑代码:#include <IOStream>#include <vec

基于C++的UDP网络通信系统设计与实现详解

《基于C++的UDP网络通信系统设计与实现详解》在网络编程领域,UDP作为一种无连接的传输层协议,以其高效、低延迟的特性在实时性要求高的应用场景中占据重要地位,下面我们就来看看如何从零开始构建一个完整... 目录前言一、UDP服务器UdpServer.hpp1.1 基本框架设计1.2 初始化函数Init详解

C++ 右值引用(rvalue references)与移动语义(move semantics)深度解析

《C++右值引用(rvaluereferences)与移动语义(movesemantics)深度解析》文章主要介绍了C++右值引用和移动语义的设计动机、基本概念、实现方式以及在实际编程中的应用,... 目录一、右值引用(rvalue references)与移动语义(move semantics)设计动机1

C++ move 的作用详解及陷阱最佳实践

《C++move的作用详解及陷阱最佳实践》文章详细介绍了C++中的`std::move`函数的作用,包括为什么需要它、它的本质、典型使用场景、以及一些常见陷阱和最佳实践,感兴趣的朋友跟随小编一起看... 目录C++ move 的作用详解一、一句话总结二、为什么需要 move?C++98/03 的痛点⚡C++

详解C++ 存储二进制数据容器的几种方法

《详解C++存储二进制数据容器的几种方法》本文主要介绍了详解C++存储二进制数据容器,包括std::vector、std::array、std::string、std::bitset和std::ve... 目录1.std::vector<uint8_t>(最常用)特点:适用场景:示例:2.std::arra

C++构造函数中explicit详解

《C++构造函数中explicit详解》explicit关键字用于修饰单参数构造函数或可以看作单参数的构造函数,阻止编译器进行隐式类型转换或拷贝初始化,本文就来介绍explicit的使用,感兴趣的可以... 目录1. 什么是explicit2. 隐式转换的问题3.explicit的使用示例基本用法多参数构造

C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解

《C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解》:本文主要介绍C++,C#,Rust,Go,Java,Python,JavaScript性能对比全面... 目录编程语言性能对比、核心优势与最佳使用场景性能对比表格C++C#RustGoJavapythonjav

C++打印 vector的几种方法小结

《C++打印vector的几种方法小结》本文介绍了C++中遍历vector的几种方法,包括使用迭代器、auto关键字、typedef、计数器以及C++11引入的范围基础循环,具有一定的参考价值,感兴... 目录1. 使用迭代器2. 使用 auto (C++11) / typedef / type alias

C++ scoped_ptr 和 unique_ptr对比分析

《C++scoped_ptr和unique_ptr对比分析》本文介绍了C++中的`scoped_ptr`和`unique_ptr`,详细比较了它们的特性、使用场景以及现代C++推荐的使用`uni... 目录1. scoped_ptr基本特性主要特点2. unique_ptr基本用法3. 主要区别对比4. u

C++11中的包装器实战案例

《C++11中的包装器实战案例》本文给大家介绍C++11中的包装器实战案例,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录引言1.std::function1.1.什么是std::function1.2.核心用法1.2.1.包装普通函数1.2.