C++写的一个List<T>
2010-08-02 18:00
253 查看
#include "stdafx.h"
#include <iostream>
using namespace std;
template<typename T>
struct Node
{
T value;
Node* next;
Node* pre;
};
template<typename T>
class List
{
private:
Node<T>* head;
Node<T>* current;
Node<T>* create;
int number ;
public:
List()
{
head=new Node<T>();
head->next=NULL;
head->pre=NULL;
current=head;
number=0;
}
~List(){
Node<T> * temp=current;
while (temp->next!=NULL)
{
temp=temp->next;
delete(temp->pre);
}
if (temp->next==NULL)
{
delete(temp);
}
cout<<"析构完成"<<endl;
}
void Add(T a)
{
create=new Node<T>();
create->value=a;
create->next=current;
create->pre=NULL;
current->pre=create;
current=create;
number++;
}
bool Contains(T a){
Node<T> * temp=current;
bool ret=false;
while (temp->next!=NULL)
{
if (temp->value==a)
{
ret=true;
break;
}
temp=temp->next;
}
return ret;
}
void Remove(T a){
Node<T> * temp=current;
while (temp->next!=NULL)
{
if (temp->value==a)
{
number--;
if (temp->pre!=NULL)
{
temp->pre->next=temp->next;
temp->next->pre=temp->pre;
delete(temp);
}
else
{
current=current->next;
delete(temp);
}
break;
}
else
{
temp=temp->next;
}
}
}
void Display()
{
Node<T> * temp=current;
while (temp->next!=NULL)
{
cout<<temp->value<<endl;
temp=temp->next;
}
}
int getNumber(){
return number;
}
};
void Test()
{
List<int> *p=new List<int>();
for (int i=0;i<3553;i++)
{ p->Add(i);
}
p->Display();
cout<<"个数:"<<p->getNumber()<<endl;
getchar();
cout<<p->Contains(333)<<endl;
delete(p);
}
int main()
{
Test();
getchar();
return 0;
}
#include <iostream>
using namespace std;
template<typename T>
struct Node
{
T value;
Node* next;
Node* pre;
};
template<typename T>
class List
{
private:
Node<T>* head;
Node<T>* current;
Node<T>* create;
int number ;
public:
List()
{
head=new Node<T>();
head->next=NULL;
head->pre=NULL;
current=head;
number=0;
}
~List(){
Node<T> * temp=current;
while (temp->next!=NULL)
{
temp=temp->next;
delete(temp->pre);
}
if (temp->next==NULL)
{
delete(temp);
}
cout<<"析构完成"<<endl;
}
void Add(T a)
{
create=new Node<T>();
create->value=a;
create->next=current;
create->pre=NULL;
current->pre=create;
current=create;
number++;
}
bool Contains(T a){
Node<T> * temp=current;
bool ret=false;
while (temp->next!=NULL)
{
if (temp->value==a)
{
ret=true;
break;
}
temp=temp->next;
}
return ret;
}
void Remove(T a){
Node<T> * temp=current;
while (temp->next!=NULL)
{
if (temp->value==a)
{
number--;
if (temp->pre!=NULL)
{
temp->pre->next=temp->next;
temp->next->pre=temp->pre;
delete(temp);
}
else
{
current=current->next;
delete(temp);
}
break;
}
else
{
temp=temp->next;
}
}
}
void Display()
{
Node<T> * temp=current;
while (temp->next!=NULL)
{
cout<<temp->value<<endl;
temp=temp->next;
}
}
int getNumber(){
return number;
}
};
void Test()
{
List<int> *p=new List<int>();
for (int i=0;i<3553;i++)
{ p->Add(i);
}
p->Display();
cout<<"个数:"<<p->getNumber()<<endl;
getchar();
cout<<p->Contains(333)<<endl;
delete(p);
}
int main()
{
Test();
getchar();
return 0;
}
相关文章推荐
- C++中输入输出<< 和>>重载,以便适应输出输入一个对象
- 当需要对一个list<Map<String,Object>>中的map添加一个相同的数据是可以参考
- 对从c++中向qml中导入list<qobject*>的操作的深入学习
- hibernate多生成一个外键以及映射文件中含有<list-index>标签
- c/c++学习之c++ 中的list <>容器
- 改变一个list<Map<String,Object>>,并给他重新赋值,不能使用新循环,要使用旧循环
- 复制一个List<T>对象
- List<Entity>排序:根据javabean中的一个字段对beanList进行排序
- fltk在Ubuntu Linux里面的搭建和测试--<<C++程序设计原理与实践>>Chapter12:一个显示模型 环境构建篇
- C++ Greedy Snake的OOP实现 贪食蛇 <list> STL初次学习
- 将一个LIST分组,转为map<string,list>类型
- SqlQueryToList 执行SQL语句并把结果返回成一个List<dynamic>
- c++ 一个关于计时器的源代码 <转载>
- 判定一个list<int>中的元素和一个vector<int>中的元素是否相等
- 类型转换——如何将一个List<Map<String,String>>类型换成json格式
- List<Integer>.remove()的一个小细节
- 判定一个list<int>中的元素和一个vector<int>中的元素是否相等
- MVC分页控件之二,为IQueryable定义一个扩展方法,直接反回PagedList<T>结果集
- ArrayList<Integer> list = new ArrayList<Integer>(); 在这个泛型为Integer的ArrayList中存放一个String类型的对象。
- Java List<Object>根据Object中某一个属性排序