Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- class node {
- public:
- int elem;
- node* next;
- };
- class nodet {
- public:
- int elem;
- nodet* next;
- nodet* prev;
- };
- node* push_before(node* list, int elem) {
- node* p = new node;
- p->elem = elem;
- p->next = list;
- return p;
- }
- void push(nodet*& list, int elem, nodet*& last) {
- if (list == NULL) {
- list = new nodet;
- list->next = NULL;
- list->prev = NULL;
- list->elem = elem;
- last = list;
- return;
- }
- nodet* p = new nodet;
- p->elem = elem;
- p->prev = last;
- p->next = NULL;
- last->next = p;
- last = p;
- }
- void merge(nodet*& listf, nodet*& lastf, nodet* lists, nodet* lasts) {
- nodet* listp = listf;
- listf = NULL;
- nodet* lastp = lastf;
- lastf = NULL;
- while (listp != NULL && lists != NULL) {
- if (listp->elem > lists->elem) {
- if (listf == NULL) {
- listf = lists;
- lists->prev = NULL;
- nodet* tmp = lists->next;
- lists->next = NULL;
- lists = tmp;
- lastf = listf;
- } else {
- lists->prev = lastf;
- lastf->next = lists;
- nodet* tmp = lists->next;
- lists->next = NULL;
- lists = tmp;
- lastf = lastf->next;
- }
- } else {
- if (listf == NULL) {
- listf = listp;
- listp->prev = NULL;
- nodet* tmp = listp->next;
- listp->next = NULL;
- listp = tmp;
- lastf = listf;
- } else {
- listp->prev = lastf;
- lastf->next = listp;
- nodet* tmp = listp->next;
- listp->next = NULL;
- listp = tmp;
- lastf = lastf->next;
- }
- }
- }
- while (listp != NULL) {
- listp->prev = lastf;
- lastf->next = listp;
- lastf = listp;
- nodet* tmp = listp->next;
- listp->next = NULL;
- listp = tmp;
- }
- while (lists != NULL) {
- lists->prev = lastf;
- lastf->next = lists;
- lastf = lists;
- nodet* tmp = lists->next;
- lists->next = NULL;
- lists = tmp;
- }
- }
- void mergesort(nodet*& listf, nodet*& lastf) {
- if (listf != lastf && listf != NULL&& lastf != NULL) {
- nodet* listp = listf;
- nodet* lastp = lastf;
- while (listp != lastp) {
- if (listp->next != lastp) {
- lastp = lastp->prev;
- listp = listp->next;
- } else {
- break;
- }
- }
- nodet* list1 = listf;
- nodet* last1 = listp;
- nodet* list2 = listp->next;
- listp->next->prev = NULL;
- listp->next = NULL;
- nodet* last2 = lastf;
- mergesort(list1, last1);
- mergesort(list2, last2);
- merge(list1, last1, list2, last2);
- listf = list1;
- lastf = last1;
- }
- }
- node* sorth(node* list, int size) {
- if (size > 1) {
- node* miner = NULL;
- node* bigger = NULL;
- node* startm = NULL;
- node* startb = NULL;
- node* starte = NULL;
- int sizeb = 0, sizem = 0, sizee = 0;
- node* eq = NULL;
- int piv = list->elem;
- while (list != NULL) {
- if (list->elem < piv) {
- if (miner != NULL) {
- miner->next = list;
- miner = miner->next;
- } else {
- miner = list;
- startm = list;
- }
- ++sizem;
- }
- if (list->elem > piv) {
- if (bigger != NULL) {
- bigger->next = list;
- bigger = bigger->next;
- } else {
- bigger = list;
- startb = list;
- }
- ++sizeb;
- }
- if (list->elem == piv) {
- if (eq != NULL) {
- eq->next = list;
- eq = eq->next;
- } else {
- eq = list;
- starte = list;
- }
- ++sizee;
- }
- node* a = list->next;
- list->next = NULL;
- list = a;
- }
- startm = sorth(startm, sizem);
- startb = sorth(startb, sizeb);
- int slu = 0;
- node* start = startm;
- while (startm != NULL && startm->next != NULL) {
- startm = startm->next;
- }
- if (startm != NULL) {
- startm->next = starte;
- } else {
- slu = 1;
- start = starte;
- }
- while (starte != NULL && starte->next != NULL) {
- starte = starte->next;
- }
- if (starte != NULL) {
- starte->next = startb;
- } else {
- if (slu == 1) {
- return startb;
- } else {
- startm->next = startb;
- return start;
- }
- }
- return start;
- }
- return list;
- }
- int main() {
- nodet* list = NULL;
- nodet* last = NULL;
- int num;
- std::cin >> num;
- for (int i = 0; i < num; ++i) {
- int count;
- std::cin >> count;
- push(list, count, last);
- }
- mergesort(list, last);
- for (int i = 0; i < num; ++i) {
- std::cout << list->elem << ' ';
- list = list->next;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement