Josif_tepe

Untitled

Sep 9th, 2025
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.67 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. const int MAX_SIZE = 100;
  4. struct node {
  5.     int info;
  6.     node * prev, * next;
  7. };
  8.  
  9. struct DoublyLinkedCircularList {
  10.     node * head;
  11.     node * tail;
  12.    
  13.     void init() {
  14.         head = NULL;
  15.         tail = NULL;
  16.     }
  17.    
  18.     void insertFront(int x) {
  19.         node * new_node = new node;
  20.         new_node->info = x;
  21.         new_node->prev = NULL;
  22.         new_node->next = NULL;
  23.        
  24.         if(head == NULL) {
  25.             head = new_node;
  26.             tail = head;
  27.             tail->next = head;
  28.             head->prev = tail;
  29.         }
  30.         else {
  31.             new_node->next = head;
  32.             head->prev = new_node;
  33.             head = new_node;
  34.             tail->next = head;
  35.             head->prev = tail;
  36.         }
  37.     }
  38.    
  39.     void insertBack(int x) {
  40.         node * new_node = new node;
  41.         new_node->info = x;
  42.         new_node->prev = NULL;
  43.         new_node->next = NULL;
  44.        
  45.        
  46.         if(head == NULL) {
  47.             head = new_node;
  48.             tail = head;
  49.             tail->next = head;
  50.             head->prev = tail;
  51.         }
  52.         else {
  53.             tail->next = new_node;
  54.             new_node->prev = tail;
  55.             tail = new_node;
  56.             tail->next = head;
  57.             head->prev = tail;
  58.         }
  59.     }
  60.    
  61.     void deleteFront() {
  62.         if(head != NULL) {
  63.             if(head->next == tail) {
  64.                 delete head;
  65.                 head = NULL;
  66.                 tail = NULL;
  67.             }
  68.             else {
  69.                 head = head->next;
  70.                 delete head->prev;
  71.                 head->prev = tail;
  72.                 tail->next = head;
  73.             }
  74.         }
  75.     }
  76.    
  77.     void deleteBack() {
  78.         if(head != NULL) {
  79.             if(head->next == tail) {
  80.                 delete head;
  81.                 head =  NULL;
  82.                 tail = NULL;
  83.             }
  84.             else {
  85.                 tail = tail->prev;
  86.                 delete tail->next;
  87.                 tail->next = head;
  88.                 head->prev = tail;
  89.                
  90.             }
  91.         }
  92.     }
  93.    
  94.     void deleteAll() {
  95.         while(head != NULL) {
  96.             deleteBack();
  97.         }
  98.     }
  99.    
  100.    
  101.     void deleteNode(int x) {
  102.         if(head != NULL) {
  103.             if(head->info == x) {
  104.                 deleteFront();
  105.             }
  106.             else {
  107.                 node * tmp = head;
  108.                 while(tmp->info != x) {
  109.                     tmp = tmp->next;
  110.                 }
  111.                
  112.                 if(tmp == tail) {
  113.                     deleteBack();
  114.                 }
  115.                 else {
  116.                     tmp->next->prev = tmp->prev;
  117.                     tmp->prev->next = tmp->next;
  118.                 }
  119.             }
  120.            
  121.         }
  122.     }
  123.    
  124.     void deleteNode(node *  x) {
  125.         if(head != NULL) {
  126.             if(head == x) {
  127.                 deleteFront();
  128.             }
  129.             else {
  130.                 node * tmp = head;
  131.                 while(tmp != x) {
  132.                     tmp = tmp->next;
  133.                 }
  134.                
  135.                 if(tmp == tail) {
  136.                     deleteBack();
  137.                 }
  138.                 else {
  139.                     tmp->next->prev = tmp->prev;
  140.                     tmp->prev->next = tmp->next;
  141.                     delete tmp;
  142.                 }
  143.             }
  144.            
  145.         }
  146.     }
  147.     void print() {
  148.         node * tmp = head;
  149.         while(tmp != tail) {
  150.             cout << tmp->info << " <--> ";
  151.             tmp = tmp->next;
  152.         }
  153.         cout << tmp->info << " <--> ";
  154.         tmp = tmp->next;
  155.        
  156.         while(tmp != tail) {
  157.             cout << tmp->info << " <--> ";
  158.             tmp = tmp->next;
  159.         }
  160.         cout << tmp->info << endl;
  161.        
  162.         while(tmp != head) {
  163.             cout << tmp->info << " <--> ";
  164.             tmp = tmp->prev;
  165.         }
  166.         cout << tmp->info << " <--> ";
  167.         tmp = tmp->prev;
  168.         while(tmp != head) {
  169.             cout << tmp->info << " <--> ";
  170.             tmp = tmp->prev;
  171.         }
  172.         cout << tmp->info << endl;
  173.     }
  174. };
  175.  
  176. void modificiraj(DoublyLinkedCircularList & a, DoublyLinkedCircularList & b, DoublyLinkedCircularList & c) {
  177.    
  178.     node * tmp = a.head;
  179.    
  180.     while(tmp != a.tail) {
  181.         if(tmp->info % 3 == 0) {
  182.             c.insertBack(tmp->info);
  183.             tmp = tmp->next;
  184.            
  185.             a.deleteNode(tmp->prev->info);
  186.         }
  187.         else {
  188.             tmp = tmp->next;
  189.         }
  190.     }
  191.    
  192.     if(tmp->info % 3 == 0) {
  193.         c.insertBack(tmp->info);
  194.         a.deleteBack();
  195.     }
  196.    
  197.    
  198.     tmp = b.head;
  199.     int pos = 0;
  200.     while(tmp != b.tail) {
  201.         if(pos % 2 == 0) {
  202.             c.insertBack(tmp->info);
  203.             tmp = tmp->next;
  204.             b.deleteNode(tmp->prev->info);
  205.         }
  206.         else {
  207.             tmp = tmp->next;
  208.         }
  209.        
  210.         pos++;
  211.     }
  212.    
  213.     if(tmp == b.tail && pos % 2 == 0) {
  214.         c.insertBack(tmp->info);
  215.         c.deleteBack();
  216.     }
  217.    
  218.     a.print();
  219.     cout << endl;
  220.     b.print();
  221.     cout << endl;
  222.     c.print();
  223.    
  224. }
  225.  int main() {
  226.  
  227.      DoublyLinkedCircularList a, b, c;
  228.      a.init();
  229.      b.init();
  230.      c.init();
  231.      
  232.      int n, m;
  233.      cin >> n;
  234.      
  235.      for(int i = 0; i < n; i++) {
  236.          int x;
  237.          cin >> x;
  238.          a.insertBack(x);
  239.      }
  240.      cin >> m;
  241.      for(int i = 0; i < m; i++) {
  242.          int x;
  243.          cin >> x;
  244.          b.insertBack(x);
  245.      }
  246.      
  247.      modificiraj(a, b, c);
  248.     return 0;
  249. }
  250. /*
  251.  8
  252.  2 5 7 9 3 3 7 1
  253.  12
  254.  5 3 4 1 5 1 1 5 3 4 4 3
  255.  
  256.  
  257.  **/
  258.  
Advertisement
Add Comment
Please, Sign In to add comment