Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct node {
- node* next;
- node* prev;
- int info;
- };
- void createList(node*& Begin, node*& End, int n) {
- for (int i = 0; i < n; i++) {
- node* el = new node;
- el->info = rand() % 101 - 50;
- if (End == nullptr) {
- el->next = NULL;
- el->prev = NULL;
- Begin = End = el;
- }
- else {
- el->next = NULL;
- el->prev = End;
- End->next = el;
- End = el;
- }
- }
- }
- bool isEmpty(node* Begin) {
- if (Begin == nullptr)
- cout << "Очередь пуста\n";
- return Begin == nullptr;
- }
- void outList(node* Begin) {
- if (isEmpty(Begin))
- return;
- node* temp = Begin;
- while (temp != NULL) {
- cout << temp->info << " ";
- temp = temp->next;
- }
- }
- node* search_min(node*& Begin, node*& End, int& min_pos)
- {
- int i = 1;
- node* min = Begin;
- node* spt = Begin;
- while (spt != nullptr)
- {
- if (spt->info < min->info) { min = spt; min_pos = i; }
- spt = spt->next;
- i++;
- }
- return min;
- }
- node* search_max(node*& Begin, node*& End, int& max_pos)
- {
- int i = 1;
- node* max = Begin;
- node* spt = Begin;
- while (spt != nullptr)
- {
- if (spt->info > max->info) { max = spt; max_pos = i; }
- spt = spt->next;
- i++;
- }
- return max;
- }
- void del_between(node*& Begin, node*& End)
- {
- int min_pos = 0, max_pos = 0;
- node* min = search_min(Begin, End, min_pos);
- node* max = search_max(Begin, End, max_pos);
- if (min_pos == max_pos) {
- std::cout << "Пустая область между min и max" << std::endl;
- return;
- }
- if (min_pos > max_pos) {
- node* temp = min;
- min = max;
- max = temp;
- }
- node* saveToFree;
- while (min->next != max) {
- saveToFree = min->next;
- min->next = min->next->next;
- delete saveToFree;
- }
- min_pos = 0;
- max_pos = 0;
- }
- int main() {
- srand((unsigned)(time(NULL)));
- setlocale(LC_ALL, "Rus");
- int n;
- node* Begin = nullptr, * End = nullptr;
- cout << "Введите количество узлов: ";
- cin >> n;
- createList(Begin, End, n);
- cout << endl;
- outList(Begin);
- del_between(Begin, End);
- cout << endl << endl;
- outList(Begin);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment