Advertisement
Aleksandr_Grigoryev

каккака

Apr 27th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.58 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.     while (f.peek()!=EOF)
  30.     {
  31.         f >> x;
  32.         createElem(q, x);
  33.         addFirst(head, q);
  34.     }
  35. }
  36. void setHead(node * head, node *& p)
  37. {
  38.     p = head;
  39. }
  40. bool nodeIsNull(node *p)
  41. {
  42.     return p == NULL;
  43. }
  44. void showNode(node * p)
  45. {
  46.     cout << p->info << ' ';
  47. }
  48. void moove(node *& p)
  49. {
  50.     p = p->next;
  51. }
  52. void showlist(node*head)
  53. {
  54.     node *q;
  55.     setHead(head, q);
  56.     while (!nodeIsNull(q))
  57.     {
  58.         showNode(q);
  59.         moove(q);
  60.     }
  61. }
  62. bool ListIsEmpty(node * head)
  63. {
  64.     return head == NULL;
  65. }
  66. bool CompInfo(node * p, node * q)
  67. {
  68.     return p->info == q->info;
  69. }
  70. void addLast(node * & head1, node * & tail, node * p)
  71. {
  72.     tail->next = p;
  73.     tail = p;
  74. }
  75. void newEl(node*&q, int x)
  76. {
  77.     q = new node;
  78.     q->info = x;
  79.     q->next = NULL;
  80. }
  81. void createListFromFileTail(node * & head1, ifstream & f)
  82. {
  83.     head1 = new node;
  84.     setZero(head1->next);
  85.     node * q, *tail = head1;
  86.     int x;
  87.     while (f.peek() != EOF)
  88.     {
  89.         f >> x;
  90.         createElem(q, x);
  91.         addLast(head1, tail, q);
  92.     }
  93.     q = head1;
  94.     moove(head1);
  95.     delete q;
  96. }
  97. void task26a(node*head1, node*head2, node*& head)
  98. {
  99.     head = new node;
  100.     setZero(head->next);
  101.     node *q1, *q2, *q, *tail;
  102.     setHead(head1, q1);
  103.     setHead(head2, q2);
  104.     setHead(head,tail);
  105.     int x;
  106.     if (ListIsEmpty(head1) && ListIsEmpty(head2))
  107.         cout << "два пустых листа";
  108.     else
  109.     {
  110.         if (ListIsEmpty(head1))
  111.             cout << "Первый лист пустой"<<endl;
  112.          if (ListIsEmpty(head2))
  113.             cout << "Второй лист пустой"<<endl;
  114.     }
  115.     while (!nodeIsNull(q1) && !nodeIsNull(q2))
  116.     {
  117.         if (q1->info <= q2->info)
  118.         {
  119.             x = q1->info;
  120.             moove(q1);
  121.         }
  122.         else
  123.         {
  124.             x = q2->info;
  125.             moove(q2);
  126.         }
  127.         newEl(q, x);
  128.         addLast(head, tail, q);
  129.     }
  130.     if (nodeIsNull(q1))
  131.         while (!nodeIsNull(q2))
  132.         {
  133.             x = q2->info;
  134.             moove(q2);
  135.             newEl(q, x);
  136.             addLast(head, tail, q);
  137.         }
  138.     if (nodeIsNull(q2))
  139.         while (!nodeIsNull(q1))
  140.         {
  141.             x = q1->info;
  142.             moove(q1);
  143.             newEl(q, x);
  144.             addLast(head, tail, q);
  145.         }
  146.     q = head;
  147.     moove(head);
  148.     delete q;
  149. }
  150. void task26b(node*&head1, node*&head2)
  151. {
  152.     node*q1, *q2, *p;
  153.     q2=head2;
  154.     q1=head1;
  155.  
  156.         if (ListIsEmpty(head1))
  157.             cout << "Первый лист пустой" << endl;
  158.  
  159.         if (ListIsEmpty(head2))
  160.             cout << "Второй лист пустой" << endl;
  161.  
  162.         if (!ListIsEmpty(head2) && !ListIsEmpty(head1))
  163.         {
  164.             while (!nodeIsNull(q1->next) && !nodeIsNull(q2->next))
  165.             {
  166.                 while ((head2->info) <= (head1->info))
  167.                 {
  168.                     p = head2;
  169.                     moove(head2);
  170.                     p->next = head1;
  171.                     head1 = p;
  172.                     q2 = head2;
  173.                     q1 = head1;
  174.                 }
  175.                 if (q2->info <= q1->next->info)
  176.                 {
  177.                     p = q2;
  178.                     moove(q2);
  179.                     p->next = q1->next;
  180.                     q1->next = p;
  181.                 }
  182.                 else moove(q1);
  183.             }
  184.             if (nodeIsNull(q1->next) && !nodeIsNull(q2->next))
  185.             {
  186.                 q1->next = q2;
  187.             }
  188.         }
  189. }
  190. int main()
  191. {
  192.     node * L1;
  193.     node * L2;
  194.     node * L3;
  195.     setlocale(LC_ALL, "RUS");
  196.     ifstream f1("input.txt");
  197.     createListFromFileTail(L1, f1);
  198.     showlist(L1);
  199.     f1.close();
  200.     cout << endl;
  201.     ifstream f2("input1.txt");
  202.     createListFromFileTail(L2, f2);
  203.     showlist(L2);
  204.     f2.close();
  205.     cout << endl;
  206.     /*
  207.         task26a(L1, L2, L3);
  208.         showlist(L3);
  209.     */
  210.         task26b(L1, L2);
  211.         showlist(L1);
  212.    
  213.     cout << endl;
  214.     system("pause");
  215.     return 0;
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement