//List链表与其可用的迭代器组合的一种简单实现
#include <iostream>
using namespace std;
//elemType为节点实际保持的数据类型
template<typename elemType>
class list_item
{
public:
list_item(elemType val,list_item<elemType> *p_item = NULL):_val(val)//初始化
{
if(!p_item) _next = NULL;
else
{
this->_next=p_item->_next;
p_item->_next=this;//插在参数p_item的后面
}
}
//默认的构造函数 ,必须要有 //系统会自动调用类成员变量的默认构造函数
list_item(){_next=0;}
elemType getval();
//返回引用,用于迭代器,可以直接改变elemType 类型的值
elemType & referval()
{
return _val;
}
list_item<elemType> * getnext()
{
return this->_next;
}
void setval(const elemType &val)
{
this->_val=val;
}
//这个函数与setval的含义相同
void setnext(list_item<elemType> *next)
{
this->_next=next;
}
virtual ~list_item()
{
//如果 _val是类类型,系统会自动调用elemtype的析构函数;
}
private:
elemType _val;
list_item *_next;
};
template<class elemType>
elemType list_item<elemType>::getval()
{
return this->_val;
}
//为List设计的迭代器类 //如果定义在List类的里面 ,好处是确保类型一致。为了清晰,
//在外面定义
template<class elemType>
class Iterator
{
public:
Iterator(list_item<elemType> *p_item = NULL)//唯一的构造函数
{
this->p_item=p_item;
}
elemType & operator*() // Iterator 类型的变量x,重载如下类型 *x,那么返回节点实际变量的引用,含义类似于通常的指针
{
return (p_item->referval());
}
Iterator<elemType>& operator++()//前置的增一运算可以返回引用
{
p_item = p_item->getnext();
return (*this);
}
Iterator<elemType> operator++(int)//后置的增一运算必须返回临时变量
{
Iterator tm=(*this);
++(*this);
return tm;
}
bool operator==(const Iterator& elm)const
{
return (this->p_item == elm.p_item);
}
bool operator!=(const Iterator& elm)const
{
return (this->p_item != elm.p_item);
}
list_item<elemType> *get()
{
return p_item;
}
void set(list_item<elemType> *pit)
{
p_item = pit;
}
private:
list_item<elemType> *p_item;
};
//将迭代器与链表分开设计
//单链表的List //不带头结点
template<class elemType>
class list
{
public:
list<elemType>():_head(NULL),_end(NULL),_current(NULL),_size(0){}//唯一的一个构造函数
//定义拷贝构造函数
list<elemType>(const list<elemType>&listed)
{
this->remove_all();
if(listed._size == 0)
return;
else
copylist(listed);
}
const list &operator=(const list<elemType>& listed)
{
remove_all();
if(listed._size==0) return (*this);
else
{
copylist(listed);
return listed;
}
}
virtual ~list(){remove_all();}
void remove_all()
{
if(0 == this->_size)
return;
else{
list_item<elemType>* q , *p = _head;
for(int i=0;i < this->_size;++i)
{
q=p->getnext();
delete p;
p = q;
}
_head = _end = _current = NULL;
this->_size=0;
return;
}
}
int size(){return _size;}
void push_back(const elemType &elem)
{
if(_size == 0){
_head = _end = new list_item<elemType>(elem);
_size = 1;
}
else{
list_item<elemType> *tm=new list_item<elemType>(elem,_end);
_end=tm;
++_size;
}
}
void pop_back()//NULL表示返回失败
{
if(_size==0)
return ;
else
{
list_item<elemType> *p=_head;
for(int i=0;i<_size-2;++i)
{
p = p->getnext();
}
delete _end;
_end = p;/*可能 _head 和 _end 指向无效内存,但是_size是正确的,其他函数行为依据_size的值*/ _size--;
}
}
elemType get_back()
{
if(_size != 0)
return _end->getval();
}
// 这里需要区分_size的值
Iterator<elemType> begin()
{
if(_size == 0)
return (Iterator<elemType>(NULL));
else
return (Iterator<elemType>(_head));
}
Iterator<elemType> end()
{
return (Iterator<elemType>(NULL));
}
//插入迭代器所指元素的后面 //迭代器必须有效
void insert(Iterator<elemType> & it, const elemType & lem)
{
new list_item<elemType>(lem,it.get());
this->_size++;
}
private:
void copylist(const list<elemType>&listed)//内部调用,需判断至少存在一个节点才可以调用
{
list_item<elemType>*p,*q,*r;
this->_size = listed._size;
p = _end = _head = new list_item<elemType>(listed._head->getval());
q = listed._head->getnext();
for(int i=0;i < _size-1;++i)
{
r = new list_item<elemType>(q->getval(),p);
q = q->getnext();
this->_end = p = r;
}
}
list_item<elemType>* _head;
//指向最后一个节点
list_item<elemType>* _end;
//这个指针还未使用
list_item<elemType>* _current;// 暂时没有用到;
int _size;
};
int main()
{
list<int> mlist;
int tem;
int i;
for( i=0;i<10;i++)
mlist.push_back(i);
mlist.remove_all();
for( i=0;i<10;i++)
mlist.push_back(i);
cout<<"size="<<mlist.size()<<endl;
list<int> mlist2;//不能在定义的时候调用拷贝函数初始化
mlist2=mlist;
cout<<"mlist2._size="<<mlist2.size()<<endl;
cout<<endl<<endl;
Iterator<int> itorb=mlist2.begin();//定义一个类型一致的迭代器
Iterator<int> itore=mlist2.end();
for(;itorb != itore; itorb++)
cout<<*itorb<<" ";
cout<<endl;
system("pause");
return 0;
}
List链表容器封装
最新推荐文章于 2023-08-02 09:30:00 发布