仿std中的list链表类实现的链表类

最近在学数据结构,不正确的地方请指出,谢谢!

#pragma once
// [5/16/2012 Administrator]
// [5/17/2012 Administrator]

template <class Object>
class ListClass
{
public:
struct Node{
Object value;
Node* head;
Node* next;

Node(const Object& d=Object(), Node* p=NULL,Node* n=NULL):value(d),head(p),next(n){}
friend class const_itrator;
friend class itrator;

};
public:
class const_itrator{
public:
const_itrator():current(NULL){};
~const_itrator(){};
public:
Node* current;
Object& retrieve()const
{ return this->current->value;};
const_itrator( Node* p ):current(p){};
public:
const Object& operator*()const
{ return retrieve();};
const_itrator& operator++()
{
if (this->current->next!=NULL)
{
this->current=this->current->next;
return (*this);
}
else
{
return *this;
}
};
const_itrator operator++(int)
{
const_itrator old=*this;
++(*this);
return old;
};
const_itrator& operator--()
{
if (this->current->head->head!=NULL)
{
this->current=this->current->head;
return (*this);
}
else
{
return *this;
}
};
const_itrator operator--(int)
{
const_itrator old=*this;
--(*this);
return old;
};

bool operator==(const const_itrator &rhs)
{
return this->current==rhs.current;
};
bool operator!=(const const_itrator &rhs)
{
return !(*this==rhs);
};
friend class ListClass<Object>;
};
class itrator:public const_itrator{
public:
itrator():const_itrator(){};
~itrator(){};
itrator(Node* p):const_itrator(p){};
public:
Object& operator*()
{ return const_itrator::retrieve(); };
const Object& operator*()const
{
return const_itrator::operator*();
};
itrator& operator++()
{
if (this->current->next!=NULL)
{
this->current=this->current->next;
return (*this);
}
else
{
return *this;
}

};
itrator operator++(int)
{
itrator old=*this;
++(*this);
return old;
};
itrator& operator--()
{
if (this->current->head->head!=NULL)
{
this->current=this->current->head;
return (*this);
}
else
{
return *this;
}

};
itrator operator--(int)
{
itrator old=*this;
--(*this);
return old;
};
friend class ListClass<Object>;
};
private:
Node* pBegin;
Node* pEnd;
int num;
public:
ListClass(void):pBegin(),pEnd()
{
pBegin=new Node(Object(),NULL,NULL);
pEnd=new Node(Object(),pBegin,NULL);
pBegin->next=pEnd;
num=0;
}
ListClass(ListClass& imp):pBegin(),pEnd()
{
/*cout<<"copy start:"<<endl;*/
pBegin=new Node(Object(),NULL,NULL);
pEnd=new Node(Object(),pBegin,NULL);
pBegin->next=pEnd;
num=0;
for (int i=1;i<=imp.size();i++)
{
this->push_back(imp.at(i));
}

}

~ListClass(void)
{
clear();
delete pBegin;
delete pEnd;
}
// [5/17/2012 Administrator]
int operator=( ListClass& imp)
{
/*cout<<"operate= start "<<endl;*/
this->clear();
for (int i=1;i<=imp.size();i++)
{
this->push_back(imp[i]);
}
return 1;
}




//************************************
// Method: push_back
// FullName: ListClass<Object>::push_back
// Access: public
// Returns: itrator
// Qualifier:向链表的尾部插入元素
// Parameter: const Object & obj
//************************************
itrator push_back(const Object& obj)
{
Node* p=new Node(obj,NULL,NULL);
p->head=pEnd->head;
p->next=pEnd;
pEnd->head=p;
p->head->next=p;
num++;
return itrator(p);

}

//************************************
// Method: push_front
// FullName: ListClass<Object>::push_front
// Access: public
// Returns: itrator
// Qualifier:向链表的头部插入元素
// Parameter: const Object & obj
//************************************
itrator push_front(const Object& obj)
{
Node* p=new Node(obj,NULL,NULL);
p->next=pBegin->next;
p->next->head=p;
p->head=pBegin;
pBegin->next=p;
num++;
return itrator(p);

}

//************************************
// Method: remove
// FullName: ListClass<Object>::remove
// Access: public
// Returns: itrator
// Qualifier:删除第n个元素
// Parameter: int n
//************************************
itrator remove(int n)
{
Node* p=search(n).current;
p->head->next=p->next;
p->next->head=p->head;
delete p;
num--;
return itrator();
}


//************************************
// Method: search
// FullName: ListClass<Object>::search
// Access: public
// Returns: itrator
// Qualifier:得到第n个元素的迭代器
// Parameter: int n
//************************************
itrator search(int n)
{
if (n==num+1)
{
return itrator(pEnd);
}
if (n<1||n>num)
{
return itrator();
}
itrator p(pBegin->next);
for (int i=1;i<n;i++)
{
++p;
}
return p;
}

//************************************
// Method: insert
// FullName: ListClass<Object>::insert
// Access: public
// Returns: itrator
// Qualifier:插入元素为第n位
// Parameter: const Object & obj
// Parameter: int n
//************************************
itrator insert(const Object& obj,int n)
{
itrator p=search(n);
Node* pTemp=new Node(obj,NULL,NULL);
pTemp->head=p.current->head;
pTemp->head->next=pTemp;
p.current->head=pTemp;
pTemp->next=p.current;
num++;
return itrator(pTemp);

}
//************************************
// Method: clear
// FullName: ListClass<Object>::clear
// Access: public
// Returns: void
// Qualifier:清空链表
//************************************
void clear()
{
while (num)
{
remove(num);
}

}

//************************************
// Method: size
// FullName: ListClass<Object>::size
// Access: public
// Returns: int
// Qualifier: const
//************************************
int size()const
{ return num; }
Object& at(const int n)
{
return *(search(n));
}

Object& operator[](const int n)
{
if (n<1||n>num)
{
cout<<"error "<<endl;
return this->at(1);
}
return this->at(n);

}

itrator pop_front()
{
Node* p=pBegin->next->next;
remove(1);
num--;
return itrator(p);
}
itrator pop_back()
{
Node* p=pEnd->head->head;
remove(num);
num--;
return itrator(p);
}
itrator begin()const
{
return itrator(pBegin->next);
}
itrator end()const
{
return itrator(pEnd);
}

public:
friend class const_itrator;
friend class itrator;

};



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值