Advertisement
xotohop

list_Dasha.cpp

Apr 11th, 2020
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. struct list
  8. {
  9.    int value;
  10.    list* next;
  11. };
  12.  
  13. list * Add(list*& list_, int value)
  14. {
  15.    list* node = new list;
  16.    node->value = value;
  17.    node->next = list_;
  18.    list_ = node;
  19.  
  20.    return list_;
  21. }
  22.  
  23. list* FillRandom(list*& list_, int count)
  24. {
  25.    for (; count; --count)
  26.    {
  27.       Add(list_, rand() % (2*(count+1)));
  28.    }
  29.  
  30.    return list_;
  31. }
  32.  
  33. void Print(const list* list_)
  34. {
  35.    for (; list_; list_ = list_->next)
  36.    {
  37.       cout << list_->value << ' ';
  38.    }
  39.    cout << endl;
  40. }
  41.  
  42. list* DeleteAfter(list* list_, int value)
  43. {
  44.    list* node = list_;
  45.    list* find = NULL;
  46.  
  47.    for (; node; node = node->next)
  48.    {
  49.       if (node->value == value)
  50.       {
  51.          find = node;
  52.       }
  53.       else if (find)
  54.       {
  55.          find->next = node->next;
  56.          delete node;
  57.          node = find->next;
  58.          find = NULL;
  59.       }
  60.    }
  61.  
  62.    return list_;
  63. }
  64.  
  65. int main()
  66. {
  67.    srand(time(NULL));
  68.  
  69.    cout << "input count elements: ";
  70.    int count;
  71.    cin >> count;
  72.  
  73.    list* list_ = NULL;
  74.  
  75.    FillRandom(list_, count);
  76.    Print(list_);
  77.  
  78.    cout << "input value element: ";
  79.    int value;
  80.    cin >> value;
  81.  
  82.    DeleteAfter(list_, value);
  83.    Print(list_);
  84.  
  85.    return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement