【发布时间】:2009-08-24 04:17:58
【问题描述】:
我只需要一个按名称排序的链表。我只能到第 1、第 3、第 5、.. 节点。我只是想不到这么远。我想成为一名 C++ 程序员,但如果我不明白这是他们的希望吗? STL 容器std::lists 目前对我来说不是一个选择。你在 list 函数中看到的正是我想要理解的。
list::node::node(const winery &winery) : item( winery.getName(), winery.getLocation(),
winery.getAcres(), winery.getRating() ), nextByName( NULL ), nextByRating( NULL )
{
}
void list::insert(const winery& winery)
{
node *current_node = new node( winery ); // but came here and did this so it has new info!
node *next_node = NULL;
node *tail_node = current_node;
if ( headByName == NULL ) // then we are here for the first item
{
headByName = current_node; // the list ptrs will have the first node's address.
headByRating = current_node;
}
while ( headByName->nextByName != NULL )
{
headByName->nextByName = tail_node;
tail_node = next_node;
//next_node = current_node;
}
tail_node = new node( winery );
headByName->nextByName = tail_node;
}
以及可供我使用的指针:
struct node
{
winery item;
node * nextByName;
node * nextByRating;
};
class list
{
...
private:
node * headByName;
node * headByRating;
};
【问题讨论】:
-
在我看来像是单链表,而不是双链表...
-
它不是传统意义上的双向链表,而是是一个表示同一组节点的两种不同排序的列表。通常的排序是按插入顺序和反向插入顺序,但在这种情况下(根据变量名称),它们是按名称按评级。
-
灯罩,不要以这种方式编辑您的帖子。
-
@lampshade:如果您不希望看到您的问题,可以将其删除。
标签: c++ linked-list