#include <iostream> #include <cstdlib> #include <string> #include <list> #include <cassert> #include <time.h> #include <set> #include <algorithm> using namespace std; #define NULL 0 template<typename T> class LinkList { class Node { public: Node():next(NULL),prev(NULL){} Node(const T& d):data(d),next(NULL),prev(NULL){} T data; Node *next; Node *prev; }; private: Node *head; public: LinkList():head(new Node()) { //point to itself head->next=head; head->prev=head; } void push_front(const T& v) { Node *t=head->next; Node *cur=new Node(v); head->next=cur; t->prev=cur; cur->next=t; cur->prev=head; } const T& front() { assert(!empty()); return head->next->data; } void pop_front() { assert(!empty()); Node *cur=head->next; head->next=cur->next; cur->next->prev=head; delete cur; } void push_back(const T& v) { Node *t=head->prev; Node *cur=new Node(v); t->next=cur; head->prev=cur; cur->next=head; cur->prev=t; } const T& back() { assert(!empty()); return head->prev->data; } bool empty() { return head->next==head; } void pop_back() { assert(!empty()); Node *cur=head->prev; cur->prev->next=head; head->prev=cur->prev; delete cur; } ~LinkList() { Node *p=head->next; while(p != head) { Node *temp=p->next; delete p; p=temp; } delete head; } }; int main() { srand(time(0)); list<int> l; LinkList<int> a; // for(int i=0;i<1000;i++) { int t=rand(); if(rand()%2) { a.push_front(t); l.push_front(t); } else { a.push_back(t); l.push_back(t); } } while(!a.empty()) { int v1,v2; if(rand()%2) { v1=a.front(); v2=l.front(); a.pop_front(); l.pop_front(); } else { v1=a.back(); v2=l.back(); a.pop_back(); l.pop_back(); } cout << v1 << '/t' << v2 << endl; assert(v1==v2); } assert(l.empty()==true); return 0; }