Josif_tepe

Untitled

Sep 4th, 2025
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.41 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct node {
  5.     int info;
  6.     node *next;
  7. };
  8.  
  9. struct SinglyLinkedCircularList {
  10.     node * head;
  11.     node * tail;
  12.    
  13.    
  14.     void init() {
  15.         head = NULL;
  16.         tail = NULL;
  17.     }
  18.    
  19.     void insertFront(int x) {
  20.         node * new_node = new node;
  21.         new_node->info = x;
  22.         new_node->next = NULL;
  23.        
  24.         if(head == NULL) {
  25.             head = new_node;
  26.             tail = head;
  27.             tail->next = head;
  28.         }
  29.         else {
  30.             new_node->next = head;
  31.             head = new_node;
  32.             tail->next = head;
  33.         }
  34.     }
  35.    
  36.     void insertBack(int x) {
  37.         node * new_node = new node;
  38.         new_node->info = x;
  39.         new_node->next = NULL;
  40.        
  41.         if(head == NULL) {
  42.             head = new_node;
  43.             tail = head;
  44.             tail->next = head;
  45.         }
  46.         else {
  47.             tail->next = new_node;
  48.             tail = new_node;
  49.             tail->next = head;
  50.         }
  51.     }
  52.    
  53.     void deleteFront() {
  54.         if(head != NULL) {
  55.             if(head->next == head) {
  56.                 delete head;
  57.                 head = NULL;
  58.                 tail = NULL;
  59.             }
  60.             else {
  61.                 node * tmp = head;
  62.                 head = head->next;
  63.                 delete tmp;
  64.                 tail->next = head;
  65.             }
  66.         }
  67.     }
  68.    
  69.     void deleteBack() {
  70.         if(head != NULL) {
  71.             if(head->next == head) {
  72.                 delete head;
  73.                 head = NULL;
  74.                 tail = NULL;
  75.             }
  76.             else {
  77.                 node * tmp = head;
  78.                 while(tmp->next != tail) {
  79.                     tmp = tmp->next;
  80.                 }
  81.                 delete tail;
  82.                 tail = tmp;
  83.                 tail->next = head;
  84.             }
  85.         }
  86.     }
  87.    
  88.     void deleteNode(int x) {
  89.         if(head != NULL) {
  90.             if(head->info == x) {
  91.                 deleteFront();
  92.             }
  93.             else {
  94.                 node * tmp = head;
  95.                 node * prev = NULL;
  96.                
  97.                 while(tmp != NULL && tmp->info != x) {
  98.                     prev = tmp;
  99.                     tmp = tmp->next;
  100.                 }
  101.                
  102.                 if(tmp == tail) {
  103.                     deleteBack();
  104.                 }
  105.                 else {
  106.                     prev->next = tmp->next;
  107.                     delete tmp;
  108.                 }
  109.                
  110.             }
  111.         }
  112.     }
  113.    
  114.     void deleteNode(node * x) {
  115.         if(head != NULL) {
  116.             if(head == x) {
  117.                 deleteFront();
  118.             }
  119.             else {
  120.                 node * tmp = head;
  121.                 node * prev = NULL;
  122.                
  123.                 while(tmp != NULL && tmp != x) {
  124.                     prev = tmp;
  125.                     tmp = tmp->next;
  126.                 }
  127.                
  128.                 if(tmp == tail) {
  129.                     deleteBack();
  130.                 }
  131.                 else {
  132.                     prev->next = tmp->next;
  133.                     delete tmp;
  134.                 }
  135.                
  136.             }
  137.         }
  138.     }
  139.    
  140.     void print() {
  141.         node * tmp = head;
  142.         for(int i = 0; i < 10; i++) {
  143.             cout << tmp->info << " --> ";
  144.             tmp = tmp->next;
  145.         }
  146.         cout << endl;
  147.     }
  148. };
  149.  
  150. void popravi(SinglyLinkedCircularList slcl) {
  151.     node * losh = slcl.tail->next;
  152.    
  153.     SinglyLinkedCircularList prva;
  154.     prva.init();
  155.    
  156.     SinglyLinkedCircularList vtora;
  157.     vtora.init();
  158.    
  159.     node * tmp = slcl.head;
  160.    
  161.     while(tmp != NULL and tmp != losh) {
  162.         prva.insertBack(tmp->info);
  163.         tmp = tmp->next;
  164.     }
  165.    
  166.     while(losh != slcl.tail) {
  167.         vtora.insertBack(losh->info);
  168.         losh = losh->next;
  169.     }
  170.     vtora.insertBack(slcl.tail->info);
  171.    
  172.    
  173.     prva.print();
  174.     vtora.print();
  175. }
  176.  
  177. int main() {
  178.    
  179.     SinglyLinkedCircularList slcl;
  180.     slcl.init();
  181.    
  182.     int n;
  183.     cin >> n;
  184.    
  185.     for(int i = 0; i < n; i++) {
  186.         int x;
  187.         cin >> x;
  188.        
  189.         slcl.insertBack(x);
  190.     }
  191.    
  192.     slcl.tail->next = slcl.head->next->next->next->next->next->next;
  193.    
  194.     cout << slcl.tail->next->info << endl;;
  195.     popravi(slcl);
  196.     return 0;
  197. }
  198.  
  199. // 108 105 110 107 101 100 108 111 111 112
  200.  
Add Comment
Please, Sign In to add comment