#include<iostream> using namespace std; template <class T> class Node{ public: Node<T>* next; Node<T>* prev; T data; Node() { next = nullptr; prev = nullptr; } Node(T x) { next = nullptr; prev = nullptr; data = x; } }; template <class T> class List { private: Node<T>* head; Node<T>* tail; int length; class list_iterator { private: Node<T>* ptr; public: list_iterator(Node<T>* p = nullptr) :ptr(p) {} //各运算符重载 T& operator*() const { return ptr->data; } Node<T>* operator->() const { return ptr; } list_iterator& operator++() { return ptr = ptr->next, *this; } list_iterator& operator++(int); list_iterator& operator--() { return ptr = ptr->prev, *this; } list_iterator& operator--(int); bool operator==(const list_iterator& t) const { return t.ptr == ptr; } bool operator!=(const list_iterator& t) const { return t.ptr != ptr; } }; //内置判断函数,用来简化重载判断符 //0:相同,1:大于,2:小于,3:大于 int judeg(List<T>& x); public: using iterator = list_iterator; List(); //构造函数 ~List(); //析构函数 void insert(const T x, int idx); //在idx插入元素 iterator insert(const T x, iterator idx); //在迭代器处插入元素,返回插入处迭代器 void push_back(const T x); //在末尾插入元素 void push_front(const T x); //在头部插入元素 iterator erase(iterator idx); //删除迭代器处元素,返回删除元素的后一个迭代器 void erase(int idx); //删除索引元素 void pop_back(); //删除末尾元素 void pop_front(); //删除头部元素 void clear(); //清空链表 Node<T>* get_node(int x); //获取索引处节点 T back()const; //返回最后一个元素 T front()const; //返回第一个元素 int size()const; //返回list中的元素个数 int search(const T x)const; //查找指定元素,存在则输出索引,不存在输出-1 iterator find(const T x); //查找指定元素并返回迭代器 iterator end(); //返回链表尾部迭代器 iterator begin(); //返回链表头部迭代器 iterator gethead(); //返回头节点 bool empty()const; //判断是否为空 T& operator[](int x); List<T>& operator=(List<T>& x); bool operator==(List<T>& x); bool operator!=(List<T>& x); bool operator<(List<T>& x); bool operator>(List<T>& x); bool operator<=(List<T>& x); bool operator>=(List<T>& x); }; template <class T> typename List<T>::list_iterator& List<T>::list_iterator::operator++(int) { Node<T>* tmp = ptr; ptr = ptr->next; auto x = list_iterator(tmp); return x; } template <class T> typename List<T>::list_iterator& List<T>::list_iterator::operator--(int) { Node<T>* tmp = ptr; ptr = ptr->prev; return list_iterator(tmp); } template <class T> List<T>::List() { head = new Node<T>; tail = new Node<T>; head->next = tail; tail->prev = head; length = 0; } template <class T> List<T>::~List() { clear(); delete head; delete tail; } //在idx插入元素 template <class T> void List<T>::insert(const T x, int idx) { Node<T>* tmp = new Node<T>(x); Node<T>* p = get_node(idx); p = p->prev; tmp->prev = p; p->next->prev = tmp; tmp->next = p->next; p->next = tmp; length++; } //在迭代器处插入元素,返回插入处迭代器 template <class T> typename List<T>::iterator List<T>::insert(const T x, iterator idx) { Node<T>* tmp = new Node<T>(x); idx->prev->next = tmp; idx->next->prev = tmp; tmp->next = idx->next; tmp->prev = idx->prev; length++; return iterator(tmp); } //在末尾插入元素 template <class T> void List<T>::push_back(const T x) { Node<T>* tmp = new Node<T>(x); tail->prev->next = tmp; tmp->prev = tail->prev; tail->prev = tmp; tmp->next = tail; length++; } //在头部插入元素 template <class T> void List<T>::push_front(const T x) { Node<T>* tmp = new Node<T>(x); head->next->prev = tmp; tmp->next = head->next; tmp->prev = head; head->next = tmp; length++; } //删除末尾元素 template <class T> void List<T>::pop_back() { if (empty())throw out_of_range("链表为空"); Node<T>* tmp = tail->prev; tail->prev = tmp->prev; delete tmp; length--; } //删除头部元素 template <class T> void List<T>::pop_front() { if (empty())throw out_of_range("链表为空"); Node<T>* tmp = head->next; head->next = tmp->next; delete tmp; length--; } //删除索引元素 template <class T> void List<T>::erase(int idx) { Node<T>* tmp = get_node(idx); tmp->prev->next = tmp->next; tmp->next->prev = tmp->prev; delete tmp; length--; } //删除迭代器处元素,返回删除元素的后一个迭代器 template <class T> typename List<T>::iterator List<T>::erase(iterator idx) { Node<T>* tmp = idx->next; Node<T>* dead = idx->next->prev; idx->prev->next = idx->next; idx->next->prev = idx->prev; delete dead; length--; return iterator(tmp); } //清空链表 template <class T> void List<T>::clear() { while (!empty()) pop_back(); } //返回最后一个元素 template <class T> T List<T>::back()const { if (empty())throw out_of_range("链表为空"); return tail->prev->data; } //返回第一个元素 template <class T> T List<T>::front() const { if (empty())throw out_of_range("链表为空"); return head->next->data; } //查找指定元素,存在则输出索引,不存在输出-1 template <class T> int List<T>::search(const T x)const { Node<T>* tmp = head->next; int i = 0; while (tmp != tail) { if (tmp->data == x) return i; tmp = tmp->next; i++; } return -1; } //查找指定元素并返回迭代器 template <class T> typename List<T>::iterator List<T>::find(const T x) { List<T>::iterator it; for (it = begin(); it != end(); ++it) { if (*it == x) { return it; } } return it; } //返回list中的元素个数 template <class T> int List<T>::size() const { return length; } //判断是否为空 template <class T> bool List<T>::empty()const { return length == 0 ? true : false; } //获取索引处节点 template <class T> Node<T>* List<T>::get_node(int x) { Node<T>* tmp; if (x >= length) throw out_of_range("链表越界"); if (x < length / 2) { tmp = head->next; for (int i = 0; i < x; i++) { tmp = tmp->next; } } else { tmp = tail->prev; for (int i = length - 1; i > x; i--) { tmp = tmp->prev; } } return tmp; } //返回链表尾部迭代器 template <class T> typename List<T>::iterator List<T>::end() { return iterator(tail); } //返回链表头部迭代器 template <class T> typename List<T>::iterator List<T>::begin() { return iterator(head->next); } //返回头节点 template <class T> typename List<T>::iterator List<T>::gethead() { return iterator(head); } //内置判断函数,用来简化重载判断符 //0:相同,1:大于,2:小于,3:大于 template <class T> int List<T>::judeg(List<T>& x) { if (this->size() > x.size())return 1; else if (this->size() < x.size())return 2; auto i = this->begin(); auto j = x.begin(); while (i != this->end()) { if (*i > *j) { return 1; } else if (*i < *j) { return 2; } ++i; ++j; } return 0; } template <class T> T& List<T>::operator[](int x) { return get_node(x)->data; } //重载等号,进行深度拷贝 template <class T> List<T>& List<T>::operator=(List<T>& x) { if (this == &x)return *this; this->~List(); head = new Node<T>; tail = new Node<T>; head->next = tail; tail->prev = head; length = 0; for (int i : x) { this->push_back(i); } return *this; } template <class T> bool List<T>::operator==(List<T>& x) { return judeg(x)==0; } template <class T> bool List<T>::operator!=(List<T>& x) { return !(*this == x); } template <class T> bool List<T>::operator<(List<T>& x) { return judeg(x) == 2; } template <class T> bool List<T>::operator>(List<T>& x) { return judeg(x) == 3; } template <class T> bool List<T>::operator<=(List<T>& x) { int flag = judeg(x); return flag == 0 || flag == 2; } template <class T> bool List<T>::operator>=(List<T>& x) { int flag = judeg(x); return flag == 0 || flag == 3; }