Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- struct list
- {
- int value;
- list* next;
- };
- list * Add(list*& list_, int value)
- {
- list* node = new list;
- node->value = value;
- node->next = list_;
- list_ = node;
- return list_;
- }
- list* FillRandom(list*& list_, int count)
- {
- for (; count; --count)
- {
- Add(list_, rand() % (2*(count+1)));
- }
- return list_;
- }
- void Print(const list* list_)
- {
- for (; list_; list_ = list_->next)
- {
- cout << list_->value << ' ';
- }
- cout << endl;
- }
- list* DeleteAfter(list* list_, int value)
- {
- list* node = list_;
- list* find = NULL;
- for (; node; node = node->next)
- {
- if (node->value == value)
- {
- find = node;
- }
- else if (find)
- {
- find->next = node->next;
- delete node;
- node = find->next;
- find = NULL;
- }
- }
- return list_;
- }
- int main()
- {
- srand(time(NULL));
- cout << "input count elements: ";
- int count;
- cin >> count;
- list* list_ = NULL;
- FillRandom(list_, count);
- Print(list_);
- cout << "input value element: ";
- int value;
- cin >> value;
- DeleteAfter(list_, value);
- Print(list_);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement