排序双链表C
作者:互联网
尝试通过遍历列表的循环来实现.
在循环中,我将头节点输入到已定义的排序函数中,然后使用strcmp来确定节点中的哪个名称应优先出现.
它不起作用,因为过早写入名称.
我通过一次向下列出一个节点,而不回头看看第一个节点是否应该在最后一个节点之前,对它们进行线性比较.解释的那部分会有所帮助.
现在,对我来说最重要的两个功能定义如下:
我已尽力去做我认为适合排序功能的事情.
void list::displayByName(ostream& out) const
{
list *ListPtr = NULL;
node *current_node = headByName;
winery *wine_t = new winery();
// winery is another class object type
// im allocating it to prevent a crash when I call it.
while ( current_node != NULL )
{
*(wine_t) = current_node->item;
wine_t = ListPtr->sort( current_node );
out << wine_t << endl;
current_node = current_node->nextByName;
}
delete wine_t;
}
winery * const list::sort( node * current_node ) const
{
// current_node is the first node.
const char *SecondName = NULL, *FirstName = NULL;
winery *wine_t = new winery();
if ( current_node != NULL )
{
SecondName = current_node->item.getName();
current_node = current_node->nextByName;
FirstName = current_node->item.getName();
}
if ( strcmp( FirstName, SecondName ) == -1 )
{
*(wine_t) = current_node->item;
FirstName = NULL;
SecondName = NULL;
return wine_t;
}
else if ( strcmp( FirstName, SecondName ) == 1 )
{
*(wine_t) = current_node->item;
FirstName = NULL;
SecondName = NULL;
return wine_t;
}
else return wine_t;// then the strings are equal
FirstName = NULL;
SecondName = NULL;
return wine_t;
}
我开始在这里开发节点:
void list::insert(const winery& winery)
{
node *current_node = new node( winery );
if ( headByName == NULL )
{
headByName = current_node;
headByRating = current_node;
tail = headByName;
current_node->prev = current_node;
}
else
{
current_node->prev = tail;
tail->nextByName = current_node;
}
tail = current_node;
current_node = NULL;
}
我认为在上述功能中它是正确的.我可以在那里整理一下吗?
以下是我正在使用的变量:
public list
{
...
void insert(const winery& winery);
void displayByName(ostream& out) const;
}
private:
struct node
{
node(const winery& winery); // constructor
winery item;
node * prev;
node * nextByName;
node * nextByRating;
};
winery * const sort(node*) const;
node * headByName;
node * headByRating;
node * tail;
};
任何帮助表示赞赏.
非常感谢=)
解决方法:
据我了解,您希望list :: sort在列表中找到大于输入的最小节点.
为此,您需要遍历所有元素,并保持找到当前最小但更大的节点.
像这样:
node * const list::sort( node * given_node ) const
{
if ( given_node == NULL )
{
return NULL;
}
// Smallest node found which is greater than given_node.
node * least_found_node = NULL;
// Node we are looking at right now.
node * current_node = given_node->nextByName;
// Go through all nodes.
while ( current_node && current_node != given_node )
{
// Is this node bigger than the given node?
if ( strcmp( current_node->item.getName(), given_node->item.getName() ) < 0 )
{
// Is this node smaller than the smallest node we know of?
if ( least_found_node == NULL ||
((strcmp( current_node->item.getName(), least_found_node->item.getName() ) > 0) )
{
// We found a better node.
least_found_node = current_node;
}
}
current_node = current_node->nextByName;
}
return least_found_node;
}
现在,更改显示功能以使用它,如下所示:
void list::displayByName(ostream& out) const
{
// Find first node initially.
node * current_node = sort( NULL );
while ( current_node != NULL )
{
// Print node.
out << current_node->item.getName();
// Find next node in sorted output.
current_node = sort( current_node );
}
}
这部分将继续调用sort,直到sort返回NULL.排序的第一个调用是NULL,因此找到了最低的项目(即排序列表中的第一个).如果没有更多的节点大于current_node,则sort返回NULL,从而终止循环.
标签:c,sorting,linked-list 来源: https://codeday.me/bug/20191009/1881994.html