Advertisement
Guest User

1

a guest
Sep 17th, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.02 KB | None | 0 0
  1. //-----------------------------------------------------------------------------------------
  2.  
  3. #include <iostream>
  4. #include <time.h>
  5. using namespace std;  
  6.  
  7. //------------------Структура-----------------------------------------------------------
  8.  
  9. struct Ochd {
  10.   int info;
  11.   Ochd *next, *prev;
  12. } *t, *p;
  13.  
  14. //------------------Прототипы-----------------------------------------------------------
  15.  
  16. Ochd *AddOchd (Ochd *, Ochd **, int);
  17. Ochd *Addbegin (Ochd *, Ochd **, int);
  18. void View (Ochd *);
  19. void Dell_All (Ochd *);
  20. Ochd *Select(Ochd *, Ochd **, Ochd *, Ochd **);
  21. void View_back (Ochd *);
  22. Ochd *FindAndDell(Ochd *begin1, int F_el);
  23.  
  24. //-----------------------------------------------------------------------------------------
  25.  
  26. Ochd *AddOchd (Ochd *begin1, Ochd **end, int in) {
  27.  
  28.   t = new Ochd;
  29.   t->info = in;
  30.   t->next = NULL;
  31.   t->prev = (*end);
  32.   if ((*end) == NULL) begin1 = t;
  33.   else (*end)->next = t;
  34.   (*end) = t;
  35.  
  36.   return begin1;
  37. }
  38.  
  39. //---------------------------Просмотр------------------------------------------------------
  40.  
  41. void View (Ochd *p) {
  42.   while (p != NULL) {
  43.     if ((p->info) < 0)
  44.       cout << p->info << endl;
  45.     else cout << " " << p->info << endl;
  46.     p = p->next;
  47.  
  48.   }
  49. }
  50.  
  51. //------------------Просмотр наоборот---------------------------------------------------
  52.  
  53. void View_back (Ochd *p) {
  54.   while (p != NULL) {
  55.     if ((p->info) < 0)
  56.       cout << p->info << endl;
  57.     else cout << " " << p->info << endl;
  58.     p = p->prev;
  59.   }
  60. }
  61.  
  62. //-----------------------------------Удаление всей очереди---------------------------------
  63.  
  64. void Dell_All (Ochd *begin1) {
  65.   while (begin1 != NULL) {
  66.     t = begin1;
  67.     begin1 = begin1->next;
  68.     delete t;
  69.   }
  70.  
  71. }
  72.  
  73. //-----------------------------------Добавить эл-т в начало--------------------------------
  74.  
  75. Ochd *Addbegin (Ochd *begin1, Ochd **end, int in) {
  76.  
  77.   t = new Ochd;
  78.   t->info = in;
  79.   t->prev = NULL;
  80.   t->next = begin1;
  81.   if (begin1 == NULL) *end = t;
  82.   else begin1->prev = t;
  83.   begin1 = t;
  84.  
  85.   return begin1;
  86. }
  87.  
  88. //------------------------------------Поиск и удаление элемента---------------------------
  89.  
  90. Ochd *FindAndDell(Ochd *begin1, int F_el) {
  91.  
  92.   t = begin1;
  93.   p = begin1;
  94.  
  95.   if (t->info == F_el) {
  96.     cout << "Удаление в начале" << endl;
  97.     t->next->prev = NULL;
  98.     p = begin1->next;
  99.     begin1 = begin1->next;
  100.     delete t;
  101.   }
  102.  
  103.   while (begin1->next != NULL) {
  104.     if (t->info == F_el) {
  105.       cout << "Удаление" << endl;
  106.       t = begin1;
  107.       t->next->prev = t->prev->prev;
  108.       t->prev->next = t->next->next;
  109.       begin1 = begin1->next;
  110.       delete t;
  111.     }
  112.     begin1 = begin1->next;
  113.   }
  114.  
  115.   t = begin1;
  116.   if (t->info == F_el) {
  117.     cout << "Удаление в конце" << endl;
  118.     t->prev->next = NULL;
  119.     begin1 = begin1->next;
  120.     delete t;
  121.   }
  122.   return p;
  123. }
  124.  
  125. //-----------------------------------------------------------------------------------------
  126. //-----------------------------------------------------------------------------------------
  127.  
  128. int main () {
  129.   setlocale(LC_ALL, "Russian");
  130.   srand(time(NULL));
  131.  
  132. //-----------------------------------------------------------------------------------------
  133.  
  134.   int n, t;
  135.   Ochd *begin1, *end;
  136.   begin1 = end = NULL;
  137.  
  138. //-----------------------------------------------------------------------------------------
  139.  
  140.   cout << "Введите количество элементов: "; cin >> n; cout << endl;
  141.   for (int i = 0; i < n; i++) {
  142.     t = -50 + rand() % 100;
  143.     begin1 = AddOchd(begin1, &end, t);
  144.   }
  145.  
  146.  
  147. //-----------------------------------------------------------------------------------------
  148.   cout << "Очередь:" << endl;
  149.   View (begin1); cout << endl;
  150.   cout << "Значение эл-та, который нужно удалить:"; cin >> n; cout << endl;
  151.   begin1 = FindAndDell(begin1, n);
  152.   View (begin1); cout << endl;
  153.  
  154.   Dell_All (begin1);
  155.  
  156. }
  157. //-----------------------------------------------------------------------------------------
  158. //-----------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement