C++中的模板与泛型编程:加速矩形框填数字算法的实现
![](https://csdnimg.cn/release/wenkucmsfe/public/img/col_vip.0fdee7e1.png)
![DOC](https://csdnimg.cn/release/download/static_files/pc/images/minetype/DOC.png)
数据结构各种算法实现(C++ 模板)极好
### 数据结构实现(C++模板) #### 一、顺序表 **知识点:** - **定义:** 顺序表是一种线性表,其中的数据元素在内存中是连续存储的。 - **实现方式:** 使用数组来表示顺序表。 - **基本操作:** - `Length()`: 返回顺序表中的元素个数。 - `Find(x)`: 查找值为`x`的元素所在位置。 - `IsElement(x)`: 判断顺序表中是否包含值为`x`的元素。 - `Insert(x, i)`: 在位置`i`处插入一个值为`x`的新元素。 - `Remove(x)`: 删除值为`x`的元素。 - `IsEmpty()`: 判断顺序表是否为空。 - `IsFull()`: 判断顺序表是否已满。 - `Get(i)`: 获取第`i`个元素的值。 **代码示例:** ```cpp template<typename Type> class SeqList { public: SeqList(int sz = DefaultSize) : m_nmaxsize(sz), m_ncurrentsize(-1) { if (sz > 0) { m_elements = new Type[m_nmaxsize]; } } ~SeqList() { delete[] m_elements; } int Length() const { // 获取长度 return m_ncurrentsize + 1; } int Find(Type x) const { // 查找 for (int i = 0; i <= m_ncurrentsize; i++) { if (m_elements[i] == x) { return i; } } cout << "找不到该元素" << endl; return -1; } int IsElement(Type x) const { // 是否存在 return Find(x) != -1; } int Insert(Type x, int i) { // 插入 if (IsFull()) { cout << "表已满,无法插入" << endl; return -1; } if (i < 0 || i > m_ncurrentsize) { cout << "插入位置不合法" << endl; return -1; } for (int j = m_ncurrentsize; j >= i; j--) { m_elements[j + 1] = m_elements[j]; } m_elements[i] = x; m_ncurrentsize++; return 0; } int Remove(Type x) { // 删除 int index = Find(x); if (index == -1) { cout << "未找到该元素" << endl; return -1; } for (int i = index; i < m_ncurrentsize; i++) { m_elements[i] = m_elements[i + 1]; } m_ncurrentsize--; return 0; } int IsEmpty() { // 判断是否为空 return m_ncurrentsize == -1; } int IsFull() { // 判断是否已满 return m_ncurrentsize == m_nmaxsize - 1; } Type Get(int i) { // 获取指定位置元素 if (i < 0 || i > m_ncurrentsize) { cout << "索引超出范围" << endl; return Type(); } return m_elements[i]; } void Print() { // 打印所有元素 for (int i = 0; i <= m_ncurrentsize; i++) { cout << m_elements[i] << " "; } cout << endl; } private: Type* m_elements; const int m_nmaxsize; int m_ncurrentsize; }; ``` #### 二、单链表 **知识点:** - **定义:** 单链表是一种线性表,其中每个元素包含两个部分:存储元素自身的数据域和指向下一个元素的指针域。 - **基本操作:** - `Insert(x, pos)`: 在位置`pos`处插入一个值为`x`的新节点。 - `Remove(x)`: 删除值为`x`的节点。 - `Find(x)`: 查找值为`x`的节点。 - `IsEmpty()`: 判断链表是否为空。 - `IsElement(x)`: 判断链表中是否存在值为`x`的节点。 - `Print()`: 输出链表的所有元素。 **代码示例:** ```cpp // ListNode.h template<typename Type> struct ListNode { Type data; ListNode<Type>* next; ListNode(const Type& d) : data(d), next(nullptr) {} }; // SingleList.h template<typename Type> class SingleList { public: SingleList() : head(new ListNode<Type>()) {} ~SingleList() { while (head->next != nullptr) { ListNode<Type>* temp = head->next; head->next = head->next->next; delete temp; } delete head; } void Insert(const Type& x, int pos) { ListNode<Type>* p = head; for (int i = 0; i < pos && p->next != nullptr; ++i) { p = p->next; } ListNode<Type>* newNode = new ListNode<Type>(x); newNode->next = p->next; p->next = newNode; } bool Remove(const Type& x) { ListNode<Type>* p = head; while (p->next != nullptr && p->next->data != x) { p = p->next; } if (p->next == nullptr) { return false; } ListNode<Type>* temp = p->next; p->next = temp->next; delete temp; return true; } bool IsElement(const Type& x) const { ListNode<Type>* p = head->next; while (p != nullptr && p->data != x) { p = p->next; } return p != nullptr; } bool IsEmpty() const { return head->next == nullptr; } void Print() const { ListNode<Type>* p = head->next; while (p != nullptr) { cout << p->data << " "; p = p->next; } cout << endl; } private: ListNode<Type>* head; }; ``` #### 三、双向链表 **知识点:** - **定义:** 双向链表是一种链表,其中每个节点包含三个部分:存储元素自身的数据域、指向前一个节点的前驱指针和指向后一个节点的后继指针。 - **基本操作:** - `Insert(x, pos)`: 在位置`pos`处插入一个值为`x`的新节点。 - `Remove(x)`: 删除值为`x`的节点。 - `Find(x)`: 查找值为`x`的节点。 - `IsEmpty()`: 判断链表是否为空。 - `IsElement(x)`: 判断链表中是否存在值为`x`的节点。 - `Print()`: 输出链表的所有元素。 **代码示例:** ```cpp // NodeList.h template<typename Type> struct NodeList { Type data; NodeList<Type>* prev; NodeList<Type>* next; NodeList(const Type& d) : data(d), prev(nullptr), next(nullptr) {} }; // DoubleList.h template<typename Type> class DoubleList { public: DoubleList() : head(new NodeList<Type>()) {} ~DoubleList() { while (head->next != nullptr) { NodeList<Type>* temp = head->next; head->next = head->next->next; delete temp; } delete head; } void Insert(const Type& x, int pos) { NodeList<Type>* p = head; for (int i = 0; i < pos && p->next != nullptr; ++i) { p = p->next; } NodeList<Type>* newNode = new NodeList<Type>(x); newNode->next = p->next; newNode->prev = p; if (p->next != nullptr) { p->next->prev = newNode; } p->next = newNode; } bool Remove(const Type& x) { NodeList<Type>* p = head->next; while (p != nullptr && p->data != x) { p = p->next; } if (p == nullptr) { return false; } if (p->prev != nullptr) { p->prev->next = p->next; } if (p->next != nullptr) { p->next->prev = p->prev; } delete p; return true; } bool IsElement(const Type& x) const { NodeList<Type>* p = head->next; while (p != nullptr && p->data != x) { p = p->next; } return p != nullptr; } bool IsEmpty() const { return head->next == nullptr; } void Print() const { NodeList<Type>* p = head->next; while (p != nullptr) { cout << p->data << " "; p = p->next; } cout << endl; } private: NodeList<Type>* head; }; ``` #### 四、循环链表 **知识点:** - **定义:** 循环链表是一种链表,其中最后一个节点的指针指向链表的第一个节点。 - **基本操作:** - `Insert(x, pos)`: 在位置`pos`处插入一个值为`x`的新节点。 - `Remove(x)`: 删除值为`x`的节点。 - `Find(x)`: 查找值为`x`的节点。 - `IsEmpty()`: 判断链表是否为空。 - `IsElement(x)`: 判断链表中是否存在值为`x`的节点。 - `Print()`: 输出链表的所有元素。 **代码示例:** ```cpp // ListNode.h template<typename Type> struct ListNode { Type data; ListNode<Type>* next; ListNode(const Type& d) : data(d), next(nullptr) {} }; // CircularList.h template<typename Type> class CircularList { public: CircularList() : head(new ListNode<Type>()) { head->next = head; } ~CircularList() { while (head->next != head) { ListNode<Type>* temp = head->next; head->next = head->next->next; delete temp; } delete head; } void Insert(const Type& x, int pos) { ListNode<Type>* p = head; for (int i = 0; i < pos && p->next != head; ++i) { p = p->next; } ListNode<Type>* newNode = new ListNode<Type>(x); newNode->next = p->next; p->next = newNode; } bool Remove(const Type& x) { ListNode<Type>* p = head; while (p->next != head && p->next->data != x) { p = p->next; } if (p->next == head) { return false; } ListNode<Type>* temp = p->next; p->next = temp->next; delete temp; return true; } bool IsElement(const Type& x) const { ListNode<Type>* p = head->next; while (p != head && p->data != x) { p = p->next; } return p != head; } bool IsEmpty() const { return head->next == head; } void Print() const { ListNode<Type>* p = head->next; while (p != head) { cout << p->data << " "; p = p->next; } cout << endl; } private: ListNode<Type>* head; }; ``` 以上内容分别介绍了顺序表、单链表、双向链表以及循环链表的基本概念、操作及其实现代码。这些数据结构在实际编程中有着广泛的应用,通过学习和掌握它们,可以更高效地解决许多问题。
![profit](https://csdnimg.cn/release/wenkucmsfe/public/img/col_text.aae01bfa.png)
![profit](https://csdnimg.cn/release/wenkucmsfe/public/img/col_load.37200090.png)
![profit](https://csdnimg.cn/release/wenkucmsfe/public/img/col_ai.cfc3dd33.png)
相关推荐
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://profile-avatar.csdnimg.cn/1ae1d3445f284231bf75536a082a5aa0_weixin_26516551.jpg!1)
张诚01
专栏目录
![](https://csdnimg.cn/release/wenkucmsfe/public/img/col_text.aae01bfa.png)
![](https://csdnimg.cn/release/wenkucmsfe/public/img/col_load.37200090.png)
![](https://csdnimg.cn/release/wenkucmsfe/public/img/col_ai.cfc3dd33.png)
最新推荐
![](https://img-home.csdnimg.cn/images/20241226111658.png)
从数据中学习,提升备份策略:DBackup历史数据分析篇
![](https://img-home.csdnimg.cn/images/20241226111658.png)
【数据库升级】:避免风险,成功升级MySQL数据库的5个策略
![](https://img-home.csdnimg.cn/images/20241226111658.png)
【射频放大器设计】:端阻抗匹配对放大器性能提升的决定性影响
![](https://img-home.csdnimg.cn/images/20241226111658.png)
【数据分布策略】:优化数据分布,提升FOX并行矩阵乘法效率
![](https://img-home.csdnimg.cn/images/20241226111658.png)
【遥感分类工具箱】:ERDAS分类工具使用技巧与心得
![](https://img-home.csdnimg.cn/images/20241226111658.png)
面向对象编程表达式:封装、继承与多态的7大结合技巧
![](https://img-home.csdnimg.cn/images/20241226111658.png)
电力电子技术的智能化:数据中心的智能电源管理
![](https://img-home.csdnimg.cn/images/20241226111658.png)
【终端打印信息的项目管理优化】:整合强制打开工具提高项目效率
![](https://img-home.csdnimg.cn/images/20241226111658.png)
TransCAD用户自定义指标:定制化分析,打造个性化数据洞察
![](https://img-home.csdnimg.cn/images/20241226111658.png)
数据分析与报告:一卡通系统中的数据分析与报告制作方法
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035111.png)
专栏目录
![](https://csdnimg.cn/release/wenkucmsfe/public/img/col_text.aae01bfa.png)
![](https://csdnimg.cn/release/wenkucmsfe/public/img/col_load.37200090.png)
![](https://csdnimg.cn/release/wenkucmsfe/public/img/col_ai.cfc3dd33.png)