#include <iostream>
#include <stdexcept>
#include <cstring>
using namespace std;
//双链表
template<typename T>
class List{//列表
private:
//双向链表结点
class Node{
public:
T m_elem; //结点元素
Node *m_prev; //前结点
Node *m_next; //下一个节点
public:
Node(const T& elem=T(),Node* prev=NULL,Node* next=NULL):m_elem(elem),m_next(next),m_prev(prev){
}
friend ostream& operator<<(ostream& os,const Node& node){
return os << node.m_elem ;
}
};
private:
Node* m_head;//头指针 指向第一个结点
Node* m_tail;//尾指针 指向最后一个结点
public:
List(void):m_head(NULL),m_tail(NULL){}
//初始化有count个元素的链表
List(size_t count):m_head(NULL),m_tail(NULL){
for(size_t i=0;i<count;i++)
push_back();//插入默认值
}
List(size_t count,const T& t):m_head(NULL),m_tail(NULL){
for(size_t i=0;i<count;i++)
push_back(t);
}
List(const List& that):m_head(NULL),m_tail(NULL){
//遍历that链表 把每个元素都添加到当前链表
for(Node *node = that.m_head; node!=NULL;node = node->m_next){
push_back(node->m_elem);
}
}
//在双向链表末尾压入一个元素
void push_back(const T& elem){
//m_tail本来是指向最后一个结点
m_tail = new Node(elem,m_tail);//新增加的结点 的prev是原来m_tail
//m_tail指向最后一个结点
if(m_tail->m_prev == NULL){//刚刚插入的是第一个结点
m_head = m_tail;
}else{//最后一个结点的前面存在结点
m_tail->m_prev->m_next = m_tail;//让最后一个结点的前一个结点的next指向最后一个结点
}
}
List& operator=(const List& that){
if(this != &that){
List list(that);
::swap(m_head,list.m_head);
::swap(m_tail,list.m_tail);
}
return *this;
}
~List(void){
clear();
}
//清空双向链中所有元素
void clear(void){
/*
for(Node *node=m_head;node!=NULL;){
Node *next = node-m_next;
delete node;
node = next;
}
m_head = NULL;
m_tail = NULL;
*/
for(Node *next; m_head!=NULL; m_head = next){
next = m_head->m_next;
delete m_head;
}
m_tail = NULL;
}
friend ostream& operator<<(ostream& os,const List& list){
for(Node *node=list.m_head;node!=NULL;node=node->m_next)
//os << *node << " ";
os << node->m_elem << " ";
return os << endl;
}
T& front(void){
if(m_head == NULL){
throw underflow_error("链表空");
}
return m_head->m_elem;
}
const T& front(void)const{
if(m_head == NULL){
throw underflow_error("链为空");
}
return m_head->m_elem;
}
T& back(void){
if(m_tail == NULL){
throw underflow_error("链为空");
}
return m_tail->m_elem;
}
const T& back(void)const{
if(m_tail == NULL){
throw underflow_error("链为空");
}
return m_tail->m_elem;
}
bool empty(void)const{
return m_head == NULL && m_tail == NULL;
}
size_t size()const{
size_t cnt = 0;
for(Node *node = m_head;node!=NULL; node=node->m_next,++cnt);
return cnt;
}
void pop_back(void){
if(empty()){
throw underflow_error("链表空");
}
Node *last = m_tail->prev;//记录倒数第二个结点
delete m_tail;//释放最后一个结点的内存
m_tail = last;
if(m_tail == NULL){//没有结点
m_head = NULL; //h_head指向NULL
}else{//还有结点
m_tail->m_next = NULL;//最后一个结点的next=NULL
}
}
//在链表前面插入一个元素
void push_front(const T& elem){
m_head = new Node(elem,NULL,m_head);
if(m_head->m_next == NULL){
m_tail = m_head;
}else{
m_head->m_next->m_prev = m_head;
}
}
void pop_front(void){
if(empty()){
throw underflow_error("链表空");
}
Node *first = m_head->m_next;
delete m_head;
m_head = first;
if(m_head == NULL){
m_tail = NULL;
}else{
m_head->m_prev = NULL;
}
}
void swap(List& other){
if(this != other){
swap(m_head,other.m_head);
swap(m_tail,other.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==(const iterator& it)const{
return m_node == it.m_node; //当前指向的结点一样为相等
}
bool operator!=(const iterator& it)const{
return !(*this == it);
}
iterator& operator++(void){
if(m_node!=NULL)
m_node = m_node->m_next;
else
m_node = m_head;
return *this;
}
const iterator operator++(int){
iterator old = *this;
++*this;
return old;
}
iterator& operator--(void){
if(m_node!=NULL)
m_node = m_node->m_prev;
else
m_node = m_tail;
return *this;
}
const iterator operator--(int){
iterator old = *this;
--*this;
return old;
}
T& operator*(void)const{
return m_node->m_elem;
}
//it->
T* operator->(void)const{
return &m_node->m_elem;
}
private:
Node *m_head;//迭代器开始
Node *m_tail;//迭代器结束
Node *m_node;//当前
friend class List;
};
public:
//正向迭代器首元素
iterator begin(void){
return iterator(m_head,m_tail,m_head);
}
iterator end(void){
return iterator(m_head,m_tail,NULL);
}
//在pos之前插入元素elem
iterator insert(iterator pos,const T& elem){
if(pos == end()){
push_back(elem);
return iterator(m_head,m_tail,m_tail);
}else{
Node *node = new Node(elem,pos.m_node->m_prev,pos.m_node);
if(node->m_prev == NULL){
m_head = node;
}else{
node->m_prev->m_next = node;
}
node->m_next->m_prev = node;
return iterator(m_head,m_tail,node);
}
}
iterator erase(iterator pos){
if(pos == end()){
throw invalid_argument("无效参数");
}
//pos.m_node
if(pos.m_node->m_prev != NULL){//删除不是第一个结点
pos.m_node->m_prev->m_next = pos.m_node->m_next;
}else{//删除第一个结点
m_head = pos.m_node->m_next;
}
if(pos.m_node->m_next != NULL){//删除的不是最后一个结点
pos.m_node->m_next->m_prev = pos.m_node->m_prev;
}else{//删除最后一个结点
m_tail = pos.m_node->m_prev;
}
Node *next = pos.m_node->m_next;
delete pos.m_node;
return iterator(m_head,m_tail,next);
}
};
//vector<int>::iterator it;
int main(){
//int ();
List<int> list;
for(int i=0;i<10;i++)
list.push_back(i);
cout << list << endl;
List<int> l(list);
cout << l << endl;
List<int> l1;
cout << boolalpha << l1.empty() << endl;
l1 = l;
cout << l1 << endl;
l.back() = 1024;
l.front() = 9527;
cout << l << endl;
cout << boolalpha << l.empty() << endl;
cout << l.size() << endl;
l.push_front(11);
l.push_front(1234);
l.push_front(1314);
cout << l << endl;
cout << "迭代器输出:";
List<int>::iterator it;
for(it = l.begin();it != l.end(); ++it){
cout << *it << " ";
}
cout << endl;
it = l.begin();
//it = it+4;
++it;
++it;
++it;
++it;
cout << *it << endl;
l.insert(it,168);
cout << l << endl;
l.insert(l.end(),2222);
cout << l << endl;
l.insert(--l.end(),155);
cout << l << endl;
it = l.erase(l.begin());
cout << *it << endl;
cout << l << endl;
it = l.erase(--l.end());
cout << l << endl;
it = l.begin();
++it;
++it;
++it;
++it;
++it;
cout << *it << endl;
it = l.erase(it);
cout << *it << endl;
cout << l << endl;
return 0;
}
模板实现链表
最新推荐文章于 2024-04-20 20:27:10 发布