akadjoker

Untitled

Jul 11th, 2025
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using std::cout;
  5. using std::endl;
  6.  
  7. struct Node
  8. {
  9.     int value{0};
  10.     Node* next{nullptr};
  11. };
  12.  
  13.  
  14.  
  15. struct Lista
  16. {
  17.     int count{0};
  18.     Node *head{ nullptr };
  19.     Node *tail{ nullptr };
  20.  
  21.     void print()
  22.     {
  23.          if (!head)
  24.          {
  25.              cout << "[]" <<endl;
  26.              return;
  27.          }
  28.          int index = 0;
  29.          Node* current = head;
  30.          cout <<count << " [" ;
  31.          while (current)
  32.          {
  33.             cout <<current->value;
  34.             index++;
  35.             if (index <count) cout << ",";
  36.  
  37.             current = current->next;
  38.          }
  39.          cout <<"]" <<endl;
  40.     }
  41.  
  42.     void push_front(int value)
  43.     {
  44.          Node* node = new Node();
  45.          node->value = value;
  46.          if(!head)
  47.          {
  48.              head = node;
  49.              tail = node;
  50.          }else
  51.          {
  52.             node->next = head;
  53.             head = node;
  54.          }
  55.          count++;
  56.     }
  57.  
  58.     void push_back(int value)
  59.     {
  60.  
  61.         Node* node = new Node();
  62.          node->value = value;
  63.          if(!head)
  64.          {
  65.              head = node;
  66.              tail = node;
  67.          }else
  68.          {
  69.              tail->next = node;
  70.              tail = node;
  71.          }
  72.          count++;
  73.  
  74.  
  75.        
  76.     }
  77.  
  78.  
  79.     bool pop_front()
  80.     {
  81.         if (!head) return false;
  82.  
  83.         Node* tmp = head->next;
  84.      //   cout << "delete:" << head->value << endl;
  85.         delete head;
  86.         head = tmp;
  87.         count--;
  88.         return true;
  89.     }
  90.  
  91.     bool pop_back()
  92.     {
  93.         if (!head) return false;
  94.  
  95.         if (count==1)
  96.         {
  97.             return pop_front();
  98.         }
  99.  
  100.  
  101.         Node* current = head;
  102.         Node* prev = nullptr;
  103.         while(current->next)
  104.         {
  105.             prev = current;
  106.             current = current->next;
  107.         }
  108.         if(prev)
  109.         {
  110.             Node* del = prev->next;
  111.             tail = prev;
  112.             tail->next = nullptr;
  113.             cout << del->value << endl;
  114.             delete del;
  115.         }
  116.  
  117.         count--;
  118.        
  119.         return true;
  120.     }
  121.  
  122.  
  123.     void clear()
  124.     {
  125.  
  126.  
  127.         Node* tmp=head;
  128.         while (head)
  129.         {
  130.             tmp = head->next;
  131.         //    cout << "delete:" << head->value << endl;
  132.             delete head;
  133.             head = tmp;
  134.         }
  135.         count = 0;
  136.     }
  137.  
  138.  
  139.     bool insert(int index, int value)
  140.     {
  141.         if(index<=0)
  142.         {
  143.             push_front(value);
  144.             return true;
  145.         }
  146.         if(index>=count)
  147.         {
  148.             push_back(value);
  149.             return true;
  150.         }
  151.  
  152.         Node* prev = head;
  153.         Node* current = nullptr;
  154.         for (int i = 0; i <= index - 1; i++)
  155.         {
  156.             current = prev;
  157.             prev = prev->next;
  158.         }
  159.  
  160.         Node* node = new Node();
  161.         node->value = value;
  162.         node->next = prev;
  163.         current->next = node;
  164.         count++;
  165.  
  166.         // cout<<"index: " << index << endl;
  167.         // cout<<"prev: " << prev->value << endl;
  168.         // cout<<"currn: " << current->value << endl;
  169.  
  170.         return false;
  171.     }
  172.  
  173.  
  174.     bool remove(int index)
  175.     {
  176.         if (!head) return false;
  177.         if (index <= 0) return pop_front();
  178.         if (index >= count) return pop_back();
  179.         Node* prev = head;
  180.         Node* current = nullptr;
  181.         for (int i = 0; i <= index - 1; i++)
  182.         {
  183.             current = prev;
  184.             prev = prev->next;
  185.         }
  186.  
  187.         // cout<<"prev: " << prev->value << endl;
  188.         // cout<<"currn: " << current->value << endl;
  189.  
  190.         current->next = prev->next;
  191.         delete prev;
  192.  
  193.         count--;
  194.  
  195.  
  196.         return false;
  197.     }
  198. };
  199.  
  200. int main()
  201. {
  202.  
  203.     Lista lista;
  204.     for (int i = 0; i <= 5;i++)
  205.         lista.push_back(i);
  206.  
  207.  
  208.     lista.insert(5, 100);
  209.  
  210.     // lista.remove(5);
  211.  
  212.  
  213.     // lista.push_back(1);
  214.  
  215.     //  lista.pop_back();
  216.     // lista.pop_back();
  217.     // lista.pop_back();
  218.     // lista.pop_back();
  219.  
  220.     lista.print();
  221.  
  222.     lista.clear();
  223.     return 0;
  224. }
Tags: single_list
Advertisement
Add Comment
Please, Sign In to add comment