Aleksandr_Grigoryev

23

Apr 18th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. struct node
  5. {
  6.     int info;
  7.     node* next;
  8. };
  9. void createElem(node *& p, int x)
  10. {
  11.     p = new node;
  12.     p->info = x;
  13.     p->next = NULL;
  14. }
  15. void addFirst(node *& head, node * p)
  16. {
  17.     p->next = head;
  18.     head = p;
  19. }
  20. void setZero(node *& p)
  21. {
  22.     p = NULL;
  23. }
  24. void createlistFromFile(node *& head, ifstream & f)
  25. {
  26.     node*q;
  27.     int x;
  28.     setZero(head);
  29.    
  30.         while (!f.eof())
  31.         {
  32.             f >> x;
  33.             createElem(q, x);
  34.             addFirst(head, q);
  35.         }
  36. }
  37. void setHead(node * head, node *& p)
  38. {
  39.     p = head;
  40. }
  41. bool nodeIsNull(node *p)
  42. {
  43.     return p == NULL;
  44. }
  45. void showNode(node * p)
  46. {
  47.     cout << p->info << ' ';
  48. }
  49. void moove(node *& p)
  50. {
  51.     p = p ->next;
  52. }
  53. void showlist(node*head)
  54. {
  55.     node *q;
  56.     setHead(head,q);
  57.     while (!nodeIsNull(q))
  58.     {
  59.         showNode(q);
  60.         moove(q);
  61.     }
  62. }
  63. bool ListIsEmpty(node * head)
  64. {
  65.     return head == NULL;
  66. }
  67. bool CompInfo(node * p, node * q)
  68. {
  69.     return p->info == q->info;
  70. }
  71. void LastEl(node *& p)
  72. {
  73.     while (!nodeIsNull(p->next))
  74.         moove(p);
  75. }
  76. void PrevEl(node*&q, node*&p)
  77. {
  78.     while (!nodeIsNull(q->next->next))
  79.         moove(q);
  80.     p = q->next;
  81. }
  82. bool task23a(node * L1, node * L2)
  83. {
  84.     node *l1;
  85.     node *l2;
  86.     setHead(L1,l1);
  87.     setHead(L2,l2);
  88.     bool t = true;
  89.     while (!nodeIsNull(l1) && !nodeIsNull(l2) && t)
  90.     {
  91.         if (!CompInfo(l1, l2))
  92.             t = false;
  93.         moove(l1);
  94.         moove(l2);
  95.     }
  96.     return t && nodeIsNull(l1) && nodeIsNull(l2);
  97. }
  98. bool task23c(node * L1)
  99. {
  100.     node *l1;
  101.     node *l2;
  102.     setHead(L1, l1);
  103.     bool f = false;
  104.     while (!nodeIsNull(l1))
  105.     {
  106.         l2 = l1->next;
  107.         while (!nodeIsNull(l2) )
  108.         {
  109.             if (l1->info == l2->info)
  110.             {
  111.                 f = true;
  112.             }
  113.                 moove(l2);
  114.         }
  115.         moove(l1); 
  116.     }
  117.     return f;
  118. }
  119.  
  120. void task23d(node *& L1)
  121. {
  122.     if (nodeIsNull(L1->next))
  123.     {
  124.         cout << "error!" << endl;
  125.     }
  126.     else
  127.     {
  128.         node *q, *p;
  129.         setHead(L1, p);
  130.         moove(L1);
  131.         setHead(L1, q);
  132.         while (!nodeIsNull(q->next))
  133.         {
  134.             moove(q);
  135.         }
  136.         q->next=p;
  137.         p->next = NULL;
  138.     }
  139. }
  140.  
  141. void task23e(node *& L1)
  142. {
  143.     if (nodeIsNull(L1->next))
  144.     {
  145.         cout << "error!" << endl;
  146.     }
  147.     else
  148.     {
  149.         node *l1;
  150.         setHead(L1, l1);
  151.         node *l2 = new node;
  152.         while (!nodeIsNull(l1->next->next))
  153.             moove(l1);
  154.         l2 = l1->next;
  155.         l1->next = NULL;
  156.         l2->next = L1;
  157.         L1 = l2;
  158.     }
  159. }
  160.  
  161. bool task23B(node *head1, node *head2)
  162. {
  163.     if (!nodeIsNull(head1) && !nodeIsNull(head2))
  164.     {
  165.         node *q = head1;
  166.         node *p = head2;
  167.         bool allT = true;
  168.         bool E;
  169.         while (p != NULL && allT)
  170.         {
  171.             E = false;
  172.             while (q != NULL && !E && (p->info >= q->info))
  173.             {
  174.                 if (CompInfo(p, q))
  175.                     E = true;
  176.                 else
  177.                     moove(q);
  178.             }
  179.             if (!E)
  180.                 allT = false;
  181.             moove(p);
  182.         }
  183.         return allT;
  184.     }
  185.     else
  186.         return false;
  187. }
  188. bool check(node*head1, node*head2)
  189. {
  190.     bool t = true;
  191.     node *l1, *l2;
  192.     setHead(head1, l1);
  193.     setHead(head2, l2);
  194.     while (t && !nodeIsNull(l1) && !nodeIsNull(l2))
  195.     {
  196.         if (!(CompInfo(l1, l2)))
  197.         t = false;
  198.         moove(l1);
  199.         moove(l2);
  200.     }
  201.     return (t&&nodeIsNull(l2));
  202. }
  203.  
  204. bool task23b_Not_Upor_Point(node * L1, node * L2)
  205. {
  206.     node *p1, *p2;
  207.     setHead(L1, p1);
  208.     setHead(L2, p2);
  209.     bool f = false;
  210.     while (!nodeIsNull(p1) && !f)
  211.     {
  212.         if (check(p1, p2))
  213.             f = true;
  214.  
  215.         moove(p1);
  216.     }
  217.     return f;
  218. }
  219.  
  220. void task23f(node *& L1, node *& L2)
  221. {  
  222.     if (L1->next == NULL)
  223.         L1 = L2;
  224.     else
  225.     {
  226.         node *l1, *l2;
  227.         node *l3 = new node;
  228.         setHead(L1, l1);
  229.         setHead(L2, l2);
  230.         while (!nodeIsNull(l2))
  231.         {
  232.             l3->next = NULL;
  233.             while (!nodeIsNull(l1->next))
  234.             {
  235.                 moove(l1);
  236.             }
  237.             l3 = l2;
  238.             l1->next = l3;
  239.             moove(l2);
  240.         }
  241.     }
  242. }
  243.  
  244.  
  245. void task23g(node *& L1, int x, node *& L2)
  246. {
  247.     if (nodeIsNull(L1))
  248.         cout << "eror1" << endl;
  249.     else  if (nodeIsNull(L2))
  250.         cout << "eror2" << endl;
  251.     else
  252.     {
  253.         node* q1, *q2, *p;
  254.         bool fl = true;
  255.         setHead(L1, q1);
  256.         setHead(L2, q2);
  257.         while (!nodeIsNull(q1) && fl)
  258.         {
  259.             if (q1->info == x)
  260.             {
  261.                 p=q1->next;
  262.                 q1->next=q2;
  263.                 LastEl(q2);
  264.                 q2->next=p;
  265.                 fl = false;
  266.             }
  267.             else
  268.                 moove(q1);
  269.         }
  270.     }
  271. }
  272.  
  273. void task23z(node *& L1)
  274. {
  275.     if (L1->next == NULL)
  276.         cout << "eror" << endl;
  277.     else if (nodeIsNull(L1->next))
  278.     {
  279.         node *l1;
  280.         l1 = L1->next,
  281.             L1->next = NULL;
  282.         l1->next = L1;
  283.         L1 = l1;
  284.     }
  285.     else
  286.     {
  287.         node* l2 = NULL, *l3, *l4;
  288.         setHead(L1, l4);
  289.         LastEl(L1);
  290.         while (l2 != l4)
  291.         {
  292.             setHead(l4, l2);
  293.             PrevEl(l2, l3);
  294.             l3->next = l2;
  295.             l2->next = NULL;
  296.         }
  297.     }
  298. }
  299. void task23i(node *& L1)
  300. {
  301.     node *l1,*l2,*l3,*l4;
  302.     setHead(L1, l1);
  303.     if (L1->next == NULL)
  304.         cout << "eror ";
  305.     else
  306.     {
  307.         while (!nodeIsNull(l1->next->next))
  308.         {
  309.             if (l1->info == l1->next->info)
  310.             {
  311.                 l2 = l1->next;
  312.                 l1->next = l1->next->next;
  313.                 delete l2;
  314.             }
  315.             else
  316.                 moove(l1);
  317.         }
  318.         if (l1->next->info == l1->info)
  319.         {
  320.             l3 = l1;
  321.             l4 = l3->next;
  322.             l3->next = NULL;
  323.             delete l4;
  324.         }
  325.     }
  326. }
  327. void deleteElement(node *& q)
  328. {
  329.     node*p;
  330.     p=q->next;
  331.     q->next=q->next->next;
  332.     delete p;
  333. }
  334. void task23k(node *& L1)
  335. {
  336.     if (!nodeIsNull(L1))
  337.     {
  338.         node *p;
  339.         node *q = L1;
  340.         node *c;
  341.         int Neto;
  342.         while (!nodeIsNull(q))
  343.         {
  344.             p = q;
  345.             c = q;
  346.             moove(p);
  347.             Neto = q->info;
  348.             while (!nodeIsNull(p))
  349.             {
  350.                 if (Neto == p->info)
  351.                 {
  352.                     moove(p);
  353.                     deleteElement(c);
  354.                 }
  355.                 else
  356.                 {
  357.                     moove(p);
  358.                     moove(c);
  359.                 }
  360.             }
  361.             moove(q);
  362.         }
  363.         showlist(L1);
  364.     }
  365.  
  366. }
  367. int main()
  368. {
  369.     node * L1;
  370.     node * L2;
  371.     setlocale(LC_ALL, "RUS");
  372.     ifstream f1("input.txt");
  373.     createlistFromFile(L1, f1);
  374.     showlist(L1);
  375.     f1.close();
  376.     cout << endl;
  377.     ifstream f2("input2.txt");
  378.     createlistFromFile(L2, f2);
  379.     showlist(L2);
  380.     f2.close();
  381.     cout << endl;
  382.  
  383.     /*  if (!ListIsEmpty(L1) && !ListIsEmpty(L2))
  384.         cout << task23a(L1,L2);
  385.     */
  386.  
  387.    
  388.         if (!ListIsEmpty(L1) && !ListIsEmpty(L2))
  389.         cout << task23B(L1, L2);
  390.  
  391.  
  392.     /*if (!ListIsEmpty(L1) && !ListIsEmpty(L2))
  393.         cout<< task23b_Not_Upor_Point(L1, L2);
  394.     */
  395.     /*
  396.         if (!ListIsEmpty(L1))
  397.         cout << task23c(L1);
  398.     */
  399.     /*
  400.             task23d(L1);
  401.             showlist(L1);
  402.     */
  403.     /*
  404.         task23e(L1);
  405.         showlist(L1);
  406.     */
  407.     /*
  408.         task23f(L1, L2);
  409.         showlist(L1);
  410.     */
  411.  
  412.     /*
  413.         if (!ListIsEmpty(L1) && !ListIsEmpty(L2))
  414.         {
  415.             task23g(L1, 2, L2);
  416.             showlist(L1);
  417.         }
  418.     */
  419.  
  420.     /*
  421.             task23z(L1);
  422.             showlist(L1);
  423.     */
  424.     /*
  425.             task23i(L1);
  426.             showlist(L1);
  427.     */
  428.  
  429. //  task23k(L1);
  430.  
  431.     cout << endl;
  432.     system("pause");
  433.     return 0;
  434. }
Advertisement
Add Comment
Please, Sign In to add comment