用C++实现一个基于模板的双向循环链表并满足如下要求: 1:具备规范且详尽的注释 2:每个节点都是一条双向循环链表 3:支持图形化打印

时间: 2024-01-22 15:19:14 浏览: 28
以下是基于模板的双向循环链表的C++实现代码,其中包含注释和支持图形化打印的功能: ```c++ #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<algorithm> #include<queue> #include<vector> #include<map> #include<set> #include<sstream> #include<iomanip> using namespace std; // 双向循环链表的节点定义 template<typename T> struct DListNode{ T data; // 存储数据 DListNode<T>* prev; // 指向前驱节点的指针 DListNode<T>* next; // 指向后继节点的指针 DListNode(T _data = T(), DListNode<T>* _prev = NULL, DListNode<T>* _next = NULL):data(_data), prev(_prev), next(_next){} }; // 双向循环链表的实现 template<typename T> class DList{ public: DList(); // 构造函数 ~DList(); // 析构函数 bool empty() const; // 判断链表是否为空 int size() const; // 获取链表长度 DListNode<T>* front() const; // 获取头节点 DListNode<T>* back() const; // 获取尾节点 void insert(DListNode<T>* pos, const T& val); // 在指定位置插入节点 void erase(DListNode<T>* pos); // 删除指定位置的节点 void push_front(const T& val); // 在头部插入节点 void pop_front(); // 删除头部节点 void push_back(const T& val); // 在尾部插入节点 void pop_back(); // 删除尾部节点 void clear(); // 清空链表 void print() const; // 打印链表 private: int len; // 链表长度 DListNode<T>* head; // 指向头节点的指针 DListNode<T>* tail; // 指向尾节点的指针 }; // 构造函数 template<typename T> DList<T>::DList(){ head = new DListNode<T>(); tail = new DListNode<T>(); head->prev = tail->next = NULL; head->next = tail; tail->prev = head; len = 0; } // 析构函数 template<typename T> DList<T>::~DList(){ clear(); delete head; delete tail; } // 判断链表是否为空 template<typename T> bool DList<T>::empty() const{ return len == 0; } // 获取链表长度 template<typename T> int DList<T>::size() const{ return len; } // 获取头节点 template<typename T> DListNode<T>* DList<T>::front() const{ return empty() ? NULL : head->next; } // 获取尾节点 template<typename T> DListNode<T>* DList<T>::back() const{ return empty() ? NULL : tail->prev; } // 在指定位置插入节点 template<typename T> void DList<T>::insert(DListNode<T>* pos, const T& val){ DListNode<T>* node = new DListNode<T>(val, pos->prev, pos); pos->prev->next = node; pos->prev = node; len++; } // 删除指定位置的节点 template<typename T> void DList<T>::erase(DListNode<T>* pos){ pos->prev->next = pos->next; pos->next->prev = pos->prev; delete pos; len--; } // 在头部插入节点 template<typename T> void DList<T>::push_front(const T& val){ insert(head->next, val); } // 删除头部节点 template<typename T> void DList<T>::pop_front(){ erase(head->next); } // 在尾部插入节点 template<typename T> void DList<T>::push_back(const T& val){ insert(tail, val); } // 删除尾部节点 template<typename T> void DList<T>::pop_back(){ erase(tail->prev); } // 清空链表 template<typename T> void DList<T>::clear(){ while(!empty()){ erase(head->next); } } // 打印链表 template<typename T> void DList<T>::print() const{ DListNode<T>* node = head->next; while(node != tail){ cout<<node->data<<" "; node = node->next; } cout<<endl; } ``` 以下是基于模板的双向循环链表的测试代码,其中包含了图形化打印的功能: ```c++ #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<algorithm> #include<queue> #include<vector> #include<map> #include<set> #include<sstream> #include<iomanip> #include<windows.h> using namespace std; // 双向循环链表的节点定义 template<typename T> struct DListNode{ T data; // 存储数据 DListNode<T>* prev; // 指向前驱节点的指针 DListNode<T>* next; // 指向后继节点的指针 DListNode(T _data = T(), DListNode<T>* _prev = NULL, DListNode<T>* _next = NULL):data(_data), prev(_prev), next(_next){} }; // 双向循环链表的实现 template<typename T> class DList{ public: DList(); // 构造函数 ~DList(); // 析构函数 bool empty() const; // 判断链表是否为空 int size() const; // 获取链表长度 DListNode<T>* front() const; // 获取头节点 DListNode<T>* back() const; // 获取尾节点 void insert(DListNode<T>* pos, const T& val); // 在指定位置插入节点 void erase(DListNode<T>* pos); // 删除指定位置的节点 void push_front(const T& val); // 在头部插入节点 void pop_front(); // 删除头部节点 void push_back(const T& val); // 在尾部插入节点 void pop_back(); // 删除尾部节点 void clear(); // 清空链表 void print() const; // 打印链表 void graphicalPrint() const; // 图形化打印链表 private: int len; // 链表长度 DListNode<T>* head; // 指向头节点的指针 DListNode<T>* tail; // 指向尾节点的指针 }; // 构造函数 template<typename T> DList<T>::DList(){ head = new DListNode<T>(); tail = new DListNode<T>(); head->prev = tail->next = NULL; head->next = tail; tail->prev = head; len = 0; } // 析构函数 template<typename T> DList<T>::~DList(){ clear(); delete head; delete tail; } // 判断链表是否为空 template<typename T> bool DList<T>::empty() const{ return len == 0; } // 获取链表长度 template<typename T> int DList<T>::size() const{ return len; } // 获取头节点 template<typename T> DListNode<T>* DList<T>::front() const{ return empty() ? NULL : head->next; } // 获取尾节点 template<typename T> DListNode<T>* DList<T>::back() const{ return empty() ? NULL : tail->prev; } // 在指定位置插入节点 template<typename T> void DList<T>::insert(DListNode<T>* pos, const T& val){ DListNode<T>* node = new DListNode<T>(val, pos->prev, pos); pos->prev->next = node; pos->prev = node; len++; } // 删除指定位置的节点 template<typename T> void DList<T>::erase(DListNode<T>* pos){ pos->prev->next = pos->next; pos->next->prev = pos->prev; delete pos; len--; } // 在头部插入节点 template<typename T> void DList<T>::push_front(const T& val){ insert(head->next, val); } // 删除头部节点 template<typename T> void DList<T>::pop_front(){ erase(head->next); } // 在尾部插入节点 template<typename T> void DList<T>::push_back(const T& val){ insert(tail, val); } // 删除尾部节点 template<typename T> void DList<T>::pop_back(){ erase(tail->prev); } // 清空链表 template<typename T> void DList<T>::clear(){ while(!empty()){ erase(head->next); } } // 打印链表 template<typename T> void DList<T>::print() const{ DListNode<T>* node = head->next; while(node != tail){ cout<<node->data<<" "; node = node->next; } cout<<endl; } // 图形化打印链表 template<typename T> void DList<T>::graphicalPrint() const{ HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); DListNode<T>* node = head->next; int pos = 0; while(node != tail){ stringstream ss; ss<<node->data; string str; ss>>str; if(pos % 2 == 0){ SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | FOREGROUND_RED); } else{ SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | FOREGROUND_GREEN); } cout<<str<<" -> "; pos++; node = node->next; } SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); cout<<"NULL\n"; } ``` 测试代码: ```c++ int main(){ DList<int> list; for(int i = 1; i <= 10; i++){ list.push_back(i); } cout<<"The original list is: "; list.print(); cout<<"The graphical print of the original list is:"<<endl; list.graphicalPrint(); DListNode<int>* pos = list.front()->next->next; list.insert(pos, 100); cout<<"After inserting 100 at the third position, the list is: "; list.print(); cout<<"The graphical print of the list after inserting 100 at the third position is:"<<endl; list.graphicalPrint(); pos = list.front()->next; list.erase(pos); cout<<"After erasing the second element, the list is: "; list.print(); cout<<"The graphical print of the list after erasing the second element is:"<<endl; list.graphicalPrint(); list.clear(); cout<<"After clearing the list, the size of the list is "<<list.size()<<endl; cout<<"The graphical print of the list after clearing is:"<<endl; list.graphicalPrint(); return 0; } ``` 输出结果: ``` The original list is: 1 2 3 4 5 6 7 8 9 10 The graphical print of the original list is: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> NULL After inserting 100 at the third position, the list is: 1 2 100 3 4 5 6 7 8 9 10 The graphical print of the list after inserting 100 at the third position is: 1 -> 2 -> 100 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> NULL After erasing the second element, the list is: 1 100 3 4 5 6 7 8 9 10 The graphical print of the list after erasing the second element is: 1 -> 100 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> NULL After clearing the list, the size of the list is 0 The graphical print of the list after clearing is: NULL ```

相关推荐

最新推荐

recommend-type

C++双向链表实现简单通讯录

主要为大家详细介绍了C++双向链表实现简单通讯录,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

C语言:一元多项式加减法运算(链表 附答案).docx

C语言链表的入门题,里面提供了两种思路供参考,用链表来实现一元多项式的加减法,并按照一定规律输出。也是练习链表和排序算法的一道小实验,初学链表的小伙伴可以参考参考噢
recommend-type

android手机应用源码Imsdroid语音视频通话源码.rar

android手机应用源码Imsdroid语音视频通话源码.rar
recommend-type

营销计划汇报PPT,市场品牌 推广渠道 产品 营销策略tbb.pptx

营销计划汇报PPT,市场品牌 推广渠道 产品 营销策略tbb.pptx
recommend-type

JavaScript_超过100种语言的纯Javascript OCR.zip

JavaScript
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】增量式PID的simulink仿真实现

# 2.1 Simulink仿真环境简介 Simulink是MATLAB中用于建模、仿真和分析动态系统的图形化环境。它提供了一个直观的用户界面,允许用户使用块和连接线来创建系统模型。Simulink模型由以下元素组成: - **子系统:**将复杂系统分解成更小的、可管理的模块。 - **块:**代表系统中的组件,如传感器、执行器和控制器。 - **连接线:**表示信号在块之间的流动。 Simulink仿真环境提供了广泛的块库,涵盖了各种工程学科,包括控制系统、电子和机械工程。它还支持用户自定义块的创建,以满足特定仿真需求。 # 2. Simulink仿真环境的搭建和建模 ### 2.
recommend-type

训练集和测试集的准确率都99%,但是预测效果不好

即使训练集和测试集的准确率都很高,但是在实际的预测中表现不佳,可能会有以下几个原因: 1. 数据质量问题:模型训练和测试的数据可能存在问题,比如数据标签错误、数据集采样不均衡、数据集中存在异常值等问题,这些问题可能会导致模型在实际预测中表现不佳。 2. 特征工程问题:模型的特征工程可能存在问题,导致模型无法很好地捕捉数据的特征,从而影响预测效果。 3. 模型选择问题:模型的选择可能不合适,比如选择的模型太简单,无法很好地拟合数据,或者选择的模型太复杂,导致过拟合等问题。 4. 超参数调整问题:模型的超参数可能没有调整到最佳状态,需要进行调整。 针对以上可能的原因,可以采取相应的措施进
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。