gpt4 book ai didi

c++ - 如何插入到已排序的链表中?

转载 作者:搜寻专家 更新时间:2023-10-31 00:23:48 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
LinkedList “node jump”

我只需要一个按名称排序的链表。我只能得到第 1、3、5、.. 节点。我只是想不到这么远。我想成为一名 C++ 程序员,但如果我无法理解这是他们的希望吗? STL 容器 std::lists 对我这个学生来说不是一个选择。您在列表函数中看到的是我试图理解的内容。

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;
};

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com