我已经为此工作了一段时间,似乎无法理解正在发生的事情。我试图将值取到istr中,将它们放在链表中,然后按字母顺序对它们进行排序。最终,我将其打印出来。我不确定我的问题在哪里,但我以为它在函数InsertAfter中。这不是我的问题吗?如果是,您是否知道是什么原因导致我的链接列表无法链接?代码的最后一部分仅输出headObj,而不是全部输出,因此我假设我的列表未在每个对象的nextNodePtr中正确链接,但是我不确定。谢谢您的帮助!
void WordNode::InsertAfter(WordNode* nodeLoc) {
WordNode* tmpNext = 0;
tmpNext = this->nextNodePtr; // Remember next
this->nextNodePtr = nodeLoc; // this -- node -- ?
nodeLoc->nextNodePtr = tmpNext; // this -- node -- next
return;
}
wordNode.hpp
#ifndef wordNode_hpp
#define wordNode_hpp
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
class WordNode {
public:
WordNode(string wordval = "", int count = 0, WordNode* nextLoc = 0);
void InsertAfter(WordNode* nodePtr);
WordNode* GetNext();
void PrWordNodeData();
string GetWord();
private:
string word;
WordNode* nextNodePtr;
int wordCount;
};
wordNode.cpp
#include "wordNode.hpp"
// Constructor
WordNode::WordNode(string wordval,int count, WordNode* nextLoc) {
this->word = wordval;
this->wordCount = count;
this->nextNodePtr = nextLoc;
return;
}
/* Insert node after this node.
* Before: this -- next
* After: this -- node -- next
*/
void WordNode::InsertAfter(WordNode* nodeLoc) {
WordNode* tmpNext = 0;
tmpNext = this->nextNodePtr; // Remember next
this->nextNodePtr = nodeLoc; // this -- node -- ?
nodeLoc->nextNodePtr = tmpNext; // this -- node -- next
return;
}
// Print dataVal
void WordNode::PrWordNodeData() {
cout << this->word <<": count=" <<this->wordCount << endl;
return;
}
// Grab location pointed by nextNodePtr
WordNode* WordNode::GetNext() {
return this->nextNodePtr;
}
//Returns word
string WordNode::GetWord()
{
return word;
}
main.cpp
#include <iostream>
#include <sstream>
#include <string>
#include "wordNode.hpp"
int main() {
WordNode* headObj = 0; // Create WordNode objects
WordNode* currObj = 0;
WordNode* nextObj = 0;
string istr ="555 999 777 333 111";
istringstream instring(istr);
string temp;
//Assigns first word to the head object
if (!instring.eof()){
instring >> temp;
headObj=new WordNode(temp,1);
}
currObj=headObj;
while (!instring.eof()){
instring >> temp;
nextObj=new WordNode(temp,1);
//swaps values if currObj is greater than the next word
if(currObj > nextObj) {
currObj->InsertAfter(nextObj);
}
currObj=nextObj;
}
// Print linked list
currObj = headObj;
while (currObj != 0) {
currObj->PrWordNodeData();
currObj = currObj->GetNext();
}
string i;
cin >> i;
return 0;
}
最佳答案
在循环的第一个迭代中(使用您作为示例给出的字符串),您松开了对head对象的引用,因此后续的迭代会将节点添加到“无头列表”中。
currObj=headObj;
while (!instring.eof()){
instring >> temp;
nextObj = new WordNode(temp,1);
//swaps values if currObj is greater than the next word
if(currObj->GetWord() > nextObj->GetWord()) {
currObj->InsertAfter(nextObj);
}
// And what happens if it is not greater?
currObj = nextObj; // Loose reference to head here if not greater
}
要修复代码,您只需要将所有节点添加到列表中,然后用sorting algorithm对其进行排序,或者立即将它们插入即可。但是,要执行后者,您将必须修改您的插入逻辑,即在开头(如果新节点按字母顺序低于第一个元素)或结尾处插入节点。我建议阅读this nice article about singly linked lists。它具有提到的示例和代码。
关于c++ - 分配指针,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35881985/