You need to write a function to find the minimum element in the list. I set the list in the following way:

template <typename T> class list { struct node { T data; node *next; node *prev; }; node *head; node *tail; ... } 

I wrote a function that seems to be compiled without errors, but does not display any result. I can not figure out where the error is.

  template <typename T> int list<T> :: seek_min() { node *temp = head; int min = temp -> data; for (temp; temp != NULL; temp = temp->next) { if (temp->data < min) { min = temp -> data; temp = temp -> next; } } return min; } 
  • And where do you expect to issue results? - Vladimir Martyanov
  • @ Vladimir Martyanov, to the console - Alex_Lastochkin
  • And you did not think that you need to insert the output operator? - Vladimir Martyanov
  • Yes, I already noticed this misstep. - Alex_Lastochkin

1 answer 1

And what will happen when a new minimum is reached? You will go to the if block, where you will safely go to the new node ... And then - again to the new node in the third part of the loop header ...

You obviously have an extra temp = temp->next in if .

And, by the way, just temp in the first part of the for header is also not needed. And - you do not check if the list is not empty, by the way.

  • Removed the temp = temp-> next line from the loop and it all worked. Thank! - Alex_Lastochkin