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.