#include<iostream> template <typename DataType> class DoubleList; /*对于DoubleList这个类的前向引用声明*/ template <typename DataType> class DoubleListNode { //对DoubleListNode这个模板类进行声明 private: friend class DoubleList<DataType>; //将DoubleList这个类声明为自己的友元, //然后DoubleList这个双向链表类就可以调用这个DoubleListNode这个类了 DoubleListNode():m_pprior(NULL), m_pnext(NULL){} DoubleListNode(const DataType item, DoubleListNode<DataType>* Prior = NULL, DoubleListNode<DataType>* next = NULL):m_data(item), m_pprior(prior), m_pnext(next) {} //DoubleListNode的两个构造函数 //并且这两个构造函数都被私有化,只有类的成员或者友元才可以访问! ~DoubleListNode() {//DoubleListNode的析构函数 m_pprior = NULL; //析构函数中使指针指向了NULL m_pnext = NULL; } DataType m_data; //数据域 DoubleListNode* m_pprior;//指向前驱结点的指针域 DoubleListNode* m_pnext;//指向后驱结点的指针域 public: DataType getData(); //获取数据的公有成员函数 }; template <typename DataType> DataType DoubleListNode<DataType>::getData() { return this->m_data; //公有成员函数模板getData的类外定义 } template<typename DataType> class DoubleList { //DoubleList双向链表这个模板类的声明 public: DoubleList() { head = new DoubleListNode<DataType>(); } ~DoubleList() { cleanDoubleLink(); delete head; } public: void cleanDoubleList();//清空双向链表 int getLength();// 获取双向链表的长度 DoubleListNode<DataType>* findNode(int i = 0);//寻找第i个结点 DoubleListNode<DataType>* findData(DataType item);//寻找具有给定值数据的结点 bool insertNode(DataType item, int i = 0);//在第i个结点的后面插入新的结点 bool removeNode(int i = 0);//删除第i个结点 DataType getData(int i = 0);//获取第i个结点的数据 private: DoubleListNode<DataType>* head; //头指针 }; template<typename DataType> void DoubleList<DataType>::cleanDoubleLink() { DoubleListNode<DataType>* pmove= head->m_pnext, *pdel; while(pmove != NULL) { //一次删除指针后面的结点 pdel是一个临时指针 把链表上每个结点的右指针域都遍历一遍 pdel = pmove; pmove = pdel->m_pnext; delete pdel; } head->m_pnext = NULL; } template <typename DataType> int DoubleList<DataType>::getLength() { int count = 0; DoubleListNode<DataType>* pmove = head->m_pnext;//遍历链表计算结点数 while(pmove != NULL) { pmove = pmove->m_pnext; count++; } return count; } template <typename DataType> DoubleListNode<DataType>* DoubleList<DataType>::findNode(int n = 1) { if(n < 1) { //判断位置是否有效0 cout << "非有效位置" << endl; return NULL: } DoubleListNode<DataType>* pmove = head->m_pnext; for(int i = 1; i < n; i++) { //利用游标指针pmove查找指定位置的结点 pmove = pmove->m_pnext; if(pmove == NULL) { //判断结点的是否存在 cout << "不存在指定结点" << endl; return NULL; } } return pmove; } template<typename DataType> bool DoubleList<DataType>::insertNode(DataType item, int n) { int n; if(n < 1) { //判断所插入的位置是否有效 cout << "插入位置无效" << endl; return 0; } //创建新结点, 设置游标指针 DoubleListNode<DataType>* newnode = new DoubleListNode<DataType>(item), *pmove = head; if(newnode == NULL) { cout << "内存分配失败,新结点无法创建!" << endl; exit(1); } for(i = 1; i < n; i++) { pmove = pmove->m_pnext; if(pmove == NULL && i < n-1) { cout << "超出链表长度,插入位置无效" << endl; return 0; } } //插入新结点 newnode-> m_pnext= pmove->m_pnext; if(pmove->m_pnext != NULL) { //判断是否在链表的尾部添加结点 pmove->m_pnext->m_pprior = newnode; } newnode->m_pprior = pmove; pmove->m_pnext = newnode; return 1; } template <typename DataType> bool DoubleList<DataType>::removeNode(int n = 1) { if(n<1 || n>getlength) { cout << "位置不合法" << endl; return false; } DoubleListNode<DataType>* pmove = head->m_pnext, *pdel; for(int i = 1; i < n; i++) { pmove = pmove->m_pnext; if(pmove == NULL) { cout << "超出了链表范围" << endl; return false; } } //删除结点 pdel = pmove; pmove->m_pprior->m_pnext = pdel->m_pnext; pmove->m_pnext->m_pprior = pdel->m_pprior; delete pdel; return true; } template<typename DataType> DataType DoubleList<DataType>::getData(int n = 1) { if(n < 1 || n > getlength()) { cout << "指定位置无效" << endl; exit(1); } DoubleListNode<DataType>*pmove = head->m_next; for(int i = 1; i < n; i++) { pmove = pmove->m_next; if(pmove == NULL) { cout << "指定结点不存在" << endl; exit(1); } } if(pmove == NULL) { cout << "结点不存在" << endl; return NULL; } return pmove->m_data; } template<typename DataType> DoubleListNode<DataType>* DoubleList<DataType>::findData(DataType item) { DoubleListNode<DataType>* pmove = head->m_pnext; if(pmove == NULL) { cout << "链表是空表" << endl; exit(1); } while(pmove->getData() != item) { pmove = pmove->m_pnext; if(pmove == NULL) { cout << "没有所查找的结点" << endl; exit(1); } } remove pmove; }
主函数部分自己写
时间: 2024-11-08 21:24:24