闲暇之余仿C++STL的思路写了一个双向链表,现在还只是个雏形,至于分离文件版的等完善了再传上来、、、
在Sublime Text上写的,贴到这边就有点乱,也懒得改了就这样吧!
//双向线性链表
#include<iostream>
#include<stdexcept>
#include<cstring>
#include<string>
using namespace std;
template<typename T>
class List{
public:
//构造
List(void):m_head(NULL),m_tail(NULL){}
//析构
~List(void){
clear();
}
//拷贝构造
List(List const& that):m_head(NULL),m_tail(NULL){
for(Node* node=that.m_head;node;node=node->m_next){
push_back(node->m_data);
}
}
//拷贝赋值
List& operator=(List const& rhs){
if(&rhs!=this){
List list=rhs;
swap(m_head,list.m_head);
swap(m_tail,list.m_tail);
}
return *this;
}
//获取首元素
T& front(void){
if(empty())
throw underflow_error("链表下溢!");
return m_head->m_data;
}
T const& front(void) const{
return const_cast<List*>(this)->front();
}
//向首部压入
void push_front(T const& data){
m_head=new Node(data,NULL,m_head);
if(m_head->m_next)
m_head->m_next->m_prev=m_head;
else
m_tail=m_head;
}
//从首部弹出
void pop_front(void){
if(empty())
throw underflow_error("链表下溢!");
Node* next=m_head->m_next;
delete m_head;
m_head=next;
if(m_head)
m_head->m_prev=NULL;
else
m_tail=NULL;
}
//获取尾元素
T& back(void){
if(empty())
throw underflow_error("链表下溢!");
return m_tail->m_data;
}
T const& back(void) const{
return const_cast<List*>(this)->back();
}
//向尾部压入
void push_back(T const& data){
m_tail=new Node(data,m_tail);
if(m_tail->m_prev)
m_tail->m_prev->m_next=m_tail;
else
m_head=m_tail;
}
//从尾部弹出
void pop_back(void){
if(empty())
throw underflow_error("链表下溢!");
Node* prev=m_tail->m_prev;
delete m_tail;
m_tail=prev;
if(m_tail)
m_tail->m_next=NULL;
else
m_head=NULL;
}
//删除所有匹配元素
void remove(T const& data){
for(Node* node=m_head,*next;node;node=next){
next=node->m_next;
if(equal(node->m_data,data)){
if(node->m_prev)
node->m_prev->m_next=node->m_next;
else
m_head=node->m_next;
if(node->m_next)
node->m_next->m_prev=node->m_prev;
else
m_tail=node->m_prev;
delete node;
}
}
}
//清空
void clear(void){
for(Node* next;m_head;m_head=next){
next=m_head->m_next;
delete m_head;
}
m_tail=NULL;
}
//判空
bool empty(void) const{
return !m_head&&!m_tail;
}
//大小
size_t size(void) const{
size_t nodes=0;
for(Node* node=m_head;node;node=node->m_next){
++nodes;
}
return nodes;
}
//输出
friend ostream& operator<<(ostream& os,List const& list){
for(Node* node=list.m_head;node;node=node->m_next)
os<<*node;
return os;
}
private:
//节点
class Node{
public:
Node(T const& data,Node* prev=NULL,Node* next=NULL):m_data(data),m_prev(prev),m_next(next){}
friend ostream& operator<<(ostream& os,Node const& node){
return os<<'('<<node.m_data<<')';
}
T m_data;//节点中保存的数据
Node* m_prev;//前指针
Node* m_next;//后指针
};
//通用版本相等性判断
bool equal(T const& x,T const& y) const{
return x==y;
}
Node* m_head;//头指针
Node* m_tail;//尾指针
public:
//正向迭代器
class Iterator{
public:
Iterator(Node* head=NULL,Node* tail=NULL,Node* node=NULL):m_head(head),m_tail(tail),m_node(node){}
bool operator==(Iterator const& it) const{
return m_node==it.m_node;
}
bool operator!=(Iterator const& it) const{
return !(*this==it);
}
Iterator& operator++(void){
if(m_node)
m_node=m_node->m_next;
else
m_node=m_head;
return *this;
}
Iterator const operator++(int){
Iterator old=*this;
++*this;
return old;
}
Iterator& operator--(void){
if(m_node)
m_node=m_node->m_prev;
else
m_node=m_tail;
return *this;
}
Iterator const operator--(int){
Iterator old=*this;
--*this;
return old;
}
T& operator*(void) const{
return m_node->m_data;
}
T* operator->(void){
return &**this;
}
private:
Node* m_head;
Node* m_tail;
Node* m_node;
friend class List;
};
//获取起始正向迭代器
Iterator begin(void){
return Iterator (m_head,m_tail,m_head);
}
//获取终止正向迭代器
Iterator end(void){
return Iterator (m_head,m_tail);
}
//插入
Iterator insert(Iterator loc,T const& data){
if(loc==end()){
push_back(data);
return Iterator(m_head,m_tail,m_tail);
}
else{
Node* node=new Node(data,loc.m_node->m_prev,loc.m_node);
if(node->m_prev)
node->m_prev->m_next=node;
else
m_head=node;
node->m_next->m_prev=node;
return Iterator(m_head,m_tail,node);
}
}
//删除
Iterator erase(Iterator loc){
if(loc==end())
throw invalid_argument("无效参数!");
if(loc.m_node->m_prev)
loc.m_node->m_prev->m_next=loc.m_node->m_next;
else
m_head=loc.m_node->m_next;
if(loc.m_node->m_next)
loc.m_node->m_next->m_prev=loc.m_node->m_prev;
else
m_tail=loc.m_node->m_prev;
Node* next=loc.m_node->m_next;
delete loc.m_node;
return Iterator(m_head,m_tail,next);
}
//常正向迭代器
//反向迭代器
//常反向迭代器
};
//针对char const*类型的成员特化
template<>
bool List<char const*>::equal(char const* const& x,char const* const& y)const{
return !strcmp(x,y);
}
//线性查找
template<typename IT,typename T>
IT find(IT begin,IT end,T const& key){
IT it;
for(it=begin;it!=end;++it)
if(*it==key)
break;
return it;
}