c++类模板实现的双向链表

c++类模板实现的双向链表

  • myList.h
#ifndef _MYLIST_H_
#define _MYLIST_H_

template <class T>
class ListNode
{
public: 

    ListNode* pHead =    NULL;
    ListNode* pTail =    NULL;      
    T value;        
};

template <class T>
class ListHead
{
public:
    ListNode<T>* pNext = NULL;
    int length         = 0; 
};

template <class T>
class MyList
{
private:

    ListHead<T>  listHead;                          //链表头 
    ListNode<T>* pCurrentNode = NULL;               //每次指向最后一个尾节点::尾插尾删使用   
public:

    MyList();
    MyList(const MyList<T>& pList);
    ~MyList();

    int   copy(const MyList<T>& pListHead);         //浅拷贝 
    int   deepCopy(const MyList<T>& pList);         //深拷贝 
    int   push_back(const T& node);                 //尾插 
    int   push_front(const T& node);                //头插 
    int   pop_back();                               //尾删 
    int   pop_front();                              //头删 
    void  earse();                                  //销毁整个链表
    T&    operator[](int i);                        //重载 
    int   empty();                                  //是否为空 
    int   size();                                   //链表大小 

};



#endif
  • myList.hpp
#ifndef _MYLIST_HPP_
#define _MYLIST_HPP_

#include <iostream>
#include "myList.h"

//构造 
template <class T>
MyList<T>::MyList()
{
    this->listHead.pNext  = NULL; 
    this->listHead.length = 0;

    this->pCurrentNode    = NULL; 
};

template <class T>
MyList<T>::MyList(const MyList<T>& list)
{
    deepCopy(list); 
};

//析构
template<class T>
MyList<T>::~MyList()
{
    this->earse();
}

//浅拷贝 
template <class T>
int MyList<T>::copy(const MyList<T>& list)
{
    this->earse();

    this->listHead    = list.listHead;
    this->pCurrentNode = list.pCurrentNode;

    return 0;
}

//深拷贝 
template <class T>
int MyList<T>::deepCopy(const MyList<T>& list)
{
    this->earse();

    ListNode<T>* m_pCurrentNode = list.listHead.pNext;
    for(int i=0; i<list.listHead.length; i++)
    {
        int isSucess = this->push_back(m_pCurrentNode->value);
        if(isSucess == -1)
        {
            this->earse();
            return -1;
        }   

        m_pCurrentNode = m_pCurrentNode->pTail;
    } 

    return 0;
}; 

//尾插 
template <class T>
int MyList<T>::push_back(const T& value)          
{
    ListNode<T>* m_pNode = new ListNode<T>[1];
    if (m_pNode == NULL)
        return -1;

    m_pNode->pTail = NULL;
    m_pNode->pHead = this->pCurrentNode;
    m_pNode->value = value; 

    if (this->listHead.length == 0)    
        this->listHead.pNext = m_pNode;
    else
        this->pCurrentNode->pTail = m_pNode;

    this->pCurrentNode = m_pNode;
    this->listHead.length ++;

    return 0;
};

template <class T>
int MyList<T>::push_front(const T& value) //头插 
{
    ListNode<T>* m_pNode = new ListNode<T>[1];
    if (m_pNode == NULL)
        return -1;

    if (this->listHead.length != 0)
        this->listHead.pNext->pHead = m_pNode;

    m_pNode->value = value;
    m_pNode->pHead = NULL;
    m_pNode->pTail = this->listHead.pNext;

    this->listHead.pNext = m_pNode;
    this->listHead.length ++;

    return 0;
}

//尾删
template<class T>
int MyList<T>::pop_back()
{
    if (this->listHead.length <= 0)
        return -1;

    this->pCurrentNode = this->pCurrentNode->pHead;
    this->listHead.length --;

    delete this->pCurrentNode->pTail;
    this->pCurrentNode->pTail = NULL;

    return 0;
}

//头删
template<class T>
int MyList<T>::pop_front()
{
    if (this->listHead.length <= 0)
        return -1;

    ListNode<T>* m_pTemporaNode = this->listHead.pNext;

    this->listHead.pNext = m_pTemporaNode->pTail;
    this->listHead.length --;

    delete m_pTemporaNode;
    m_pTemporaNode = NULL;

    return 0;
}

//销毁整个链表
template <class T>
void MyList<T>::earse() 
{
    int m_listLength = this->listHead.length;
    for (int i=0; i<m_listLength; i++)
    {
        pop_front();
    } 
}

//重载
template<class T>
T& MyList<T>::operator[](int num)
{
    int m_number = 0;

    if (num >= this->size() || (-num) >= this->size())
        m_number = 0;
    else if (num >= 0)
        m_number = num;
    else
        m_number = num + this->listHead.length;

    ListNode<T>* m_pCurrentNode = this->listHead.pNext;
    for (int i = 0; i < m_number; ++i)
    {
        m_pCurrentNode = m_pCurrentNode->pTail;
    }

    return m_pCurrentNode->value;
}  

//是否为空 
template<class T>
int MyList<T>::empty()
{
    if (this.listHead.length <= 0)
        return -1;

    return 0;
}

//链表大小
template<class T>
int MyList<T>::size()
{
    return this->listHead.length;
}


#endif
  • main.cpp
#include <iostream>
#include "myList.hpp"

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {

    MyList<int> lis;
    int k = 4;

    lis.push_back(4);
    lis.push_back(6);
    lis.push_back(1);
    lis.push_back(9);
    lis.push_front(7);
    lis.push_front(55);

    MyList<int> li(lis);

    for (int i=0; i < lis.size(); i++)
    {
        std::cout << li[i] << std::endl;
    }

    lis.earse();

    std::cout << "please input any key" << std::endl;
    getchar();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值