Advertisement
Sitisom

Односвязный список (базовые функции)

Apr 2nd, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.08 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3.  
  4. struct Node
  5. {
  6.     int data;
  7.     Node* next;
  8. };
  9.  
  10. class MyList {
  11.     Node* head;
  12.     Node* tail = new Node;
  13. public:
  14.     bool is_empty()
  15.     {
  16.         return head == NULL;
  17.     }
  18.  
  19.     int pop()
  20.     {
  21.         int data;
  22.         Node* p;
  23.         if (tail)
  24.         {
  25.             data = tail->data;
  26.             p = previous(tail);
  27.             p->next = NULL;
  28.             delete tail;
  29.             tail = p;
  30.         }
  31.         return data;
  32.     }
  33.  
  34.     void pop(int &data)
  35.     {
  36.         Node* p;
  37.         if (!is_empty())
  38.         {
  39.             data = tail->data;
  40.             p = tail->next;
  41.             delete tail;
  42.             tail = p;
  43.         }
  44.     }
  45.  
  46.     void push(int data)
  47.     {
  48.         Node* p = new Node;
  49.         p->data = data;
  50.         p->next = NULL;
  51.         if (is_empty()) {
  52.             head = tail = p;
  53.         }
  54.         else {
  55.             tail->next = p;
  56.             tail = p;
  57.         }
  58.     }
  59.  
  60.     void show_list()
  61.     {
  62.         Node* p;
  63.         p = head;
  64.         while (p)
  65.         {
  66.             std::cout << p->data << " ";
  67.             p = p->next;
  68.         }
  69.         std::cout << std::endl;
  70.     }
  71.  
  72.     Node* previous(Node* p) {
  73.         Node* pr, *q;
  74.         q = head;
  75.         pr = NULL;
  76.         while (q != p && q) {
  77.             pr = q;
  78.             q = q->next;
  79.         }
  80.         if (q == p) return pr;
  81.         return NULL;
  82.     }
  83.  
  84.     void insert_before(Node* q, int x)
  85.     {
  86.         Node* p, *r;
  87.         p = new Node;
  88.         r = previous(q);
  89.         if (q == tail)
  90.             push(x);
  91.         else
  92.             if (r == NULL)
  93.             {
  94.                 std::cout << " sorry, impossible insert element " << std::endl;
  95.                 exit(-1);
  96.             }
  97.             else
  98.             {
  99.                 p->data = x;
  100.                 p->next = r->next;
  101.                 r->next = p;
  102.             }
  103.     }
  104.  
  105.     void insert_after(Node* &q, int x)
  106.     {
  107.         Node* p;
  108.         p = new Node;
  109.         p->data = x;
  110.         p->next = q->next;
  111.         q->next = p;
  112.     }
  113.  
  114.     Node* find_address(int x)
  115.     {
  116.         Node* p = head;
  117.         while (p && (p->data) != x)
  118.             p = p->next;
  119.         return p;
  120.     }
  121.  
  122.     void delete_element(Node* p)
  123.     {
  124.         Node* r; int x;
  125.         if (p == tail)
  126.             pop(x);
  127.  
  128.         else
  129.         {
  130.             r = previous(p);
  131.             r->next = p->next;
  132.             delete p;
  133.         }
  134.     }
  135. };
  136.  
  137. int main()
  138. {
  139.     MyList list;
  140.     for (int i = 0; i < 150; i++) {
  141.         list.push(i);
  142.     }
  143.     list.show_list();
  144.     list.pop();
  145.     list.show_list();
  146.     auto a = list.find_address(20);
  147.     list.insert_after(a, 505);
  148.     list.show_list();
  149.     list.insert_before(a, 404);
  150.     list.show_list();
  151.     list.delete_element(a);
  152.     list.show_list();
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement