源文件:
#include<functional>
#include<vector>
#include<algorithm>
#ifndef List_H
#define List_H
template<typename T>
void Free(T*p){*p=T();delete p;p=nullptr;return;}
template<typename T>
class List{
private:
struct Node{
Node *pre,*next;T val;
Node(){val=T(),pre=next=nullptr;}
Node(const T&v,Node*p=nullptr,Node*n=nullptr){val=v,pre=p,next=n;}
bool friend operator==(Node a,Node b){return a.next==b.next&&a.pre==b.pre&&a.val==b.val;}
bool friend operator!=(Node a,Node b){return !(a==b);}
};
Node *head,*tail,*newnode,*cur;
unsigned long long l;
public:
struct iterator{
Node* ptr;
iterator(Node*p=nullptr):ptr(p){}
iterator& operator++(){ptr=ptr->next;return *this;}
iterator operator++(int){iterator tmp=*this;ptr=ptr->next;return tmp;}
iterator& operator--(){ptr=ptr->pre;return *this;}
iterator operator--(int){iterator tmp=*this;ptr=ptr->pre;return tmp;}
iterator friend operator+(iterator it,int n){
while(n--) ++it;
return it;
}
iterator friend operator-(iterator it,int n){
while(n--) --it;
return it;
}
T& operator*(){return ptr->val;}
bool operator!=(const iterator& other){return ptr!=other.ptr;}
bool operator==(const iterator& other){return ptr==other.ptr;}
T* operator->(){return &(ptr->val);}
void friend operator+=(iterator&it,int n){it=it+n;return;}
void friend operator-=(iterator&it,int n){it=it-n;return;}
};
public:
List(){
head=new Node();tail=new Node();
head->next=tail;tail->pre=head;l=0;
}
void clear();
void create();
void push_front(T v);
void push_back(T v);
T pop_front();
T pop_back();
void insert(iterator&it,T val);
void erase(iterator&it);
T front();
T back();
unsigned long long size();
bool empty();
auto begin();
auto end();
void sort(const std::function<bool(const T&,const T&)>&comp=std::less<T>());
void merge(List<T>&other,const std::function<bool(const T&,const T&)>&comp=std::less<T>());
void reverse(iterator b=begin(),iterator e=end());
List<T> spilt(iterator ptr);
void unique();
void swap(List<T>&other);
void resize(const unsigned long long len,T val=T());
};
template<typename T>
void List<T>::clear(){
while(!empty()) pop_back();
return;
}
template<typename T>
void List<T>::create(){
head=new Node();tail=new Node();
head->next=tail;tail->pre=head;l=0;
return;
}
template<typename T>
void List<T>::push_front(T v){
newnode=new Node(v,head,head->next);++l;
head->next->pre=newnode;head->next=newnode;
return;
}
template<typename T>
void List<T>::push_back(T v){
newnode=new Node(v,tail->pre,tail);++l;
tail->pre->next=newnode;tail->pre=newnode;
return;
}
template<typename T>
T List<T>::pop_front(){
if(empty()) return T();
cur=head->next;--l;
head->next->next->pre=head;head->next=head->next->next;
cur->pre=cur->next=nullptr;T v=cur->val;
cur->val=T();Free(cur);
return v;
}
template<typename T>
T List<T>::pop_back(){
if(empty()) return T();
cur=tail->pre;--l;
tail->pre->pre->next=tail;tail->pre=tail->pre->pre;
cur->pre=cur->next=nullptr;T v=cur->val;
cur->val=T();Free(cur);
return v;
}
template<typename T>
void List<T>::insert(iterator&it,T val){
cur=it.ptr;++l;
newnode=new Node(val,cur->pre,cur);
cur->pre->next=newnode;cur->pre=newnode;
return;
}
template<typename T>
void List<T>::erase(iterator&it) {
cur=it.ptr;
if(cur==head||cur==tail) return;
cur->pre->next=cur->next;cur->next->pre=cur->pre;
it=iterator(cur->next);Free(cur);--l;
return;
}
template<typename T>
T List<T>::front(){return head->next->val;}
template<typename T>
T List<T>::back(){return tail->pre->val;}
template<typename T>
unsigned long long List<T>::size(){return l;}
template<typename T>
bool List<T>::empty(){return (!l)||(head==nullptr&&tail==nullptr)||head->next==tail;}
template<typename T>
auto List<T>::begin(){return iterator(head->next);}
template<typename T>
auto List<T>::end(){return iterator(tail);}
template<typename T>
void List<T>::sort(const std::function<bool(const T&,const T&)>&comp) {
List l;l.create();std::vector<T> arr;
for(auto i:*this) arr.push_back(i);
std::sort(arr.begin(),arr.end(),comp);
for(auto i:arr) l.push_back(i);
*this=l;return;
}
template<typename T>
void List<T>::merge(List<T>&other,const std::function<bool(const T&, const T&)>&comp){
if(this==&other||other.empty()) return;
Node *curr=head->next,*o_curr=other.head->next;
while(curr!=tail&&o_curr!=other.tail){
if(comp(o_curr->val,curr->val)){
Node* next_o=o_curr->next;
o_curr->pre->next=next_o;
next_o->pre=o_curr->pre;
o_curr->pre=curr->pre;
o_curr->next=curr;
curr->pre->next=o_curr;
curr->pre=o_curr;
o_curr=next_o;
l++;other.l--;
}
else curr = curr->next;
}
if(o_curr!=other.tail){
Node* last=tail->pre;
last->next=o_curr;
o_curr->pre=last;
Node* o_last=other.tail->pre;
tail->pre=o_last;
o_last->next=tail;
l+=other.l;other.l=0;
other.head->next=other.tail;
other.tail->pre=other.head;
}
return;
}
template<typename T>
void List<T>::reverse(iterator b,iterator e){
--e;
while(b!=e&&*(b.ptr)!=*(e.ptr->pre)) std::swap(*b,*e),++b;--e;
return;
}
template<typename T>
List<T> List<T>::spilt(iterator ptr){
if(empty()||ptr==end()) return List<T>();
List<T> ll;ll.create();
while(ptr!=end()-1) ll.push_front(this->pop_back());
return ll;
}
template<typename T>
void List<T>::unique(){
for(auto it=begin()+1;it!=end();++it) if(*it==*(it-1)) erase(--it);
return;
}
template<typename T>
void List<T>::swap(List<T>&other){std::swap(*this,other);return;}
template<typename T>
void List<T>::resize(const unsigned long long len,T val){
if(size()==len) return;
while(size()<len) this->push_back(val);
while(size()>len) this->pop_back();
return;
}
#endif
测试代码:
#include<bits/stdc++.h>
#include"_list.h"
using namespace std;
auto cmp=[](const int&a,const int&b)->bool{
return a>b;
};
int main(){
List<int> l;l.create();
l.push_back(1);
l.push_back(2);
l.push_back(1);
l.push_back(3);
l.sort();
l.reverse();
for(auto i:l) cout<<i;
return 0;
}
报错:
64 33 F:\COB-Project\_list.h [Error] cannot call member function 'auto List<T>::begin() [with T = int]' without object
所以有大佬能捞一下这只蒟蒻吗?必关!