在指针地址访问节点

时间:2013-05-17 13:57:13

标签: c++

我已经通过

为编码类中的节点分配了一个地址
newnode.zero = &zeronode;

newnode和zeronode是节点struct的实例,其成员指针Node *为零。如何在同一个类的不同函数中访问该节点?目前我似乎只能通过

获得一个指针
Node newnode = *root.zero;

这是现在的整个编码类;

/**
 * File: encoding.cpp
 * ------------------
 * Place your Encoding class implementation here.
 */

#include "encoding.h"
#include "map.h"
#include "string.h"
#include <string>
#include "strlib.h"
#include "huffman-types.h"
#include "pqueue.h"

using namespace std;

Encoding::Encoding() {

}

Encoding::~Encoding() {
    frequencyTable.clear();
}

void Encoding::compress(ibstream& infile, obstream& outfile) {
    getFrequency(infile);
    compresskey = "";
    foreach(ext_char key in frequencyTable) {

        int freq = frequencyTable.get(key);
        Node newnode;
        newnode.character = key;
        newnode.weight = freq;
        newnode.zero = NULL;
        newnode.one = NULL;

        huffqueue.enqueue(newnode, freq);
        string keystring = integerToString(key);
        string valstring = integerToString(freq);
        compresskey = compresskey + "Key = " + keystring + " " + "Freq = " + valstring + " ";

    }
    buildTree();
    createReferenceTable();

}

void Encoding::decompress(ibstream& infile, obstream& outfile) {

}

void Encoding::getFrequency(ibstream& infile) {

    int ch;

    while((ch = infile.get()) != EOF){
        if(frequencyTable.containsKey(ch)){
            int count;
            count = frequencyTable.get(ch);
            frequencyTable[ch] = ++count;
        }

        else {
            frequencyTable.put(ch, 1);
        }
    }
    frequencyTable.put(PSEUDO_EOF, 1);
}

void Encoding::buildTree() {
    int numnodes = huffqueue.size();
    for (int i = 1; i < numnodes; i++) {
        Node newnode;
        newnode.character = NOT_A_CHAR;
        Node zeronode = huffqueue.extractMin();
        newnode.zero = &zeronode;
        Node onenode = huffqueue.extractMin();
        newnode.one = &onenode;
        int newfreq = zeronode.weight + onenode.weight;
        newnode.weight = newfreq;
        huffqueue.enqueue(newnode, newfreq);
    }
}

void Encoding::createReferenceTable() {
    Node root = huffqueue.extractMin();
    string path = "";

    tracePaths(root, path);

}

void Encoding::tracePaths(Node root, string path) {

    if (!root.character == NOT_A_CHAR) {
        ext_char ch = root.character;
        referenceTable.put(ch, path);
        return;
    }

    for (int i = 0; i < 2; i++) {
        if (i == 0) {
            if (root.zero != NULL) {
                Node newnode = root->zero;// This is where the problem is


                path = path + "0";
                tracePaths(newnode, path);
            }
        }
        else if (i == 1) {
            if (root.one != NULL) {
                Node newnode = root.one;
                path = path + "1";
                tracePaths(newnode, path);
            }
        }
    }
    return;
}

2 个答案:

答案 0 :(得分:2)

  

如何在同一个类的不同函数中访问该节点?

您是否在询问如何从成员函数中访问数据成员?只需使用它的名称zero即可。如果您喜欢显性,可以说this->zero

或者你是否在询问如何从一个对它一无所知的函数中获取newnode?你不能;你需要以某种方式将它提供给其他功能。如何做到这一点取决于你保留newnode的位置,以及你调用其他功能的位置;我们需要更多细节来提供建议。

  

我似乎只能通过

获得一个指针
Node newnode = *root.zero;

那不是指针,那是副本。如果你想要一个指针,那么:

Node * newnode = root.zero;

答案 1 :(得分:0)

当我们指向结构指针时,我们应该使用 - &gt;运算符来访问Structure内部的元素而不是。运算符,因为您使用自引用结构内部元素可以像使用成员的直接名称或this-&gt;节点一样访问其他普通元素