自己编写的List

这个纯粹属于自己写来玩的,大家看看即可 有意见提出我改正
没有实现的有以下部分:
1 迭代器 某些数据成员没有添加
2 某些构造函数:如List(const List& srclist, int iBeginPos, int iEndPos);等等
3 某些运算符的重载:如operator[], operator(),等
4 特殊List<string> 的具体实现。
最新头文件

#ifndef _LIST_H #define _LIST_H #include <iostream> #include <windows.h> template<class T> class Node { public:     Node(){}//构造函数     Node(T x,Node<T> *pNext = NULL,Node<T> *pPrev = NULL)         :m_x(x),m_pNext(pNext),m_pPrev(pPrev){}     ~Node(){}     T m_x;//x坐标     Node<T> *m_pNext;//前指针     Node<T> *m_pPrev;//后指针 };

template<class T> class List { public:     List()     {         m_pHead = m_pTail = NULL;         m_size = 0;     }     List(T x)     {         m_pHead = m_pTail = new Node(x);         m_size = 1;     }     List(List<T> &list)     {         Node<T> * pNode = list.m_pHead;         m_pHead = NULL;//此处必须初始化,否则会出现错误         m_pTail = NULL;         m_size = 0;         while (NULL != pNode)         {             PushBackPoint(pNode->m_x);             pNode = pNode->m_pNext;         }     }     List(int iLength, T iValue)     {         m_size = iLength;         m_pHead = NULL;         m_pTail = NULL;         while (iLength != 0)         {             PushBackPoint(iValue);             iLength -= 1;         }     }     List(WORD iLength, T iValue)     {         m_size = iLength;         m_pHead = NULL;         m_pTail = NULL;         while (iLength != 0)         {             PushBackPoint(iValue);             iLength -= 1;         }     }     //析构函数     ~List();

    //前插节点     bool PushFrontPont(T x);

    //后插节点     bool PushBackPoint(T x);

    //前出节点     bool PopFrontPoint();

    //后出节点     bool PopBackPoint();

    //查询节点     Node<T>*FindPoint(T x);

    //插入节点     bool InsertPoint(T x, int pos);         //删除节点     bool DeletePoint(T x);

    //输出树     void FrontOutPutTree();         //输出树     void BackOutPutTree();

    //排序函数     void Sort();

    //getfront     Node<T> * GetFront(){return m_pHead;}

    //getend     Node<T> * GetEnd(){  return m_pTail;}

    bool Empty();

    //赋值运算符重载     List<T>& operator= (List<T> &bt)     {         Node<T> *pNode = bt.m_pHead;         while (NULL != pNode)         {             PushBackPoint(pNode->m_x);             pNode = pNode->m_pNext;         }         return *this;     }

    //加号运算符重载 链表相加 就是两个表的节点相加     friend List<T> operator+(const List<T> &lbt,const List<T> &rbt)     {         List<T> list;         Node<T> *pNode = lbt.m_pHead;         while (NULL != pNode)         {             list.PushBackPoint(pNode->m_x);//从前取从后入             pNode = pNode->m_pNext;         }         pNode = rbt.m_pHead;         while (NULL != pNode)         {              list.PushBackPoint(pNode->m_x);              pNode = pNode->m_pNext;         }         return list;     }

    // >运算符重载     friend bool operator> (List<T> &lbt,List<T> &rbt)     {         Node<T> *plNode = lbt.m_pHead;         Node<T> *prNode = rbt.m_pHead;         while (NULL != plNode && NULL != prNode)         {             if (plNode->m_x > prNode->m_x)             {                 return true;             }             else if (plNode->m_x == prNode->m_x)             {                 plNode = plNode->m_pNext;                 prNode = prNode->m_pNext;             }             else             {                 return false;             }         }         if (NULL == prNode)         {             return true;         }         else         {             return false;         }     }     //获取list的大小     WORD GetSize(){return m_size;}

    //size增加     void AddSize(){m_size++;}

    //size减小     void SubSize(){m_size--;}

private:     WORD m_size;     //list的大小     Node<T> *m_pHead;//头指针     Node<T> *m_pTail;//尾指针 };

#endif

 
 
具体的实现
 

#include "List.h"

using namespace std; //后出节点 template<class T> bool List<T>::PopBackPoint() {     if (NULL == m_pTail)     {         return false;     }     Node<T> * pNode = m_pTail;     m_pTail = m_pTail->m_pPrev;     m_pTail->m_pNext = NULL;

    if (NULL == m_pTail)     {         m_pHead = NULL;     }     delete pNode;     pNode = NULL;     SubSize();     return true; } //前插入节点 template<class T> bool List<T>::PushFrontPont(T x) {     m_pHead = new Node<T>(x,m_pHead);     if (m_pTail == NULL)     {         m_pTail = m_pHead;     }     else     {         m_pHead->m_pNext->m_pPrev = m_pHead;     }     AddSize();     return true; } //找到节点并返回节点的指针 template<class T> Node<T>* List<T>::FindPoint(T x) {     Node<T> * pNode = m_pHead;     while (NULL != pNode)     {         if (pNode->m_x == x)         {             return pNode;         }         pNode = pNode->m_pNext;     }     return NULL; } //输出节点 template<class T> void List<T>::FrontOutPutTree() {     Node<T> *pNode = m_pHead;     while (NULL != pNode)     {         cout << pNode->m_x << " ";         pNode = pNode->m_pNext;     }     cout << endl; } //输出节点 template<class T> void List<T>::BackOutPutTree() {     Node<T> *pNode = m_pTail;     while (NULL != pNode)     {         cout << pNode->m_x << " ";         pNode = pNode->m_pPrev;     }     cout << endl; } //后插节点 template<class T> bool List<T>::PushBackPoint(T x) {     m_pTail = new Node<T>(x, NULL, m_pTail);     if (m_pHead == NULL)     {         m_pHead = m_pTail;     }     else     {         m_pTail->m_pPrev->m_pNext = m_pTail;     }     AddSize();     return true; }

//前出节点 template<class T> bool List<T>::PopFrontPoint() {     Node<T> * pNode = m_pHead;     if (pNode == NULL)     {         return false;     }     m_pHead = pNode->m_pNext;     delete pNode;     pNode = NULL;

    m_pHead->m_pPrev = NULL;     if (NULL == m_pHead)     {         m_pTail = m_pHead;     }     SubSize();     return true; } //插入节点 template<class T> bool List<T>::InsertPoint(T x, int pos) {     Node<T> *pNode = m_pHead;     Node<T> *pNewNode = NULL;     int iPosTemp = 0;     while ((NULL != pNode) && (iPosTemp != pos))     {         pNode = pNode->m_pNext;         iPosTemp++;     }     if (NULL == pNode)     {         return false;     }     if (pos == iPosTemp)     {         pNewNode  = new Node<T>(x, pNode,pNode->m_pPrev);         pNode->m_pPrev->m_pNext = pNewNode;         pNode->m_pPrev = pNewNode;     }     AddSize();     return true; } //删除节点 template<class T> bool List<T>::DeletePoint(T x) {     Node<T> *pFind = NULL;     pFind = FindPoint(x);     if (NULL == pFind)     {         return false;     }     pFind->m_pPrev->m_pNext = pFind->m_pNext;     pFind->m_pNext->m_pPrev = pFind->m_pPrev;     delete pFind;     pFind = NULL;     SubSize();     return true; } //析构函数 template<class T> List<T>::~List() {     Node<T> *pNode = m_pHead;     Node<T> *pNext = m_pHead;     while (NULL != pNode)     {         pNext = pNode->m_pNext;         delete pNode;         pNode = NULL;         pNode = pNext;     }     m_pHead = m_pTail = NULL;     m_size = 0; } template<class T> bool List<T>::Empty() {     if (NULL == m_pTail && NULL == m_pHead)     {         return true;     }     return false; }

template<class T> void List<T>::Sort() {     Node<T> *pNode = NULL;     Node<T> *pTemp = m_pTail;     T Temp = 0;     for (;pTemp != m_pHead; pTemp = pTemp->m_pPrev)     {         pNode = m_pHead;         for (; pNode != pTemp; pNode = pNode->m_pNext)         {             if (pNode->m_x > pTemp->m_x)             {                 Temp = pNode->m_x;                 pNode->m_x = pTemp->m_x;                 pTemp->m_x = Temp;             }         }     } }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值