简体   繁体   English

为什么列表上的迭代失败?

[英]Why does iteration on my list fail?

I'm currently working my way through the Stanford open CS106B, and I'm running into a problem on Assignment 3, Part B. I am given a struct Node as follows: 我目前正在通过斯坦福开放的CS106B工作,我在Assignment 3,Part B上遇到了问题。我给了一个结构节点,如下所示:

struct Node {
string name;   // my person's name
string killer; // who eliminated me
Node* next;    // ptr to next node
Node(string name, Node* next) {...}
};

I have to implement a class that makes a list of Nodes. 我必须实现一个创建节点列表的类。 I have the constructor working properly, but when I try to iterate through the list, my program crashes. 我让构造函数正常工作,但是当我尝试遍历列表时,我的程序崩溃了。 My iteration code: 我的迭代代码:

void AssassinsList::printGameRing() {
    Node* current;
    for(current = ring; current->next != NULL; current = current->next) {
        cout << endl << "  " << current->name << " is targeting " << current->next->name;
    }
    cout << endl << "  " << current->name << " is targeting " << ring->name << endl;
}

However, if I use a for-loop to loop the number of times I know I need to for a certain list length, it works. 但是,如果我使用for循环来循环我知道我需要的特定列表长度的次数,它可以工作。 Help? 救命? The link to the assignment pdf: http://www.stanford.edu/class/cs106b/homework/3-tiles-assassins/spec.pdf 作业链接pdf: http//www.stanford.edu/class/cs106b/homework/3-tiles-assassins/spec.pdf

Thanks! 谢谢!

I am guessing that you don't initialize * next to nullptr . 我猜你没有在nullptr * next初始化* next So for all the links you setup between nodes it is fine, but the last object in the list points to garbage. 因此,对于您在节点之间设置的所有链接,它很好,但列表中的最后一个对象指向垃圾。

Sorry, nullptr is c++11. 对不起, nullptr是c ++ 11。 If your compiler older then its just NULL . 如果您的编译器较旧,那么它只是NULL

There's a chance that if cur is NULL or doesn't point to anything, you could be dereferencing a bad pointer and therefore crashing your program. 如果cur为NULL或者没有指向任何内容,则可能会取消引用错误的指针,从而导致程序崩溃。 The other option is that as woolstar pointed out, you don't have a terminating node in your list (that points to NULL.) Observe the following code: 另一个选项是,如woolstar所指出的,您的列表中没有终止节点(指向NULL。)请注意以下代码:

Node* head = new Node{0};
Node* cur = head;
for (int i = 1; i <= 10; i++)
{
    cur->next = new Node{i};
    cur = cur->next;
}

// Set terminating node
cur->next = nullptr;

// We'll iterate until cur is null
// So if we access cur->next
// It won't result in undefined behavior
for (cur = head; cur != nullptr; cur = cur->next)
{
    std::cout << cur->value;
}

// cur should be nullptr now
if (!cur)
    std::cout << "end of list";

You can also use 0. Yeah, it's not as cool as nullptr , but it's supported. 你也可以使用0.是的,它不像nullptr那么酷,但它是支持的。 Fixed constructor: 固定构造函数:

Node(string name_, Node* next_=0): name(name_), next(next_) {}

The fact a fixed length loop works, but a NULL terminated loop doesnt work shows that its likely you have an invalid address in the last Node's next field. 固定长度循环工作的事实,但NULL终止循环不起作用表明它可能在最后一个节点的下一个字段中有一个无效地址。

I would expect your problem comes from either your Node constructor or your list code or the interplay between them. 我希望您的问题来自您的Node构造函数或列表代码或它们之间的相互作用。

Try setting next to 0 / nullptr in the Node constructor, that should help. 尝试在Node构造函数中设置0 / nullptr旁边,这应该有所帮助。

Alternatively, have your list set the next field to 0 when you add the first element to the list, or add any element to the end of the list. 或者,当您将第一个元素添加到列表中时,让列表将下一个字段设置为0,或者将任何元素添加到列表的末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM