How can I save the address of the structure that is in front of the minimum element?
The list is simply connected!
Here is the function:

Single_List Find_Min_Elem(Single_List* Head) { //элементы списка(к примеру): 7 5 9 3 1 5 Single_List *temp = Head; Single_List *temp2 = temp->Next; Single_List *preMin= temp;//адрес предыдущего элемента миним //структуры(нужен для того, чтобы не потерять) for (int i = 0;i < 5; i++) { if ((temp->Data) < (temp2->Data)) { temp2 = (temp2->Next); }//end if else { temp = temp2; temp2 = (temp2->Next); }//else }//for return *preMin; } 

Where should I use premin?

    1 answer 1

     Single_List* Find_Min_Elem(Single_List* Head) { Single_List* preMin(nullptr); for(Single_List* cur(Head); cur && cur->Next; cur = cur->Next) if((preMin && preMin->Next->Data > cur->Next->Data) || (!preMin && Head->Data > cur->Next->Data)) preMin = cur; return preMin; } 

    In my code, if the minimum item in the list is the first item, then preMin will be nullptr, which means that the minimum item in the list does not have a previous item. In your code, you initialize preMin with a pointer to the head of the list, which suggests that in the above case it should never be nullptr ... In general, without understanding what the result of the function should be, in the case of the minimum first element, I left the preMin for this case equal to nullptr.

    • one
      Use nullptr instead of NULL , it's time to switch to “normal C ++” - ixSci
    • Thanks for the comment, corrected the message. - kodv
    • one
      std:: before nullptr not needed - ixSci