Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>//bug
- #include <stdio.h>
- using namespace std;
- struct list{
- int inf;
- list* next;
- list* prev;
- };
- void init(list *&h, list *&t){
- h = t = NULL;
- }
- void print(list *&t){
- list* p = t;
- while(p){
- cout << p->inf << " ";
- p = p->prev;
- }
- }
- void add_head(list *&h, list *&t, int x){
- list* p = new list;
- p->prev = NULL;
- p->inf = x;
- if(!t){
- p->next = NULL;
- h = t = p;
- }
- else{
- h->prev = p;
- p->prev = t;
- t = p;
- }
- }
- void add_back(list *&h, list *&t, int x){
- list* p = new list;
- p->next = NULL;
- p->inf = x;
- if(!h){
- p->prev = NULL;
- h = t = p;
- }
- else{
- t->next = p;
- p->prev = t;
- t = p;
- }
- }
- void del_all(list *&h, list *&t){
- while(h){
- list* p = h;
- if(p == t)
- h = t = NULL;
- else{
- h = h->next;
- h->prev = NULL;
- }
- delete p;
- }
- }
- list* find(list *&h, list *&t, int x){
- list* p = h;
- while(p){
- if(p->inf == x) break;
- p = p->next;
- }
- return p;
- }
- void insert_after(list *&h, list *&t, list *&r, int x){//?
- list* p = new list;
- p->inf = x;
- p->prev = r;
- p->next = r->next;
- if(r == t){
- t->next = p;
- r = p;//?
- }
- else{
- (r->next)->prev = p;
- r->next = p;
- }
- }
- int pop_front(list *&h, list *&t){
- int i = h->inf;
- list *r = h;
- h = h->next;
- if(h) h->prev = NULL;
- if(!h) t = NULL;
- delete r;
- return i;
- }
- int pop_back(list *&h, list *&t){
- int i = t->inf;
- list *r = t;
- t = t->prev;
- if(t) t->next = NULL;
- if(!t) h = NULL;
- delete r;
- return i;
- }
- void erase(list *&h, list *&t, list *&r){
- if(r == h && r == t)
- h = t = NULL;
- else if(r == h){
- h = h->next;
- t->next = NULL;
- }
- else if(r == t){
- t = t->prev;
- t->next = NULL;
- }
- else{
- (r->prev)->next = r->next;
- (r->next)->prev = r->prev;
- }
- delete r;
- }
- //sort list, using stable qsort
- void connect(list *&auxt, list *&sup){
- auxt->next = sup;
- sup->prev = auxt;
- }
- void sort(list *&h, list *&t, list *&ansh){
- cout << h->inf << " " << t->inf << endl;
- if(h != t)
- {
- list *sup = h;//support list
- list *auxh, *auxt;
- init(auxh, auxt);
- list *cur = h->next;//current element
- while(cur){
- if(cur->inf < sup->inf){
- add_back(auxh, auxt, cur->inf);
- list *ers = cur;
- cur = cur->next;
- erase(h, t, ers);
- }
- else cur = cur->next;
- }
- cout << "ok\n";
- if(auxh && auxt){
- connect(auxt, sup);
- //print(auxt); cout << endl; print(t); cout << endl;
- if(!ansh || auxh->inf < ansh->inf){
- ansh = auxh;
- }
- sort(auxh, auxt, ansh);
- sort(sup, t, ansh);
- }
- else{
- cout << "new iter\n";
- if(sup->next) sort(sup->next, t, ansh);
- }
- }
- cout << ansh->inf << "woow\n";
- while(ansh){
- cout << ansh->inf << endl;
- ansh = ansh->next;
- }
- cout << "woow\n";
- if(ansh->prev != h) connect(ansh->prev, h);
- }
- int main()
- {
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- printf("Enter the number of elements and new member, then array of numbers\n");
- int n; scanf("%d", &n);
- list *h, *t;
- init(h, t);
- for(int i = 0; i < n; i++){
- int cur; scanf("%d", &cur);
- add_back(h, t, cur);
- }
- list *ansh = h;
- sort(h, t, ansh);
- while(ansh){
- cout << ansh->inf << " ";
- ansh = ansh->next;
- }
- //cout << ansh->inf << " ";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement