c++ - *&在C语言中是什么意思?

标签 c++ c serialization tree binary-tree

我对树木的概念很陌生。我正在研究 SerializationdeSerialization。我从链接中获得了一个示例程序,复制并执行了它。它运行了,但是当我试图理解它时,我无法理解一行 - void deSerialize(Node *&root, FILE *fp)

为什么*&是什么意思?

整个代码是:

#include <stdio.h>
#define MARKER -1

/* A binary tree Node has key, pointer to left and right children */
struct Node
{
int key;
struct Node* left, *right;
};

/* Helper function that allocates a new Node with the
given key and NULL left and right pointers. */
Node* newNode(int key)
{
Node* temp = new Node;
temp->key = key;
temp->left = temp->right = NULL;
return (temp);
}

// This function stores a tree in a file pointed by fp
void serialize(Node *root, FILE *fp)
{
// If current node is NULL, store marker
if (root == NULL)
{
    fprintf(fp, "%d ", MARKER);
    return;
}

// Else, store current node and recur for its children
fprintf(fp, "%d ", root->key);
serialize(root->left, fp);
serialize(root->right, fp);
}

// This function constructs a tree from a file pointed by 'fp'
void deSerialize(Node *&root, FILE *fp)
{
// Read next item from file. If theere are no more items or next
// item is marker, then return
int val;
if ( !fscanf(fp, "%d ", &val) || val == MARKER)
   return;

// Else create node with this item and recur for children
root = newNode(val);
deSerialize(root->left, fp);
deSerialize(root->right, fp);
}

// A simple inorder traversal used for testing the constructed tree
void inorder(Node *root)
{
if (root)
{
    inorder(root->left);
    printf("%d ", root->key);
    inorder(root->right);
}
}

/* Driver program to test above functions*/
int main()
{
// Let us construct a tree shown in the above figure
struct Node *root        = newNode(20);
root->left               = newNode(8);
root->right              = newNode(22);
root->left->left         = newNode(4);
root->left->right        = newNode(12);
root->left->right->left  = newNode(10);
root->left->right->right = newNode(14);

// Let us open a file and serialize the tree into the file
FILE *fp = fopen("tree.txt", "w");
if (fp == NULL)
{
    puts("Could not open file");
    return 0;
}
serialize(root, fp);
fclose(fp);

// Let us deserialize the storeed tree into root1
Node *root1 = NULL;
fp = fopen("tree.txt", "r");
deSerialize(root1, fp);

printf("Inorder Traversal of the tree constructed from file:\n");
inorder(root1);

return 0;
}

感谢任何帮助。

最佳答案

*& 不是一个单一的符号。但是两者的结合:

* 为指针。 & 供引用。

所以你有函数:

void deSerialize(Node *&root, FILE *fp)

这个函数的第一个参数是对节点指针的引用。

含义 - 使用它时,您向它发送一个 Node * 对象。并且函数本身可以更改此指针值 - 因为您通过引用传递了它。

这允许您在函数内部分配内存。

编写此函数的另一种方法是:

Node *deSerialize(Node *root, FILE *fp)

当然还有不同的用法:

root->left = deSerialize(root->left, fp)

在这里查看完整的解决方案:http://ideone.com/5GzAyd .和相关部分:

Node *deSerialize(Node *root, FILE *fp)
{
    // Read next item from file. If theere are no more items or next
    // item is marker, then return
    int val;
    if ( !fscanf(fp, "%d ", &val) || val == MARKER)
       return NULL;

    // Else create node with this item and recur for children
    root = newNode(val);
    root->left = deSerialize(root->left, fp);
    root->right = deSerialize(root->right, fp);
    return root;
}

关于c++ - *&在C语言中是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34505788/

相关文章:

c# - 如何从 C/C++ 调用 .NET 程序集?

c++ - 如何实现大尺寸非指针成员的 move 构造函数?

c++ - libxml 2 的 xml 复制 NodeList 中的内存泄漏

c - c中的小写字符串

.net - 是否应该在客户端和服务器端都将DTO映射到域实体或从域实体映射DTO?

c# - c# 中 [Serializable] 和 [Serializable()] 之间有区别吗?

c++ - 编译时链接错误故障排除

c++ - 一个 vector/映射中的 std::function 变量参数

c - 如何将程序输出重定向到文本文件

c# - 未序列化的结构列表列表。 System.RuntimeType 由于其保护级别而无法访问。只能处理公共(public)类型