Advertisement
Domerk

Списки

Nov 25th, 2013
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.35 KB | None | 0 0
  1. // создать список
  2. // оставить только первые вхождения одинаковых элементов
  3. // functions.h
  4.  
  5. #ifndef FUNCTIONS_BRIDGE_H
  6. #define FUNCTIONS_BRIDGE_H
  7.  
  8.  
  9.  
  10. struct elements;
  11.  
  12. struct elements
  13. {                    
  14.         char inf;
  15.         elements* ref;
  16. };
  17.  
  18. void insert (elements*& head, char value);
  19. bool search (elements* head, char value);
  20. void handling (elements*& head);
  21. void print (elements* head);
  22. void erase (elements*& head);
  23. int length (elements* head);
  24.  
  25.  
  26.  
  27. #endif
  28.  
  29. //===========================================================
  30.  
  31. // functions.cpp
  32.  
  33. #include "functions.h"
  34. #include <iostream>
  35.  
  36. using namespace std;
  37.  
  38. void insert(elements*& head, char value)
  39. {
  40.         elements*p=new elements;
  41.         p->inf=value;
  42.         p->ref=head;
  43.         head=p;
  44. }
  45.  
  46. // печать списка
  47. void print(elements* head)
  48. {
  49.     while(head)                           // пока элемент существует
  50.         {
  51.                 cout << head->inf << " ";       // вывести его инфу
  52.                 head = head->ref;                   // перейти к следующему
  53.         }
  54. }
  55.  
  56.  
  57. void erase(elements*& head)
  58. {
  59.         elements* th;                      // вспомогательный указатель
  60.         while(head)                           // пока есть текущий элемент
  61.         {
  62.                 th = head->ref;                  // запоминаем следующий элемент
  63.                 delete head;                      // удаляем текущий
  64.                 head = th;                        // переходим к следующему
  65.         }
  66. }
  67.  
  68. int length (elements* head)
  69. {
  70.     int i=0;
  71.     while(head)                           // пока элемент существует
  72.         {
  73.                 head = head->ref;
  74.                 i++;                 // перейти к следующему
  75.         }
  76.     return i;
  77. }
  78.  
  79. bool search (elements* head, char value)
  80. {
  81.     elements*p=new elements;
  82.    
  83.     while (head) // пока элемент существует
  84.         {
  85.                 if (value==head->inf)
  86.                     {
  87.                         cout<<endl<<"Element!"<<endl<<head->inf;
  88.                         return true;
  89.                 }
  90.                 head=head->ref;                   // перейти к следующему
  91.         }
  92.  
  93. }
  94.  
  95. void handling (elements*& head)
  96. {
  97.     if (!head)
  98.     {
  99.         cout<<"List does not exist"<<endl;
  100.         return;
  101.     }
  102.     elements*p1=new elements;
  103.     elements*p2=new elements;
  104.     elements*p3=new elements;
  105.  
  106.     int n = length(head);
  107.     cout<<endl<<n<<endl;
  108.  
  109.     if (n>=3)
  110.     {
  111.         p1=head->ref;
  112.         p2=head->ref->ref;
  113.         p3=head->ref->ref->ref;
  114.         for (int i = 0; i<n; i++)
  115.         {
  116.             if (search(p2, p2->inf))
  117.             {
  118.                 p1=p3;
  119.                 delete p2;
  120.                 n--;
  121.             }
  122.             else
  123.             {
  124.                 p1=p1->ref;
  125.                 p2=p2->ref;
  126.                 p3=p3->ref;
  127.             }
  128.         }
  129.     }
  130.  
  131. }
  132.  
  133. //===============================================
  134.  
  135. //main.cpp
  136.  
  137. #include <iostream>
  138. #include "functions.h"
  139.  
  140. using namespace std;
  141.  
  142. int main ()
  143. {
  144.     int key = 1;
  145.     char value;
  146.     elements* head = 0;
  147.  
  148.     while (key)
  149.     {
  150.         cout<<"New value: ";
  151.         cin>>value;
  152.         cout<<endl;
  153.         insert (head, value);
  154.         cout<<"Press 0 to exit, 1 to continue: ";
  155.         cin>>key;
  156.     }
  157.  
  158.     print (head);
  159.     handling (head);
  160.     print (head);
  161.     erase(head);
  162.  
  163.     cin.sync();
  164.     cin.clear();
  165.     cin.get();
  166.  
  167.     return 0;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement