分配指针

时间:2016-03-09 02:49:25

标签: c++ pointers

我一直在研究这个问题,似乎无法理解发生了什么。我试图获取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;
}

1 个答案:

答案 0 :(得分:0)

在循环的第一次迭代中(使用您给出的字符串作为示例),您将松开对头对象的引用,因此后续迭代将节点添加到“无头列表”。

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。它包含了上述插入的示例和代码。