正在访问指针地址处的节点

Accessing Node at a pointer address

本文关键字:节点 地址 指针 访问      更新时间:2023-10-16

我通过在编码类中为节点分配了一个地址

newnode.zero = &zeronode;

newnode和zeronode是节点结构的实例,该结构具有成员指针node*zero。如何访问同一类中不同函数中的节点?目前,我似乎只能通过获得指针

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;
}

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

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

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

我似乎只能通过获得指针

Node newnode = *root.zero;

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

Node * newnode = root.zero;

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