There is a code:

#include <iostream> #include <limits> #include <conio.h> void addElements(stack *&stack1); int menu(); void input(int &a); void searchMaxValue(stack *stack1); using namespace std; struct stack { int inf; stack *head, *next; }; int main() { stack *stack1 = new stack; stack1->head = NULL; while (true) { switch (menu()) { case 1: cout << "Enter the element:" << endl; addElements(stack1); cout << "Element added" << endl; break; case 4: searchMaxValue(stack1); break; case 0: cout << "Press Enter if you want to exit" << endl; if (_getch() == 13) { delete stack1->head; delete stack1->next; delete stack1; return 0; } break; default: cout << "Choose 1-4 or 0" << endl; break; } } } void addElements(stack *&stack1) { int inf; input(inf); stack *temp = new stack; temp->inf = inf; temp->next = stack1->head; stack1->head = temp; } int menu() { cout << "1 - add element" << endl; cout << "2 - show stack" << endl; cout << "3 - clear stack" << endl; cout << "4 - delete max element" << endl; cout << "0 - exit" << endl; int choise; input(choise); return choise; } void input(int &a) { while (true) { cin >> a; if (cin.good()) { break; } cout << "Wrong input" << endl; cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); } } void searchMaxValue(stack *stack1) { stack *temp = stack1->head; int i = 0, n = 0; while (temp != NULL) { temp = temp->next; n++; } int *arr = new int[n]; while (temp != NULL) { arr[i] = temp->inf; temp = temp->next; i++; } for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; } 

The task is to find and delete the maximum stack item, but have not completed it yet. I decided to implement it as follows: fill the stack, transfer everything from the stack to the array, delete the maximum element in the array, and then return everything back to the stack. However, when transferring from the stack to an array with any contents of the stack, I get:

-842150451 in each cell of the array. I understand that this is due to the fact that all the elements of the array remain empty, but I do not understand why.

    1 answer 1

    I still do not understand what you have with the array, but the idea itself is doubtful. With the same success, you can search for items directly in the list, especially if it is sorted. You can generally store items in a sorted array and not bathe. If you still use lists, it is easier to handle deleting and adding if there are pointers to the beginning and end of the list.

    I have altered your version a bit, I hope you will understand more clearly how to work with lists.

     #include <iostream> #include <limits> using namespace std; struct stack { int value; stack *head, *tail; }; stack* addElements(stack *oldTail, int value) { stack *newTail = new stack{ value, oldTail, nullptr }; if(oldTail) oldTail->tail = newTail; return newTail; } stack* findMax(stack* head) { if(head == nullptr) return nullptr; stack* ret = head; for(stack* tail = head->tail; tail != nullptr; tail = tail->tail){ if(ret->value < tail->value) ret = tail; } return ret; } void remove(stack* node){ if(node == nullptr) return; stack* head = node->head; stack* tail = node->tail; delete node; if(head) head->tail = tail; if(tail) tail->head = head; } void input(int &a) { while (true) { cin >> a; if (cin.good()) { break; } cout << "Wrong input" << endl; cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); } } int menu() { cout << "1 - add element" << endl; cout << "2 - show stack" << endl; cout << "3 - clear stack" << endl; cout << "4 - find max element" << endl; cout << "5 - remove max element" << endl; cout << "0 - exit" << endl; int choise; input(choise); return choise; } int main() { stack *stackHead = nullptr; stack *stackTail = nullptr; while (true) { switch (menu()) { case 1:{ cout << "Enter the element:" << endl; int value = 0; input(value); stackTail = addElements(stackTail, value); if(stackHead == nullptr) stackHead = stackTail; cout << "Element " << value << " added" << endl; break; } case 2:{ for(stack* head = stackHead; head != nullptr; head = head->tail){ cout << head->value << " -> "; } cout << endl; break; } case 4:{ stack* node = findMax(stackHead); if(node) cout << node->value << endl; break; } case 5:{ stack* node = findMax(stackHead); if(node){ int value = node->value; if(node->head == nullptr) stackHead = node->tail; if(node->tail == nullptr){ stackTail = node -> head; } remove(node); std::cout << "node " << value << " removed" << endl; } break; } case 0:{ cout << "Press y if you want to exit" << endl; char ch; cin.ignore(numeric_limits<streamsize>::max(), '\n'); if (cin.get(ch) && ch == 'y') { // TODO удалить стек в цикле return 0; } break; } default: cout << "Choose 1-4 or 0" << endl; break; } } } 
    • better to store in map - AR Hovsepyan
    • @ARHovsepyan, then it is better to use std::priority_queue , built on make_heap , in extreme cases, boost::flat_set , built on a sorted array. For large objects, pointers to them can be stored in the container. Ordinary trees (especially std :: map) will not give advantages here. - Ariox
    • but the map will allow not to write the switch construction, just the code will be needed to be remade and made more elegant - AR Hovsepyan