class Node { public: Node(); Node(int a); virtual ~Node(); show(); push_back(Node*); pop_back(); int length(); Node* Insert(Node *,size_t); friend Node* Delete(Node *,Node *,size_t); friend Node* Switch(Node *,size_t,size_t); Node operator = (Node &); friend Node Single(Node); friend int Reverse(Node *); friend int GetNodeValue(Node *,size_t); friend Node* Root(Node*); friend Node* B_Sort(Node*); private: Node *next; Node *prev; int val; }; Node::Node() { val=-1; this->next=NULL; this->prev=NULL; } Node::Node(int a) { val=a; this->next=NULL; this->prev=NULL; } Node::~Node() { // delete next; // delete prev; } Node::show() { if (this) { cout<<val<<endl; if (this->next) { this->next->show(); } } } Node::push_back(Node *n) { if (this->next) { this->next->push_back(n); } else { this->next=n; n->prev=this; //n->next=NULL; } } Node::pop_back() { if (this->next) { this->next->pop_back(); } else { this->prev->next=NULL; this->prev=NULL; } } int Node::length() { if (this) { if (this->next) { return this->next->length()+1; } else { return 1; } } else return 0; } Node* Node::Insert(Node *n,size_t loc) { assert(loc>=0&&loc<=this->length()); Node *t=this; if (loc==0) { n->next=this; n->prev=NULL; this->prev=n; return n; } // else if (loc==this->length()) // { // this->push_back(n); // //return this; // } else { while (--loc) { t=t->next; } n->next=t->next; n->prev=t; if (t->next) { t->next->prev=n; } t->next=n; //*t=root(t); //this->show(); //return this; } return this; } Node* Delete(Node *p,Node *n,size_t loc) { assert(loc>0&&loc<=p->length()); Node *t=p; if (loc==1) { *n=Single(*p); p->next->prev=NULL; //this->next=NULL; return p->next; } // else if (loc==this->length()) // { // this->pop_back(); // } else { while (--loc) { t=t->next; } t->prev->next=t->next; if (t->next) { t->next->prev=t->prev; } p=Root(t->prev); t->next=NULL; t->prev=NULL; *n=*t; } return p; } Node* Switch(Node *n,size_t a,size_t b) { assert(a>0&&a<=n->length()); assert(b>0&&b<=n->length()); size_t min=a<b?a:b; size_t max=a+b-min; if (a==b) { } else if (n->length()==2) { Node *d=new Node; Node *t=n; t=Delete(t,d,1); t->push_back(d); *n=*t; } else { Node *nleft=new Node; Node *nright=new Node; Node *t=n; t=Delete(t,nleft,min); t=Delete(t,nright,max-1); t=t->Insert(nright,min-1); t=t->Insert(nleft,max-1); *n=*t; //return t; } return n; } Node Node::operator=(Node &n) { this->next=n.next; this->prev=n.prev; this->val=n.val; return *this; } Node Single(Node n) { n.next=NULL; n.prev=NULL; return n; } int Reverse(Node *n) { size_t len=n->length(); for (size_t i=1;i<=len/2;++i) { Switch(n,i,len-i+1); } return 0; } Node* B_Sort(Node *n) { size_t len=n->length(); for (size_t i=1;i<len;++i) { for (size_t j=i+1;j<=len;++j) { if (GetNodeValue(n,i)>GetNodeValue(n,j)) { Switch(n,i,j); } } } return n; } int GetNodeValue(Node *n,size_t loc) { assert(loc>0&&loc<=n->length()); while (--loc) { n=n->next; } return n->val; } // Node GetNode(Node *n,size_t loc) // { // assert(loc>0&&loc<=n->length()); // Node *t=n; // while (--loc) // { // t=t->next; // } // t->prev=NULL; // t->next=NULL; // return *t; // } Node* Root(Node *n) { if (n->prev) { return Root(n->prev); } else { return n; } } void main() { Node *roo=new Node(2); //cout<<root->length()<<endl; Node *a=new Node(3); Node *b=new Node(1); Node *c=new Node(4); Node *d=new Node; roo->push_back(a); roo->push_back(b); //cout<<root->length()<<endl; //roo->pop_back(); //roo->show(); //cout<<root->length()<<endl; roo=roo->Insert(c,0); roo->show(); //roo=roo->Delete(d,1); //GetNode(roo,3); cout<<endl; //roo->show(); //cout<<endl; //Switch(roo,2,3); //roo->show(); //cout<<endl; //Switch(roo,1,4); //roo->show(); //Switch(roo,1,4); Reverse(roo); roo->show(); cout<<endl; B_Sort(roo); roo->show(); //b->show(); //d->show(); delete roo,a,b,c,d; }