分配指针

Assigning pointer

本文关键字:指针 分配      更新时间:2023-10-16

我已经为此工作了一段时间,似乎无法理解发生了什么。我正在尝试获取 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;
};

单词节点.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;
}

主.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
}

要修复您的代码,您只需将所有节点添加到列表中,然后使用排序算法对其进行排序,或者像现在打算的那样动态插入它们。但是,要执行后者,您必须修改插入逻辑,即在开头插入节点(如果新节点按字母顺序低于第一个元素)或末尾插入节点。我建议阅读这篇关于单链表的好文章。它包含上述插入的示例和代码。