Advertisement
manger32

Doubly-Linked List

May 26th, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <locale>
  5. #include <iomanip>
  6. #include <ctime>
  7. #include <cmath>
  8.  
  9. using namespace std;
  10.  
  11. struct list
  12. {
  13.     int data;
  14.     list *next, *prev;
  15. };
  16.  
  17. void list_add(int num, list *&head)
  18. {
  19.     if (head == 0)
  20.     {
  21.         head = new list;
  22.         head->next = 0;
  23.         head->prev = 0;
  24.         head->data = num;
  25.     }
  26.     else
  27.     {
  28.         list *temp = head;
  29.         while (temp->next != NULL)
  30.             temp = temp->next;
  31.         list *p = new list;
  32.         p->data = num;
  33.         p->next = 0;
  34.         p->prev = temp;
  35.         temp->next = p;
  36.         temp = temp->next;
  37.     }
  38. }
  39.  
  40. bool list_empty(list *head)
  41. {
  42.     if (head == NULL)
  43.         return 1;
  44.     else
  45.         return 0;
  46. }
  47.  
  48. void list_print(list *head)
  49. {
  50.     if (list_empty(head))
  51.     {
  52.         cout << "Список пуст" << endl;
  53.         return;
  54.     }
  55.     list *temp = head;
  56.     while (temp->next != NULL)
  57.     {
  58.         temp = temp->next;
  59.         cout << temp->data << " ";
  60.     }
  61.     cout << endl;
  62.     temp = head;
  63. }
  64.  
  65. void list_delete_first(list *&head)
  66. {
  67.     if (list_empty(head))
  68.         return;
  69.     if (head->next == NULL)
  70.     {
  71.         list *temp = head;
  72.         delete temp;
  73.         head = 0;
  74.         return;
  75.     }
  76.     list *temp = head;
  77.     head = head->next;
  78.     head->prev = NULL;
  79.     delete temp;
  80. }
  81.  
  82. void list_delete_last(list *&head)
  83. {
  84.     if (list_empty(head))
  85.         return;
  86.     if (head->next == NULL)
  87.     {
  88.         list *temp = head;
  89.         delete temp;
  90.         head = 0;
  91.         return;
  92.     }
  93.     list *temp = head;
  94.     while (temp->next != NULL)
  95.         temp = temp->next;
  96.     temp->prev->next = NULL;
  97.     delete temp;
  98. }
  99.  
  100. void list_delete_n(int pos, list *&head)
  101. {
  102.     if (list_empty(head))
  103.         return;
  104.     if (head->next == NULL)
  105.     {
  106.         list *temp = head;
  107.         delete temp;
  108.         head = 0;
  109.         return;
  110.     }
  111.     if (pos == 1)
  112.     {
  113.         list_delete_first(head);
  114.         return;
  115.     }
  116.     list *temp = head;
  117.     for (int i = 0; i < pos; i++)
  118.         temp = temp->next;
  119.     if (temp->next == NULL)
  120.     {
  121.         list_delete_last(head);
  122.         return;
  123.     }
  124.     temp->next->prev = temp->prev;
  125.     temp->prev->next = temp->next;
  126.     temp->next = 0;
  127.     temp->prev = 0;
  128.     delete temp;
  129. }
  130.  
  131. void list_delete_with_spec_all(int val, list *&head)
  132. {
  133.     list *temp = head;
  134.     int k = 1;
  135.     while (temp != 0)
  136.     {
  137.         if (temp->data == val)
  138.             if (k == 1)
  139.                 list_delete_first(head);
  140.             else if (temp->next == 0)
  141.             {
  142.                 list_delete_last(head);
  143.                 break;
  144.             }
  145.             else
  146.             {
  147.                 temp->next->prev = temp->prev;
  148.                 temp->prev->next = temp->next;
  149.                 list *kill = temp;
  150.                 temp = temp->next;
  151.                 k++;
  152.                 delete kill;
  153.                 continue;
  154.             }
  155.         k++;
  156.         temp = temp->next;
  157.     }
  158. }
  159.  
  160. void list_delete_spec_first(int val, list *&head)
  161. {
  162.     list *temp = head;
  163.     if (temp->data == val)
  164.     {
  165.         list_delete_first(head);
  166.         return;
  167.     }
  168.     else
  169.     {
  170.         while ((temp->data != val) && (temp->next != 0))
  171.             temp = temp->next;
  172.         if ((temp->next == 0) && (temp->data == val))
  173.         {
  174.             list_delete_last(head);
  175.             return;
  176.         }
  177.         temp->next->prev = temp->prev;
  178.         temp->prev->next = temp->next;
  179.         list *kill = temp;
  180.         temp = temp->next;
  181.         delete kill;
  182.     }
  183. }
  184.  
  185. void task();
  186. int main()
  187. {
  188.     setlocale(LC_ALL, "Russian");
  189.     task();
  190.     system("pause");
  191.     return 0;
  192. }
  193.  
  194. void task()
  195. {
  196.     srand(time(0));
  197.     int n;
  198.     cout << "Введите количество элементов списка: ";
  199.     cin >> n;
  200.     list *list_ = 0;
  201.     for (int i = 0; i <= n; i++)
  202.         list_add(i*rand() % 10, list_);
  203.     list_print(list_);
  204.     list_delete_first(list_);
  205.     list_print(list_);
  206.     list_delete_last(list_);
  207.     list_print(list_);
  208.     int pos;
  209.     cout << "Введите позицию элемента, который требуется удалить: ";
  210.     cin >> pos;
  211.     list_delete_n(pos,list_);
  212.     list_print(list_);
  213.     int val;
  214.     cout << "Введите значение, которое требуется удалить: ";
  215.     cin >> val;
  216.     list_delete_with_spec_all(val, list_);
  217.     list_print(list_);
  218.     int val_;
  219.     cout << "Введите значение, которое требуется удалить первым в списке: ";
  220.     cin >> val_;
  221.     list_delete_spec_first(val_, list_);
  222.     list_print(list_);
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement