问题描述
尝试弄清楚为什么我的 list()
类指针正被第三个节点覆盖。在插入函数(下面)中发生的是,第三次插入函数被调用时,我的 headByName-> nextByName
节点指针被第三个节点覆盖指向第二个。所以你可以猜测第4个节点被第5个节点覆盖,第6个节点被第7个节点覆盖,因此节点中的跳跃。
Trying to figure out why my list()
class pointers are being overwritten with the third node. What happens in the insert function (below) is that the third time the insert function is called my headByName->nextByName
node pointer is overwritten by the third node, when it should point to the second. So as you can guess the 4th node is overwritten by the 5th and the 6th node is overwritten by the 7th, hence the "jump" in nodes.
winery对象通过winery ctor传递,然后通过主程序中的这些调用传递到 list :: insert
函数:
The attributes of the winery object are passed through the winery ctor and then to the list::insert
function via these calls in main:
//main.cpp
//the attributes of the winery object are the paramaters, name, location, acres, rating:
list *listPtr = new list();
wineries->insert(winery("Lopez Island Vinyard", "San Juan Islands", 7, 95));
wineries->insert(winery("Gallo", "Napa Valley", 200, 25));
wineries->insert(winery("Cooper Mountain", "Willamette Valley", 100, 47));
然后调用winery ctor,其中我分配私有指针memembers:
Then the winery ctor is called where I allocate the private pointer memembers:
//winery.cpp
winery::winery(const char * const name, const char * const location, const int acres, const int rating)
: m_acres( acres ), m_rating( rating )
{
if (name)
{
size_t len = strlen( name ) +1;
m_name = new char[len];
strcpy_s( m_name, len, name );
}
else
m_name = NULL;
if (location)
{
size_t len = strlen( location ) +1;
m_location = new char[len];
strcpy_s( m_location, len, location );
}
else
m_location = NULL;
}
然后到手头的问题:如果你能想象这个函数已经被调用两次,然后current_node将有thrid酒厂。 headByName
将有第一个节点,在它[+], headByName->下一个
将有第三个。我需要 headByName->下
有第二个。我只是想知道为什么它已被覆盖..
And then to the issue at hand: If you can imagine this function has already been called twice then current_node will have the thrid winery. headByName
will have the first node and in it [+], headByName->next
will have the third. I need headByName->next
to have the second. I am just wondering why it has been overwritten..
// list.cpp
void list::insert(const winery& winery)
{
node *current_node = new node( winery ); // the third wineries info!
node *next_node = NULL;
list *list_ptr = NULL;
do
{
if ( headByName == NULL || headByRating == NULL ) // then we are here for the first item
{
headByName = current_node; // the list ptrs have a node address.
headByRating = current_node;
}
else
{
next_node = current_node;
// transfer by the next call in main.
headByName->nextByName = next_node;
headByRating->nextByRating = next_node;
}
} while ( headByName == NULL || headByRating == NULL );
next_node = NULL;
current_node = NULL;
}
有人可以找到我的第二个节点吗?
哦,以及可用的指针:
Could someone please find my second node?oh yeah, and the pointers that are available to me:
struct node
{
winery item;
node * nextByName;
node * nextByRating;
};
class list
{
...
private:
node * headByName;
node * headByRating;
};
这可能有些重要,它的节点ctor的主体:
this might be of some importance, its the body of the node ctor:
//list.cpp
list::node::node(const winery &winery) :
nextByName( NULL ), nextByRating( NULL ), item( winery.getName(),
winery.getLocation(), winery.getAcres(), winery.getRating() )
{
// where the item paramters are all pub-mem functions of the winery class.
}
推荐答案
这个问题在这里:
headByName->nextByName = next_node;
headByRating->nextByRating = next_node;
您总是覆盖第二个节点。根据我的理解,你应该通过遍历整个列表并插入最后一个节点后找到最后一个。
You're always override second node. As I understand you should find last one by iterating through whole list and insert after last node.
这篇关于LinkedList“节点跳转”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!