Wednesday 4 December 2019

Sorting Doubly Linked List C++


Possible Duplicate:
Sorting a Doubly Linked List C++ Question







as of right now the sort function that I have only sorts 1 node out of the many.



I need this function disregard the node that is sorted only after it is printed.



I've tried removing the node so that it is not considered twice for the sort, because It will keep printing that same sorted node over and over again. That didnt work so here i am.



This is defined and its where I call my sorting function. It has one node *Paramater and returns a node.



void list::displayByName(ostream& out) const
{

node *current_node = headByName; // is @the head of the list
node *evil_node = tail;

while ( current_node != NULL )
{
current_node = sort( current_node );
out << current_node->item.getName() << endl;
}
}



Defined as a private function of my list class.



list::node * const list::sort( node *given_node ) const
{
node *least_found_node = NULL;
node *current_node = given_node->nextByName;

while ( current_node && current_node != given_node ) // while current_node != NULL and..
{

if ( strcmp( current_node->item.getName(), given_node->item.getName() ) < 0 )
{
if ( least_found_node == NULL ||
( strcmp( least_found_node->item.getName(), current_node->item.getName() ) > 0 ) )
{
least_found_node = current_node;
}
}
current_node = current_node->nextByName;


}

return least_found_node; // return that sorted node
}

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...