循环链表(测试代码,Qt5.1 for windows)
2018-04-12 00:00
302 查看
摘要: 只要将单链表的尾节点和头节点连接起来,单向链表就称为循环链表。
循环链表的原理很简单,这里就不在赘述。下面是循环链表的测试代码:
主函数所在文件:
circularlistwithheader.cpp
chainNode.h
circularListWithHeader.h
myExceptions.h
循环链表的原理很简单,这里就不在赘述。下面是循环链表的测试代码:
主函数所在文件:
circularlistwithheader.cpp
/* * 主函数 */ #include<iostream> #include "circularlistwithheader.h" using namespace std; int main(int argc, char *argv[]) { //测试构造函数 circularListWithHeader<int>y,z; cout<<"Initial size of y and z = " <<y.size()<<", " <<z.size() <<endl; //测试insert()函数 y.insert(0,2); y.insert(1,6); cout<<"after insert 2 and 6:";y.output(cout);cout<<endl; y.insert(0,1); y.insert(2,4); cout<<"after insert 1 and 4:";y.output(cout);cout<<endl; y.insert(3,5); y.insert(2,3); cout<<"after insert 5 and 3:";y.output(cout);cout<<endl; //测试size()函数 cout<<"size of y = "<<y.size()<<endl; //测试indexOf()函数 int index = y.indexOf(5); if(index < 0) { cout << "4 not fouond" <<endl; } { cout << "The index of 4 is" <<index <<endl; } index = y.indexOf(7); if(index < 0) { cout << "7 not found" <<endl; } else { cout << "The index of 7 is " <<index <<endl; } return 0; }
chainNode.h
/* * chainNode.h * 链表节点定义 * */ #ifndef CHAINNODE_H #define CHAINNODE_H template<class T> struct chainNode { //数据成员 T element; chainNode<T> *next; //方法 chainNode(){} chainNode(const T& element) { this->element = element; } chainNode(const T& element,chainNode<T>* next) { this->element = element; this->next = next; } }; #endif // CHAINNODE_H
circularListWithHeader.h
/* * circularListWithHeader.h * 循环链表实体类定义 */ #ifndef CIRCULARLISTWITHHEADER_H #define CIRCULARLISTWITHHEADER_H #include<iostream> #include<sstream> #include<string> #include "chainnode.h" #include "myexceptions.h" using namespace std; template<class T> class circularListWithHeader { public: //构造函数 circularListWithHeader(); /* * 一些其他函数 */ //返回链表中元素个数 int size() const {return listSize;} int indexOf(const T& theElement) const; void insert(int theIndex,const T& theElement); void output(ostream& out) const; protected: //检查索引是否合法 void checkIndex(int theIndex) const; chainNode<T>* headerNode;//指向头节点 int listSize;//元素个数 }; /* * 类中各个函数的具体实现 * */ template<class T> circularListWithHeader<T>::circularListWithHeader() { headerNode = new chainNode<T>(); headerNode->next = headerNode; listSize = 0; } template<class T> void circularListWithHeader<T>::checkIndex(int theIndex) const { if(theIndex < 0 || theIndex >= listSize) { ostringstream s; s <<"index = "<<theIndex<<" size = "<<listSize; throw illegalIndex(s.str()); } } template<class T> int circularListWithHeader<T>::indexOf(const T &theElement) const { headerNode->element = theElement; chainNode<T>* currentNode = headerNode->next; int index = 0; while(currentNode->element != theElement) { currentNode = currentNode->next; index++; } if(currentNode == headerNode) return -1; else return index; } template<class T> void circularListWithHeader<T>::insert(int theIndex, const T &theElement) { if(theIndex < 0 || theIndex > listSize) { ostringstream s; s<<"index = "<<theIndex<<" size = "<<listSize; throw illegalIndex(s.str()); } chainNode<T>* p = headerNode; for(int i = 0;i< theIndex;i++) { p = p->next; } p->next = new chainNode<T>(theElement,p->next); listSize++; } template<class T> void circularListWithHeader<T>::output(ostream &out) const { for(chainNode<T>* currentNode = headerNode->next; currentNode != headerNode; currentNode = currentNode->next) out<<currentNode->element<<" "; } //<<的重载 template<class T> ostream& operator <<(ostream& out,const circularListWithHeader<T>& x) { x.output(out); return out; } #endif // CIRCULARLISTWITHHEADER_H
myExceptions.h
/* * myExceptions.h * 这个类包含类对各种异常的处理类 */ #ifndef MYEXCEPTIONS_H #define MYEXCEPTIONS_H #include<string> using namespace std; //不合法的参数值 class illegalParameterValue { public: illegalParameterValue(string theMessage = "Illegal patameter value") { message = theMessage; } void outputMessage() { cout<<message<<endl; } private: string message; }; //输入数据不合法 class illegalInputData { public: illegalInputData(string theMessage = "Illegal data input") { message = theMessage; } void outputMessage() { cout <<message <<endl; } private: string message; }; //索引不合法 class illegalIndex { public: illegalIndex(string theMessage = "Illegal index") { message = theMessage; } void outputMessage() { cout<<message<<endl; } private: string message; }; #endif // MYEXCEPTIONS_H
相关文章推荐
- 线性表——数组描述(Qt5.1测试代码 for windows)
- 通过手动建立ODBC QT的测试代码
- Qt浅谈之窗体缩放(仅增加测试代码)
- Fedora14(linux)下qt配置OpenCV、测试代码 及qt无法启动终端模拟器'xterm' 的解决方案
- qt类似于qq托盘图标闪烁的消息提示 (胡峰原创,代码已经过测试运行)
- 日志_测试代码_Qt532
- 基于Qt的轨迹还原之三:代码实现与测试
- Qt浅谈之窗体缩放(仅增加测试代码)
- Qt测试代码执行时间
- win10环境下VTK7.1的编译和Qt调用VTK的详细教程(本文链接含VTK库和测试代码的分享)
- Linux USB HID Device 测试代码
- java识别文件编码格式代码(无引用jar包,测试可行)
- 白盒测试 代码检查 二
- QT5.1之Shadow build(QTcreator界面改了,但是显示的时候没有更新的问题)
- [Java虚拟机读书笔记]2章:各内存区OOM测试代码
- Java---设计模块(设计模块的简介及最简单的俩个单例代码加测试)
- qt4 代码迁移到qt5上方法
- 如何编写Junit测试代码
- opendir,readir对目录的操作+原创测试代码
- MongoDB性能测试与Python测试代码