Advertisement
Guest User

16.24

a guest
May 16th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.64 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. struct node
  7. {
  8.     int data;
  9.     node * next;
  10. };
  11. void setNull(node *&p)
  12. {
  13.     p = NULL;
  14. }
  15. void createElement(node *&p, int y)
  16. {
  17.     p = new node;
  18.     p->data = y;
  19.     p->next = NULL;
  20. }
  21. void addFirst(node *&head1, node *p)
  22. {
  23.     p->next = head1;
  24.     head1 = p;
  25. }
  26. void addLast(node *&head1, node *&tail, node *p)
  27. {
  28.     tail->next = p;
  29.     tail = p;
  30. }
  31. void setHead(node *head1, node *&p)
  32. {
  33.     p = head1;
  34. }
  35. bool nodeIsNull(node *p)
  36. {
  37.     return p == NULL;
  38. }
  39. bool nextNodeIsNull(node *p)
  40. {
  41.     return p->next == NULL;
  42. }
  43. bool next2NodeIsNull(node *p)
  44. {
  45.     return p->next->next == NULL;
  46. }
  47. void showNode(node *p)
  48. {
  49.     cout << p->data << ' ';
  50. }
  51. void move(node *&p)
  52. {
  53.     p = p->next;
  54. }
  55. bool compareData(node *p1, node *p2)
  56. {
  57.     return p1->data == p2->data;
  58. }
  59. void nextNull(node *d)
  60. {
  61.     d->next = NULL;
  62. }
  63. void nextNode(node *&p, node *d)
  64. {
  65.     p->next = d;
  66. }
  67. void nodeNext(node *&p, node *d)
  68. {
  69.     p = d->next;
  70. }
  71. bool listIsEmpty(node *head1)
  72. {
  73.     return head1 == NULL;
  74. }
  75.  
  76. void createListFromFile(node * &head1, ifstream &f)
  77. {
  78.     node * q; int x;
  79.     setNull(head1);
  80.     while (f.peek()!=EOF)
  81.     {
  82.         f >> x;
  83.         createElement(q, x);
  84.         addFirst(head1, q);
  85.     }
  86. }
  87. void createListFromFileTail(node * &head1, ifstream &f)
  88. {
  89.     head1 = new node;
  90.     head1->next = NULL;
  91.     node * tail = head1;
  92.     node * q;
  93.     int x;
  94.     while (f.peek() != EOF)
  95.     {
  96.         f >> x;
  97.         createElement(q, x);
  98.         addLast(head1, tail, q);
  99.     }
  100.     setHead(head1, q);
  101.     move(head1);
  102.     delete q;
  103. }
  104. void showList(node * head1)
  105. {
  106.     if (head1) {
  107.         node * q;
  108.         setHead(head1, q);
  109.         while (!nodeIsNull(q))
  110.         {
  111.             showNode(q);
  112.             move(q);
  113.         }
  114.         cout << endl;
  115.     }
  116.     else cout << "Список пустой" << endl;
  117. }
  118. bool point_A(node *head, int x) // входит ли элемент Х в список
  119. {
  120.     node *p, *q = new node;
  121.     q->data = x;
  122.     setHead(head, p);
  123.     while (p)
  124.     {
  125.         if (compareData(p, q))
  126.             return true;
  127.         else {
  128.             if (nextNodeIsNull(p))
  129.             {
  130.                 return false;
  131.                 break;
  132.             }
  133.             else
  134.             {
  135.                 move(p);
  136.                 point_A(p, x);
  137.             }
  138.         }
  139.     }
  140. }
  141. int point_B(node *head, int x) // подсчитывает кол-во вхождений Х в список
  142. {
  143.     node *p, *q = new node;
  144.     q->data = x;
  145.     setHead(head, p);
  146.     int k = 0;
  147.     bool t = false;
  148.     while (p)
  149.     {
  150.         if (compareData(p, q))
  151.         {
  152.             t = true;
  153.             k++;
  154.             move(p);
  155.             point_B(p, x);
  156.         }
  157.         else {
  158.             if (!nextNodeIsNull(p))
  159.             {
  160.                 move(p);
  161.                 point_B(p, x);
  162.             }
  163.             else break;
  164.         }
  165.     }
  166.     return k;
  167. }
  168. int point_V(node *head) // макс элемент в списке
  169. {
  170.     node *p;
  171.     setHead(head, p);
  172.     int max = head->data;
  173.     if (p->data > max)
  174.     {
  175.         max = p->data;
  176.         move(p);
  177.     }
  178.     else {
  179.         if (!nextNodeIsNull(p))
  180.         {
  181.             move(p);
  182.             point_V(p);
  183.         }
  184.         return max;
  185.     }
  186. }
  187. void point_G(node *&head1) // печатает в обратном порядке
  188. {
  189.     if (nextNodeIsNull(head1))
  190.         cout << head1->data << " ";
  191.     else
  192.     {
  193.         point_G(head1->next);
  194.         cout << head1->data << " ";
  195.     }
  196. }
  197. void point_D(node *&head1, int x, int y) // заменяет все вхождения X на Y
  198. {
  199.     node *q, *p = new node;
  200.     p->data = y;
  201.     setHead(head1, q);
  202.     while (q)
  203.     {
  204.         if (q->data == x)
  205.         {
  206.             q->data = p->data;
  207.             move(q);
  208.         }
  209.         else {
  210.             if (!nextNodeIsNull(q))
  211.             {
  212.                 move(q);
  213.                 point_D(q, x, y);
  214.             }
  215.             else break;
  216.         }
  217.     }
  218. }
  219. node* prev(node* head, node* p)
  220. {
  221.     node* pr, *q;
  222.     q = head;
  223.     pr = NULL;
  224.     while (q != p && q)
  225.     {
  226.         pr = q;
  227.         q = q->next;
  228.     }
  229.  
  230.     if (q == p) return pr;
  231.     return NULL;
  232. }
  233. void point_E(node*&head, int x) // удаляет первое вхождение эл-та Х
  234. {
  235.     node*q, *p;
  236.     setHead(head, q);
  237.     setNull(p);
  238.     if (head->data == x)
  239.     {
  240.         head = head->next;
  241.         q->next = NULL;
  242.         delete q;
  243.     }
  244.     else
  245.         if (!nextNodeIsNull(q))
  246.             if (head->next->data == x)
  247.             {
  248.                 nodeNext(p, q);
  249.                 q->next = q->next->next;
  250.                 delete p;
  251.             }
  252.             else point_E(q->next, x);
  253. }
  254. void point_J(node*&head, int x) // удаляет все вхождения эл-та Х
  255. {
  256.     node *p, *q = new node, *r;
  257.     q->data = x;
  258.     setHead(head, p);
  259.     while (p)
  260.     {
  261.         if (compareData(p, q))
  262.         {
  263.             node * w = p;
  264.             r = prev(head, p);
  265.             r->next = p->next;
  266.             delete w;
  267.             move(p);
  268.             point_J(p, x);
  269.         }
  270.         else {
  271.             if (!nextNodeIsNull(p))
  272.             {
  273.                 move(p);
  274.                 point_J(p, x);
  275.             }
  276.             else break;
  277.         }
  278.     }
  279. }
  280. void point_Z(node *&head1, node *&head2) // строит копию списка
  281. {
  282.     node *q1, *q2;
  283.     setHead(head1, q1);
  284.     setNull(head2);
  285.     while (q1) {
  286.         createElement(q2, q1->data);
  287.         addFirst(head2, q2);
  288.         move(q1);
  289.         point_Z(q1, q2);
  290.     }
  291.     point_G(head2);
  292. }
  293. void point_I(node *&head, int x) // удваивает каждое вхождение элемента Х
  294. {
  295.     if (head)
  296.     {
  297.         node *q, *p;
  298.         setHead(head, q);
  299.         if (q)
  300.         {
  301.             if (q->data == x)
  302.             {
  303.                 p = new node;
  304.                 p->data = x;
  305.                 nextNode(p, q->next);
  306.                 nextNode(q, p);
  307.                 move(q);
  308.             }
  309.             if (q)
  310.                 point_I(q->next, x);
  311.         }
  312.     }
  313. }
  314. int point_K(node *head) // находит сред ариф значение всех элементов
  315. {
  316.     int sum = 0, c = 0;
  317.     double n;
  318.     while (head)
  319.     {
  320.         sum = sum + head->data;
  321.         c += 1;
  322.         if (!nextNodeIsNull(head))
  323.         {
  324.             move(head);
  325.             point_K(head);
  326.         }
  327.         else break;
  328.     }
  329.     n = sum / c;
  330.     return n;
  331. }
  332. int main()
  333. {
  334.     setlocale(LC_ALL, "Russian");
  335.  
  336.     ifstream f("Text.txt");
  337.     node *head1 = NULL;
  338.     createListFromFileTail(head1, f);
  339.     cout << "Ваш список А: " << endl;
  340.     showList(head1);
  341.     cout << endl;
  342.  
  343.     if (head1) {
  344.         //cout << "Входит ли элемент в список?: " << point_A(head1, 2) << endl; // А
  345.         //cout << "Максимальный элемент в списке = " << point_V(head1) << endl; // В
  346.         //cout << "Среднее ариф-е элементов списка = " << point_K(head1) << endl; // K
  347.  
  348.         //cout << "Переворачивает список: "; // Г
  349.         //point_G(head1); cout << endl;
  350.  
  351.         node *head2;
  352.         //cout << "Копия списка: "; point_Z(head1, head2); showList(head2); // З
  353.  
  354.         if (point_A(head1, 1))
  355.         {
  356.             //cout << "Сколько раз входит Х в список?: " << point_B(head1, 1) << endl; // Б
  357.             //cout << "Заменяет все вхождения Х на Y: "; point_D(head1, 1, 3); showList(head1); // Д
  358.               cout << "Удаляет первое вхождение Х: "; point_E(head1, 1); showList(head1); // Е
  359.             //cout << "Удваивает каждое вхождение Х: "; point_I(head1, 1); showList(head1); // И
  360.         }
  361.         else cout << "Нет такого элемента" << endl;
  362.     }
  363.     else cout << "Список пустой" << endl;
  364.  
  365.     system("pause");
  366.     return 0;
  367. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement