C++ C++;:而loop赢了';t终止为空,我是否遗漏了什么?
似乎我不明白为什么while循环不会终止 它本质上应该是while(null) 我想不出来 如蒙惠顾,不胜感激 我不知道什么可能会停止while循环?它说第一个条目被存储,下一个条目是0x0000000000,而???姓名和???所以它实际上是空的。我尝试在构造函数中添加next=0;而不是next=NULL,它仍然不起作用 谢谢各位 安东尼C++ C++;:而loop赢了';t终止为空,我是否遗漏了什么?,c++,loops,while-loop,linked-list,terminate,C++,Loops,While Loop,Linked List,Terminate,似乎我不明白为什么while循环不会终止 它本质上应该是while(null) 我想不出来 如蒙惠顾,不胜感激 我不知道什么可能会停止while循环?它说第一个条目被存储,下一个条目是0x0000000000,而???姓名和???所以它实际上是空的。我尝试在构造函数中添加next=0;而不是next=NULL,它仍然不起作用 谢谢各位 安东尼 EDIT: Value of nodePtr = 00000000 if next = 0 in the constructor Valu
EDIT: Value of nodePtr = 00000000 if next = 0 in the constructor
Value of nodePtr = 00899FE0 if next = NULL in the constructor
if adding a cout << nodePtr->next; before the while.
EDIT:如果构造函数中的next=0,则nodePtr=00000000的值
如果构造函数中的next=NULL,则nodePtr=00899FE0的值
如果下一步添加cout;在这之前。
--完整的程序供参考
编辑2:
是我进入第二个条目时的弹出窗口
void LinkedList::appendNode(string name, double x, double y)
{
ListNode* newNode; // To point to new node
ListNode* nodePtr; // To traverse List
// allocate new node
newNode = new ListNode(name, x, y);
// If no head, head is the newNode
// else traverse the list to find the end and append newNode
if (!head)
{
head = newNode;
cout << "Record inserted successfully.\n" << endl;
}
else
{
nodePtr = head;
//traverse the list loop
while (nodePtr->next) //VS 2012 locks up here <-----
{
//Checks for duplicate entry by name
if (nodePtr->cityName == name)
{
cout << "No need to insert again, as this record exists in the existing data set.\n" << endl;
return;
}
//traverse the list
nodePtr = nodePtr->next;
}
// checks 2nd entry, as while loop wont run for a 2nd entry.
if (nodePtr->cityName == name) {
{
cout << "No need to insert again, as this record exists in the existing data set.\n" << endl;
return;
}
}
// if next is NULL add newNode
else if (!nodePtr->next)
{
nodePtr->next = newNode;
cout << "Record inserted successfully.\n" << endl;
}
}
void LinkedList::appendNode(字符串名,双x,双y)
{
ListNode*newNode;//指向新节点
ListNode*nodePtr;//遍历列表
//分配新节点
newNode=新的ListNode(名称,x,y);
//如果没有head,则head是新节点
//否则遍历列表以查找结束并追加新节点
如果(!头)
{
头=新节点;
我试图重现您的问题,但未能重现。我对您的ListNode和LinkedList类进行了反向工程。下面的代码可以工作,可能会帮助您解决问题。我对其进行了一些重构以简化它(进一步,您应该考虑跟踪列表的末尾,因为这将有助于您列表所需的其他操作)。请确保为Link KedListC类实现一个析构函数来清理节点。
#include <string>
#include <iostream>
using namespace std;
struct ListNode
{
string cityName;
double m_x, m_y;
ListNode* next;
ListNode(string name, double x, double y) : cityName(name), m_x(x), m_y(y), next(nullptr)
{}
};
class LinkedList
{
ListNode* head;
public:
LinkedList() : head(nullptr)
{
}
~LinkedList()
{
}
void LinkedList::appendNode(string name, double x, double y)
{
ListNode* newNode; // To point to new node
// allocate new node
newNode = new ListNode(name, x, y);
// If no head, head is the newNode
// else traverse the list to find the end and append newNode
if (!head)
{
head = newNode;
cout << "Record inserted successfully.\n" << endl;
}
else
{
ListNode *nodePtr = head;
ListNode *prevNode = nullptr;
//traverse the list loop
while (nodePtr)
{
//Checks for duplicate entry by name
if (nodePtr->cityName == name)
{
cout << "No need to insert again, as this record exists in the existing data set.\n" << endl;
return;
}
//traverse the list
prevNode = nodePtr;
nodePtr = nodePtr->next;
}
// if next is NULL add newNode
if (prevNode)
{
prevNode->next = newNode;
cout << "Record inserted successfully.\n" << endl;
}
}
}
};
int main()
{
LinkedList list;
list.appendNode("New York", 1.0, 2.0);
list.appendNode("Boston", 1.5, 2.5);
list.appendNode("Miami", 1.7, 2.7);
list.appendNode("Miami", 1.7, 2.7);
}
#包括
#包括
使用名称空间std;
结构列表节点
{
字符串cityName;
双m_x,m_y;
ListNode*下一步;
ListNode(字符串名,双x,双y):cityName(名称),m_x(x),m_y(y),next(nullptr)
{}
};
类链接列表
{
ListNode*头;
公众:
LinkedList():头(nullptr)
{
}
~LinkedList()
{
}
void LinkedList::appendNode(字符串名,双x,双y)
{
ListNode*newNode;//指向新节点
//分配新节点
newNode=新的ListNode(名称,x,y);
//如果没有head,则head是新节点
//否则遍历列表以查找结束并追加新节点
如果(!头)
{
头=新节点;
cout除了在尝试插入已有名称时出现明显的内存泄漏外,您的代码似乎工作正常。当使用0
或NULL
初始化指针时,它工作正常。这没有什么区别
(我可以猜测,在实际调用代码(您没有显示)中,您设法通过值传递了LinkedList
。由于您的LinkedList
不满足三个规则,列表的完整性遭到了破坏,这导致了您观察到的未定义的后果。)
顺便说一句,通过使用额外级别的间接寻址,您可以将分支严重的appendNode
函数简化为一个更紧凑、几乎没有分支的函数
void LinkedList::appendNode(string name, double x, double y)
{
ListNode** pnodePtr;
for (pnodePtr = &head; *pnodePtr != NULL; pnodePtr = &(*pnodePtr)->next)
if ((*pnodePtr)->cityName == name)
break;
if (*pnodePtr == NULL)
{
*pnodePtr = new ListNode(name, x, y);
cout << "Record inserted successfully.\n" << endl;
}
else
cout << "No need to insert again, as this record exists in the existing data set.\n" << endl;
}
void LinkedList::appendNode(字符串名,双x,双y)
{
ListNode**pnodePtr;
对于(pnodePtr=&head;*pnodePtr!=NULL;pnodePtr=&(*pnodePtr)->next)
if((*pnodePtr)->cityName==name)
打破
如果(*pnodePtr==NULL)
{
*pnodePtr=newlistnode(名称,x,y);
你不能在ListNode的构造函数中初始化NULL旁边吗?发布构造函数。实际上,发布两个构造函数:一个是LinkedList
,另一个是ListNode
。提示:当名称
已经在列表中时,你正在泄漏内存,因为你分配的newNode
永远不会被删除!@discobat他没有这样做It’他不想在列表中出现重复的城市名称,所以每次追加时他都需要遍历列表。我可以在明天早上显示用于启动呼叫的另一个文件。感谢您的帮助。它实际上会遍历第一组数据,尽管这很奇怪。可能它只是VS2012的编译器。@user2565679:我运行了您的代码并输入了nu列表中没有任何问题的条目数。您的代码在每次插入时都会打印列表head
值(第一次插入除外),但在其他情况下它会按预期工作。不,编译器不太可能出现问题。为什么vs12会给我一个问题(@user2565679:嗯,我在Visual Studio 2012和Visual Studio 2013中运行了你的代码,没有任何问题。如果你从这段代码中遇到问题,你需要一步一步地调试它,并找出它们第一次出现的时间和地点。现在我唯一能猜到的是,还有一些东西你没有向我们展示。那就是al此时实现的l,除了int main(){Output();return 0;}之外,还发布了它停止的位置的图片,它在while处停止(nodePtr->next)行,谢谢你的帮助,朋友。也许我的VS2012编译器不喜欢它。我通常将Netbeans与cygwin一起使用,但后来重新格式化了我的电脑。下面是调用它的代码,不过我假设它是一个编译器。