最近开始学习STL,C 标准模板库,个人根据标准模板库中的list 自己写了个list模板,由于作业量较大,只实现了list里的基本功能,给大家分享下,如有更好的意见请加qq350544011,一起讨论讨论. 代码如下:List.h文件:
最近创建了自己的讨论群 希望大家能一起加入 群号:45811732
#ifndef LIST#define LIST#include "stdafx.h"
/////////////////////////////链表类模板的声明///////////////////////
template<typename T>
class CList
{
public:
//结点 struct Node
{
T node;
Node* next;
};
//跌代器
class iterator
{
public: iterator()
{
};
~iterator() { };
//重载"!="
bool operator != (iterator &it)
{
if(m_val != it.m_val)
{
return true;
}
return false;
}
//重载"前++ "
iterator operator++ ()
{
m_val = m_val->next;
return *this;
}
//重载 "后++ "
iterator operator++ (int)
{
m_val = m_val->next;
return *this;
}
//重载*
T operator*()
{
return m_val->node;
}
Node *m_val;
private:
};
CList();
~CList();
//在连表尾部添加
void push_back(const T &_node);
//在连表的头部添加
void push_front(const T &_node);
//删除连表中的最后一个元素
void pop_back();
//删除连表中的第一个元素
void pop_front();
//返回指向第一个元素的迭代器
typename CList<T>::iterator begin();
//返回末尾的迭代器
typename CList<T>::iterator end();
//判断是否为空
bool empty(){ if(!m_phead){ return true; } return false; }
//清空链表
void clear();
protected:
private:
Node *m_phead;
};
////////////////////////////类模板的函数定义///////////////////////
template<typename T>
CList<T>::~CList()
{
//删除链表里的所有结点
if(m_phead)
{
Node* temp = m_phead;
while(temp->next!=NULL)
{
Node* temp1 = temp;
temp = temp->next;
delete temp1;
temp1 = NULL;
}
m_phead = NULL;
}
}
template<typename T>
CList<T>::CList()
{
m_phead = NULL;
}
template<typename T>
void CList<T>::push_back(const T &_node)
{
//如果连表为空
if(empty())
{
Node* tempnode = new Node;
tempnode->node = _node;
tempnode->next = NULL;
m_phead = tempnode;
}
//如果连表不为空
else
{
Node *temp = m_phead;
while(temp->next!=NULL)
{
temp = temp->next;
}
Node* tempnode = new Node;
tempnode->node = _node;
tempnode->next = NULL;
temp->next = tempnode;
}
}
template<typename T>
void CList<T>::push_front(const T &_node)
{
if(empty())
{
Node* tempnode = new Node;
tempnode->node = _node;
tempnode->next = NULL;
m_phead = tempnode;
}
else
{
Node* tempnode = new Node;
tempnode->node = _node;
tempnode->next = m_phead->next; m_phead = tempnode;
}
}
template<typename T>void CList<T>::pop_back()
{
if(empty())
{
return ;
}
else
{
Node* temp = m_phead;
if(!temp->next)
{
delete temp;
m_phead = NULL;
}
else
{
while(temp->next->next!=NULL)
{
temp = temp->next;
}
delete temp->next;
temp->next = NULL;
}
}
}
template<typename T>void CList<T>::pop_front()
{
if(empty())
{
return;
}
else
{
Node* temp = m_phead;
m_phead = m_phead->next;
delete temp;
temp = NULL;
}
}
template<typename T>typename CList<T>::iterator CList<T>::begin()
{
iterator tempit;
tempit.m_val = m_phead;
return tempit;
}
template<typename T>typename CList<T>::iterator CList<T>::end()
{
//如何去释放?
iterator tempit ;
Node* tempnode1 = m_phead;
while(tempnode1!=NULL)
tempnode1 = tempnode1->next;
tempit.m_val = tempnode1;
return tempit;
}
template<typename T>void CList<T>::clear()
{
//删除链表里的所有结点 if(m_phead)
{
Node* temp = m_phead;
while(temp->next!=NULL)
{
Node* temp1 = temp;
temp = temp->next;
delete temp1;
temp1 = NULL;
}
m_phead = NULL;
}
}
#endif