C++模板--------双向链表

    技术2022-05-20  47

     

    /***********************************************/

     

                        CListT.h

     

    POSITION        是参照C++标准模板CList写的

    /***********************************************/   

    #ifndef  CLISTT_H#define  CLISTT_H

    #include "stdafx.h"

    #include <iostream>using namespace std;struct __POSITION {};typedef __POSITION* POSITION;

    template <typename Type>class CListT{protected: class Node{ public:  Node(const Node* pNode):data(pNode->data),   pre(pNode->pre),   next(pNode->next){};  Node(void):data(0),   pre(NULL),   next(NULL){};  Node(Type v):data(v),   pre(NULL),   next(NULL){};  ~Node(void){data=0;delete pre;delete next;};  Type  data;  Node* pre;  Node* next; };

    public:  CListT(void):pHead(NULL),  pEnd(NULL){}; ~CListT(); void Insert(POSITION pos,Type v); void Append(Type v); void Remove(POSITION pos); POSITION posFindPositionByIndex(int index); void printCListT();private: Node* pHead; Node* pEnd;

    };template <typename Type>CListT<Type>::~CListT(){

    }template <typename Type>void CListT<Type>::Insert(POSITION pos,Type v){ if (pos==NULL||((Node*)pos)->next==NULL) {  Append(v); } else {  Node* pTemp=new Node(v);  ((Node*)pos)->next->pre=pTemp;  pTemp->next=((Node*)pos)->next;  pTemp->pre=((Node*)pos);  ((Node*)pos)->next=pTemp; }}

    template <typename Type>void CListT<Type>::Append(Type v){ Node* pTemp=new Node(v); if (pHead==NULL) {  pHead=pTemp;  pEnd=pHead; } else {  pTemp->pre=pEnd;  pEnd->next=pTemp;  pEnd=pEnd->next; }}

    template <typename Type>void CListT<Type>::Remove(POSITION pos){ if (!pos) {  return; } if (((Node*)pos)->pre) {  ((Node*)pos)->next->pre=((Node*)pos)->pre;  ((Node*)pos)->pre->next=((Node*)pos)->next;  ((Node*)pos)->next=NULL;  ((Node*)pos)->pre=NULL; } else {  pHead=pHead->next;  pHead->pre=NULL;  ((Node*)pos)->next=NULL;  ((Node*)pos)->pre=NULL; }  delete ((Node*)pos);

    }

    template <typename Type>POSITION CListT<Type>::posFindPositionByIndex(int index){ int i=1; Node * pTemp=pHead; while (pTemp) {  if (i==index)  {   return (POSITION)pTemp;  }  else if (i>index)  {   break;  }  pTemp=pTemp->next;  i++; } return NULL;}

    template <typename Type>void CListT<Type>::printCListT(){ Node * pTemp=pHead; while (pTemp) {  if (pTemp->next)  {   cout<<pTemp->data<<"->";  }  else  {   cout<<pTemp->data;  }    pTemp=pTemp->next; } cout<<endl;}#endif

     

     

     

     

    /*******************************************/

     

                       CListT.c======测试代码

     

    /*******************************************/

    // CListT.cpp : Defines the entry point for the console application.//

    #include "stdafx.h"#include "CListT.h"

    int _tmain(int argc, _TCHAR* argv[]){ CListT<float> test1;    test1.Append(2.0); test1.Append(3.0); test1.Append(5.3);// test1.printCListT(); cout<<"在第2个节点插入元素"<<endl; test1.Insert(test1.posFindPositionByIndex(2),6); test1.printCListT(); cout<<"删除第2个元素"<<endl; test1.Remove(test1.posFindPositionByIndex(2)); test1.printCListT(); cout<<"Append:"<<endl; test1.Append(90); test1.printCListT();

     cout<<endl<<endl; CListT<char*> test2; cout<<"字符串操作 :"<<endl; test2.Append("gg"); test2.Append("hh"); test2.printCListT(); cout<<"在第2个节点插入元素"<<endl; test2.Insert(test2.posFindPositionByIndex(2),"insert"); test2.printCListT(); cout<<"删除第1个元素"<<endl; test2.Remove(test2.posFindPositionByIndex(1)); test2.printCListT();

     

     system("pause"); return 0;}

     


    最新回复(0)