#ifndef LIST_H
#define LIST_H
#include<iostream>
using namespace std;
template<class T>
class List
{
private:
struct Node//双向结点声明
{
T data;
Node *prev, *next;
Node(T d = 0, Node *p = nullptr, Node *n = nullptr) :data(d), prev(p), next(n){}
};
Node* head;//头结点指针
Node* tail;//尾结点指针
int size;//数据个数
void init()//初始化
{
head = new Node;
tail = new Node;
size = 0;
head->next = tail;
tail->prev = head;
}
public:
class const_iterator
{
protected:
Node* current;
T& retrive()const{ return current->data; }
explicit const_iterator(Node *p) :current(p){}//转换构造函数
friend class List<T>;
public:
explicit const_iterator() :current(nullptr) {}
const T&operator*()const{ return retrive(); }
const_iterator&operator ++()//前++
{
current = current->next;
return *this;
}
const_iterator operator ++(int)//后++
{
const_iterator old = *this;
++(*this);
return old;
}
const_iterator&operator --()//前--
{
current = current->prev;
return *this;
}
const_iterator operator --(int)//后--
{
const_iterator old = *this;
--(*this);
return old;
}
bool operator==(const const_iterator& rhs)const
{
return current == rhs.current;
}
bool operator!=(const const_iterator& rhs)const
{
return current != rhs.current;
}
};
class iterator :public const_iterator
{
protected:
explicit iterator(Node *p) :const_iterator(p){}
friend class List<T>;
public:
explicit iterator(){};
T& operator*()
{
return const_iterator::retrive();
}
const T&operator *()const
{
return const_iterator::operator*();
}
iterator& operator++()
{
const_iterator::current = const_iterator::current->next;
return *this;
}
iterator operator++(int)
{
iterator old = (*this);
++(*this);
return old;
}
iterator& operator--()
{
const_iterator::current = const_iterator::current->prev;
return *this;
}
iterator operator--(int)
{
iterator old = *this;
--(*this);
return old;
}
};
List(){ init(); }///默认构造函数,生成空表
List(const List<T>&l)//复制构造
{
init();
operator=(l);
}
const List&operator=(const List& L);//转换构造
iterator Begin() { return iterator(head->next); }
const_iterator Begin()const { return const_iterator(head->next); }
iterator End() { return iterator(tail); }
const_iterator End()const { return const_iterator(tail); }
T& front() { return *Begin(); }//取首元素的引用
const T& front()const { return *Begin(); }
T& back() { return *(--End()); }//取尾元素的引用
const T& back()const { return *(--End()); }
void push_front(const T& item)//首插
{
Insert(Begin(), item);
}
void push_back(const T& item)//尾插
{
Insert(End(), item);
}
void pop_front()//删除数据首结点
{
Erase(Begin());
}
void pop_back()//删除数据尾结点
{
Erase(--End());
}
iterator Erase(iterator itr);//删除迭代器所指结点
iterator Insert(iterator itr, const T& item);//在迭代器位置插入item
int Size()const { return size; }//取结点个数
bool empty()const { return size == 0; }//判空
void clear(){ while (!empty()){ pop_front(); } }//清表
};
template<class T>
typename List<T>::iterator List<T>::Insert(iterator itr, const T& item)
{
Node *p = itr.current;
p->prev->next = new Node(item, p->prev, p);
p->prev = p->prev->next;
size++;
return iterator(p->prev);
}
template<class T>
typename List<T>::iterator List<T>::Erase(iterator itr)
{
Node *p = itr.current;
iterator re(p->next);
p->prev->next = p->next;
p->next->prev = p->prev;
delete p;
size--;
return re;
}
template<class T>
const List<T>& List<T>::operator=(const List<T>& L)
{
clear();
for (const_iterator itr = L.Begin(); itr != L.End(); ++itr)
{
push_back(*itr);
}
return *this;
}
#endif
C++ 面向对象-链表类模板的实现
最新推荐文章于 2023-07-01 23:54:13 发布