Advertisement
Guest User

Untitled

a guest
May 26th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.10 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include "ctime"
  4.  
  5. using namespace std;
  6.  
  7. //-----------------------------------------------------------
  8.  
  9. struct Spis2 {
  10.     int info;
  11.     Spis2 * next, *prev;
  12. } *t, *beg, *finish;
  13. void Sort_p(Spis2 **);
  14. void Sort_info(Spis2 *);
  15. void Del_2(Spis2 *);
  16. void Del_All(Spis2 **);
  17. void Create(Spis2**, Spis2**, int);
  18. void Add(int, Spis2**, Spis2**, int);
  19. void View(int, Spis2*);
  20. //----------------------------------------------------------
  21. void Create(Spis2 **b, Spis2 **e, int in) {
  22.     t = new Spis2;
  23.     t->info = in;
  24.     t->next = t->prev = NULL;
  25.     *b = *e = t;
  26. }
  27. void Add(int kod, Spis2 **b, Spis2 **e, int in) {
  28.     t = new Spis2;
  29.     t->info = in;
  30.     if (kod == 0) {
  31.         t->prev = NULL;
  32.         t->next = *b;
  33.         (*b)->prev = t;
  34.         *b = t;
  35.     }
  36.     else {
  37.         t->next = NULL;
  38.         t->prev = *e;
  39.         (*e)->next = t;
  40.         *e = t;
  41.     }
  42. }
  43.  
  44. //-----------------------------------------------------------
  45. void View(int kod, Spis2 *p) {
  46.     while (t != NULL) {
  47.         cout << "   " << t->info << endl;
  48.         if (kod == 0) t = t->next;
  49.         else  t = t->prev;
  50.  
  51.     }
  52. }
  53. //-----------------------------------------------------------
  54.  
  55. void Del_2(Spis2 *p)
  56. {
  57.     t = p->next;
  58.     while (t != NULL) {
  59.         if (t->info % 2 == 0) {
  60.             p->next = t->next;
  61.             delete t;
  62.             t = p->next;
  63.         }
  64.         else {
  65.             p = t;
  66.             t = t->next;
  67.         }
  68.     }
  69.  
  70.     delete t;
  71.  
  72. }
  73.  
  74. //-----------------------------------------------------------
  75. void Sort_p(Spis2 **p) {
  76.     Spis2 *t = NULL, *t1, *r, *l;
  77.     if ((*p)->next->next == NULL) return;
  78.     do {
  79.         for (t1 = *p; t1->next->next != t; t1 = t1->next)
  80.             if (t1->next->info > t1->next->next->info) {
  81.                 r = t1->next->next; l = t1->next->next->prev;
  82.                 t1->next->next = r->next;
  83.                 t1->next->prev = l;
  84.                 r->next = t1->next;
  85.                 l->prev = t1->next->prev;
  86.                 t1->next = r;
  87.             }
  88.         t = t1->next;
  89.     } while ((*p)->next->next != t);
  90. }
  91. void Sort_info(Spis2 *p) {
  92.     Spis2 *t = NULL, *t1;
  93.     int r;
  94.     do {
  95.         for (t1 = p; t1->next != t; t1 = t1->next)
  96.             if (t1->info > t1->next->info) {
  97.                 r = t1->info;
  98.                 t1->info = t1->next->info;
  99.                 t1->next->info = r;
  100.             }
  101.         t = t1;
  102.     } while (p->next != t);
  103. }
  104. void Del_All(Spis2 **p) {
  105.     Spis2 *t;
  106.     while (*p != NULL) {
  107.         t = *p;
  108.         *p = (*p)->next;
  109.         delete t;
  110.     }
  111. }
  112. Spis2* InSpis2(Spis2 *p, int in) {
  113.     Spis2 *t = new Spis2;
  114.     t->info = in;
  115.     t->next = p;
  116.     return t;
  117. }
  118.  
  119. Spis2* pop(Spis2* p, int &out)
  120. {                              
  121.     if (!p) return p;
  122.     out = p->info;
  123.     Spis2* tmp = p->next;
  124.     delete p;
  125.     return tmp;
  126. }
  127.  
  128. //-----------------------------------------------------------
  129. int main()
  130. {
  131.     int  in, kod, kod1;
  132.     char Str[2][10] = { "Begin ", "End " };
  133.     while (true) {
  134.         cout << "\n\tCreat - 1.\n\tAdd - 2.\n\tView - 3.\n\tDel - 4.\n\tSort1 - 5.\n\tSort2 - 6.\n\tInd_zad - 7.\n\tEXIT - 0. : ";
  135.         cin >> kod;
  136.         switch (kod) {
  137.         case 1:        if (beg != NULL) {
  138.             cout << "Clear Memory!" << endl;
  139.             break;
  140.         }
  141.                        cout << "Begin Info = ";     cin >> in;
  142.                        Create(&beg, &finish, in);
  143.                        cout << "Creat Begin = " << beg->info << endl;
  144.                        break;
  145.         case 2:
  146.             cout << "Info = ";        cin >> in;
  147.             cout << "Add Begin - 0, Add End - 1 :  ";      cin >> kod1;
  148.             Add(kod1, &beg, &finish, in);
  149.             if (kod1 == 0) t = beg;
  150.             else t = finish;
  151.             cout << "Add to " << Str[kod1] << "  " << t->info << endl;
  152.             break;
  153.  
  154.         case 3: if (!beg) {
  155.             cout << "Spis2 Pyst!" << endl;
  156.             break;
  157.         }
  158.                 cout << "--- Spis2 ---" << endl;
  159.                 cout << "View Begin-0,View End-1:";
  160.                 cin >> kod1;
  161.                 if (kod1 == 0) {
  162.                     t = beg;
  163.                     cout << "-- Begin --" << endl;
  164.                 }
  165.                 else {
  166.                     t = finish;
  167.                     cout << "--- End --" << endl;
  168.                 }
  169.  
  170.                 View(kod1, t);
  171.                 break;
  172.         case 4:
  173.             Del_All(&beg);
  174.             cout << "Memory Free!" << endl;
  175.             break;
  176.         case 5:
  177.             beg = InSpis2(beg, 0);
  178.             int kek;
  179.             if (beg != NULL) Sort_p(&beg);
  180.             beg = pop(beg, kek);
  181.             break;
  182.         case 6:
  183.             if (beg != NULL) Sort_info(beg);
  184.             break;
  185.         case 7:
  186.             beg = InSpis2(beg, 1337);
  187.             if (beg != NULL) Del_2(beg);
  188.             int t;
  189.             beg = pop(beg, t);
  190.             break;
  191.         case 0:
  192.             if (beg != NULL)
  193.                 Del_All(&beg);
  194.             return 0;
  195.         }
  196.     }
  197.     return 0;
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement