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);
}
};
#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);
}
};
相关文章推荐
- Python 使用list实现简单的set
- C#使用泛型数组简单实现一个列表List
- Python 使用list实现简单的堆栈
- Java基础课程学习总结,使用LinkedList简单模拟队列数据结构和堆栈数据结构的实现
- Python 使用list实现简单的map
- 使用Redis List简单实现抢红包
- Python 使用list实现简单的集合set
- 使用list实现一个简单的Listener管理
- 使用go语言的list实现一个简单的LRU缓存
- 使用异或进行简单的密码加密(JAVA实现)
- .NET中使用datagrid实现的简单分页效果
- .NET中使用datagrid实现的简单分页效果
- 使用Ado.Net进行简单事务处理的四种实现及比较
- 使用低级界面实现简单的数字输入
- Asp.Net使用POST方法最简单的实现
- Linux下C语言实现的简单使用线程向FIFO里写入与读取数据的例子
- 使用System.DirectoryServices.Protocols实现对AD的简单操作
- Spring--简单使用quartz实现定时作业
- Spring--简单使用quartz实现定时作业
- 一个使用监听器模式实现的J2ME网络编程框架,包括一个简单的登录功能实现(含源代码)