Advertisement
Domerk

Списки (черновик)

Nov 9th, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. // functions.h
  2.  
  3. #ifndef FUNCTIONS_BRIDGE_H
  4. #define FUNCTIONS_BRIDGE_H
  5.  
  6.  
  7.  
  8. struct elements;
  9.  
  10. struct elements
  11. {                    
  12.         char inf;
  13.         elements* ref;
  14. };
  15.  
  16. bool existe (elements* head);
  17.  
  18. void insert (elements*& head, char value);
  19.  
  20. void handling (elements* head);
  21. void print (elements* h);
  22. void erase (elements* h);
  23.  
  24. #endif
  25.  
  26. //main.cpp
  27.  
  28. #include <iostream>
  29. #include "functions.h"
  30.  
  31. using namespace std;
  32.  
  33. int main ()
  34. {
  35.     int key = 1;
  36.     char value;
  37.     elements* head = 0;
  38.  
  39.     while (key)
  40.     {
  41.         cout<<"New value: ";
  42.         cin>>value;
  43.         cout<<endl;
  44.         insert (head, value);
  45.         cout<<"Press 0 to exit, 1 to continue";
  46.         cin>>key;
  47.     }
  48.  
  49.      print (head);
  50.     /*handling (head);
  51.     print (head);
  52.     erase(head);*/
  53.  
  54.     cin.sync();
  55.     cin.clear();
  56.     cin.get();
  57.  
  58.     return 0;
  59. }
  60.  
  61. // functions.cpp
  62.  
  63. #include "functions.h"
  64. #include <iostream>
  65.  
  66. using namespace std;
  67.  
  68. bool existe (elements* head)
  69. {
  70.     if (head)
  71.         return true;
  72.     else
  73.         return false;
  74. }
  75.  
  76.  
  77. void insert(elements*& head, char value)
  78. {
  79.     elements *actual = 0;
  80.  
  81.     if (existe (head)) // если голова существует
  82.     {
  83.         actual = new elements;             // создаем новый элемент
  84.         (*actual).inf = value;  
  85.         (*actual).ref = 0;
  86.     }
  87.     else
  88.     {
  89.         head = new elements;          // ни одного элемента нет! создаем первый, указываем на него головой
  90.         (*head).inf = value;               // заполняем инфу
  91.         (*head).ref = 0;
  92.     }
  93. }
  94.  
  95. // печать списка
  96. void print(elements* h)
  97. {
  98.     while(h)                           // пока элемент существует
  99.         {
  100.                 cout << (*h).inf << " ";       // вывести его инфу
  101.                 h = (*h).ref;                   // перейти к следующему
  102.         }
  103. }
  104.  
  105.  
  106. void erase(elements*& head)
  107. {
  108.         elements* th;                      // вспомогательный указатель
  109.         while(head)                           // пока есть текущий элемент
  110.         {
  111.                 th = (*head).ref;                  // запоминаем следующий элемент
  112.                 delete head;                      // удаляем текущий
  113.                 head = th;                        // переходим к следующему
  114.         }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement