您的位置:首页 > 其它

list使用与简单实现

2014-08-31 22:11 330 查看
1.首先我们看看怎么使用
#include"stdafx.h"
#include<iostream>
#include<list>
#include<algorithm>
usingnamespacestd;

int_tmain(intargc,_TCHAR*argv[])
{
list<int>iList;
iList.push_back(1);
iList.push_back(2);
iList.push_back(7);
iList.push_back(4);
iList.push_back(5);
iList.push_back(3);
iList.push_back(10);
cout<<iList.size()<<endl;
//相对于vector来说list是没有capacity的(使用的是指针)

list<int>::iteratorit;
for(it=
iList.begin();it!=
iList.end();++it)
cout<<*it<<"
";
cout<<endl;

it=
find(iList.begin(),iList.end(),5);
//找到第五个位置的泛型指针
iList.insert(it,1000);

for(it=
iList.begin();it!=
iList.end();++it)
cout<<*it<<"
";
cout<<endl;

it=
find(iList.begin(),iList.end(),1);
//find不是list的类函数
iList.erase(it);

for(it=
iList.begin();it!=
iList.end();++it)
cout<<*it<<"
";
cout<<endl;

iList.pop_back();
//减少一个数据
iList.pop_back();

for(it=
iList.begin();it!=
iList.end();++it)
cout<<*it<<"
";
cout<<endl;

getchar();
return0;
}

2.简单的封装list
List的源代码比vector的难度要打一些,其中涉及到指针,链表(双向循环链表)
只是简单的封装了几个函数(参考数据结构)
#include"stdafx.h"
#include<iostream>
usingnamespacestd;

template<classT>
classListNode//定义节点类
{
public:
typedefListNode<T>*pListNode;
private:
pListNodeNext;
Tvalue;
public:
ListNode(constT&val,pListNodeN=NULL):value(val),Next(N){};
~ListNode(){};
ListNode():Next(NULL){};
};

template<classT>
classList;
//前置声明
template<classT>
classListIterator
{
public:
typedefListNode<T>*pListNode;
typedefListNode<T>&rListNode;
typedefList<T>*pList;
typedefList<T>&rList;

private:
constpListNodehead;
pListNodepCur;
//当前的节点

public:
ListIterator():head(NULL)
{
if(pCur)
pCur=
head;
pCur=
head->Next;
}
~ListIterator(){};

intFind(constT&val)
{
pListNodep=
head->Next;
while(p!=NULL&&
!(
p->value==val))p=
p->Next;
if(p==
NULL)return0;
pCur=
p;
return1;
}

voidinsert(constT&val)
{
pListNodep;
p=
newListNode<T>(val,pCur->next);
pCur=
pCur->Next=
p;
}

voidIsFound(constT&val)
{
pListNodep=
head->Next;
while(p!=NULL&&
!(
p->value==val))p=
p->Next;
returnp!=
NULL;
}

intremove(constT&val)
{
pListNodep=
head;
while(p->Next!=NULL&&!(p->value==val))p=p->Next;
if(p->Next==NULL)
return0;
pListNodepL=
p->Next;
p->Next=
p->Next->Next;
deletepL;
pCur=
head;
return1;
}

constListIterator&operator=(constListIterator&it)
{
if(this==&it)
return*this;
pCur=
it.pCur;
return*this;
}
voidoperator++()
{
pCur=
pCur->Next;
}
};

template<classT>
classList
{
public:
typedefListNode<T>*pListNode;
typedefListNode<T>rListNode;
typedefListIterator<T>lt;
pListNodehead;
//定义头结点
public:
List()
{
head=
newrListNode();
}
~List()
{
deleteAllNode();
deletehead;
head->Next=
NULL;
}
voiddeleteAllNode()
//循环删除所有节点
{
pListNodep;
pListNodepNext;
for(p=head->Next;p!=
NULL;p=pNext)
{
pNext=
p->Next;
deletep;
}
}
constList&operator=(constList&src)
{
if(this==&src)
return*this;
deleteAllNode();
pListNodep=head;
pListNodepSrc=
src.head;
ListIterator<T>it(*this);
while(pSrc!=NULL)
{
it.insert(pSrc->value);
pSrc=
pSrc->Next;
}
return*this;
}
intIsEmpty()const
{
return(head->Next==
NULL);
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: