我需要创建一个类成员(二进制)的链表,并且我遇到了无限循环问题。二进制类只包含度(int)和下一个节点指针。 在二进制类中,二进制链接列表的实现在set_bit方法内执行。 set_bit方法需要两个整数,它们是1/0 int(位)和int度。这一点虽然不是必需的。使用while循环创建链接列表
的SET_BIT方法是如下:
void Binary::set_bit(int b, int d){
BinaryNode* current = firstTerm;
BinaryNode* toSet;
if (current == NULL) {
firstTerm = new BinaryNode(d, NULL);
current = firstTerm;
cout << "\nd: " << d << " <--> current degree: " << current->degree << endl;
system("pause");
} else {
while (current != NULL){
firstTerm = new BinaryNode(d, current);
cout << "\nd: " << d << " <--> current degree: " << current->degree << endl;
cout << "first term: " << firstTerm->degree << endl;
system("pause");
}
}
}
上的main.cpp文件
,我试图设置以下位:
b1.set_bit(1, 2);
b1.set_bit(1, 5);
b1.set_bit(1, 0);
b1.set_bit(0, 2);
该方法是设定第一比特(2) ,并进入下一个(5),然后开始尝试设置该位的无限循环。 我在哪里错了? 我已经问了我的实验室老师的帮助,他向我提供了以下代码: 为什么实验室教师不工作的代码?
void Binary ::set_bit(int b , int d){
BinaryNode * current = firstTerm;
if (current == NULL){
firstTerm = new BinaryNode(d,NULL); // Corrected Line
}
while (current != NULL){
firstTerm = new BinaryNode(d,firstTerm); // Corrected Line
}
}
感谢
您不会在while循环中更改当前条件(current!= NULL)将始终为真。 – drescherjm 2014-09-29 19:55:10