Advertisement
palenda21

Lab14A

May 25th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. #include<iostream>
  2. #include<iomanip>
  3. using namespace std;
  4.  
  5. struct queue
  6. {
  7.     int D;
  8.     queue* next;
  9.     queue* prev;
  10. };
  11.  
  12. void push(queue** top, queue** top_first_in_first_out, int element)
  13. {
  14.     queue* q = new queue();
  15.     q->D = element;
  16.     q->next = NULL;
  17.     q->prev=NULL;
  18.     if (*top_first_in_first_out == NULL)
  19.     {
  20.         *top = *top_first_in_first_out = q;
  21.     }
  22.     else
  23.     {
  24.         (*top)->next = q;
  25.         q->prev=(*top);
  26.         *top = q;
  27.     }
  28. }
  29.  
  30. void print(queue* top_first_in_first_out)
  31. {
  32.     queue* q = top_first_in_first_out;
  33.     while (q != NULL)
  34.     {
  35.         cout << setw(10) << q->D;
  36.         q = q->next;
  37.     }
  38.     cout << endl;
  39. }
  40.  
  41.  
  42. void insert_99(queue* top)
  43. {
  44.     //проверка если очередь пуста
  45.      if(top==NULL){
  46.          cout<<"queue is empty!";
  47.          exit(1);
  48.      }
  49.    
  50.     //проверка! если у нас в очереди один улемент то заканчиваем программу//так как некорруктный ввод данных для вставки
  51.     if(top->next==NULL && top->prev==NULL){
  52.         cout<<"в очереди один улемент!!!";
  53.         exit(1);
  54.     }
  55.    
  56.     // -4   -5 -6 8        4 -5  -6
  57.     //    99
  58.     while (top->next != NULL)
  59.     {
  60.         if(top->D<0 && top->next->D<0){
  61.             queue *q=new queue();
  62.             q->D=99;
  63.             q->next=top->next;
  64.             q->prev=top;
  65.            
  66.             top->next->prev=q;
  67.             top->next=q;
  68.            
  69.             top=top->next;
  70.         }
  71.         top=top->next;
  72.     }
  73. }
  74.  
  75.  
  76. void delete_queue(queue* top_first_in_first_out)
  77. {
  78.     queue* q ;
  79.  
  80.     while (top_first_in_first_out != NULL)
  81.     {
  82.         q = top_first_in_first_out;
  83.         top_first_in_first_out = top_first_in_first_out->next;
  84.         q->next = NULL;
  85.         delete q;
  86.     }
  87. }
  88.  
  89. int main()
  90. {
  91.     queue* top = NULL;
  92.     queue* top_first_in_first_out = NULL;
  93.  
  94.     int n;
  95.     cout << "Write number of elements: " << endl;
  96.     cin >> n;
  97.     int element=0;
  98.  
  99.     for(int i=0;i<n;i++){
  100.         cin>>element;
  101.         push(&top, &top_first_in_first_out, element);
  102.     }
  103.  
  104.    
  105.  
  106.     cout << endl<<endl<< "Old queue:" << endl;
  107.     print(top_first_in_first_out);
  108.  
  109.     insert_99(top_first_in_first_out);
  110.  
  111.     cout << endl << "New queue" << endl;
  112.     print(top_first_in_first_out);
  113.  
  114.     delete_queue(top_first_in_first_out);
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement