SHARE
TWEET

Untitled

a guest Feb 27th, 2020 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ctime>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. struct list
  6. {
  7.     int field;
  8.     struct list* next;
  9.     struct list* prev;
  10. };
  11. list *create(list *lst) {
  12.     list* tmp;
  13.     tmp = new list;
  14.     tmp->field = 1 + rand() % 100;
  15.     tmp->next = NULL;
  16.     tmp->prev = lst;
  17.     lst->next = tmp;
  18.     return tmp;
  19. }
  20. list* init()  {
  21.     list* lst;
  22.     lst = new list;
  23.     srand(time(0));
  24.     lst->field = 1 + rand() % 100;
  25.     lst->next = NULL;
  26.     lst->prev = NULL;
  27.     return(lst);
  28. }
  29. void show(list *first) {
  30.     list* tmp = first;
  31.     while (tmp) {
  32.         cout << tmp->field << ' ';
  33.         tmp = tmp->next;
  34.     }
  35. }
  36. list* chek(list* tmp, list *&lstTop)
  37. {
  38.     if (tmp->field % 2 == 0) {
  39.         list* prev = tmp->prev, * next = tmp->next;
  40.         if (prev == NULL) {
  41.             next->prev = prev;
  42.             lstTop = next;
  43.             delete tmp;
  44.             return next;
  45.         }
  46.         else if (next == NULL) {
  47.             prev->next = next;
  48.             delete tmp;
  49.             return prev->next;
  50.         }
  51.         else
  52.         prev->next = next;
  53.         next->prev = prev;
  54.         delete tmp;
  55.         return next;
  56.     }
  57.     else return tmp->next;
  58. }
  59. void deleteAllList(list* tmp) {
  60.     list* next;
  61.     while (tmp) {
  62.         cout << tmp->field << ' ';
  63.         next = tmp->next;
  64.         delete tmp;
  65.         tmp = next;
  66.     }
  67. }
  68. int main()
  69. {
  70.     setlocale(LC_ALL, "RU");
  71.     list* lst, *lstTop; int m; cout << "Введите кол узлов: "; cin >> m;
  72.     lst = init(); lstTop = lst;
  73.     for (int i = 1; i < m; i++) lst = create(lst);
  74.     show(lstTop);
  75.     for (list* p = lstTop; p; ) {
  76.         p = chek(p, lstTop);
  77.     }
  78.     cout << endl;
  79.     //show(lstTop);
  80.     deleteAllList(lstTop);
  81. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top