Facebook
From Scribby Lion, 2 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 77
  1. #include <iostream>
  2. #include <time.h>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. struct element
  8. {
  9.     int number;
  10.     element* next;
  11. };
  12.  
  13. struct single_list
  14. {
  15.     element* head;
  16.     element* tail;
  17.     int counter;
  18. };
  19.  
  20. void show_single_list(single_list q) {
  21.     element* iterator = q.head;
  22.     while (iterator != nullptr) {
  23.         cout << iterator->number << " ";
  24.         iterator = iterator->next;
  25.     }
  26.     cout << endl;
  27. }
  28.  
  29. void add_tail(single_list& l, int value)
  30. {
  31.     element* el;
  32.     el = new element;
  33.     el->number = value;
  34.     el->next = nullptr;
  35.     if (l.tail != nullptr)
  36.         l.tail->next = el;
  37.     else
  38.         l.head = el;
  39.     l.tail = el;
  40.     l.counter++;
  41. }
  42. void add_head(single_list& l, int value)
  43. {
  44.     element* el;
  45.     el = new element;
  46.     el->number = value;
  47.     el->next = l.head;
  48.     l.head = el;
  49.     if (l.tail == nullptr)
  50.     {
  51.         l.tail = el;
  52.     }
  53.     l.counter++;
  54. }
  55. void z2(single_list &l)
  56. {
  57.     int liczbarandom=1;
  58.     while (liczbarandom != 0)
  59.     {
  60.         liczbarandom =(rand() % 40) - 20;
  61.         if (liczbarandom < 0)
  62.         {
  63.             add_head(l, liczbarandom);
  64.         }
  65.         if (liczbarandom > 0)
  66.         {
  67.             add_tail(l, liczbarandom);
  68.         }
  69.     }
  70.     show_single_list(l);
  71. }
  72. int main()
  73. {
  74.     srand(time(NULL));
  75.     single_list l;
  76.     l.head = nullptr;
  77.     l.tail = nullptr;
  78.     z2(l);
  79.     return 0;
  80. }
  81.