- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是 C 编程的新手,但我想做的是使用数组创建 BST。这是我正在使用的 header 的代码:
bst.h
#ifndef ASSIGNMENT4_BST_H
#define ASSIGNMENT4_BST_H
// ====== this is in bst.h
#include "data.h"
typedef struct {Node *tree_nodes; unsigned char *is_free; int size;} BStree_struct;
typedef BStree_struct* BStree;
BStree bstree_ini(int size);
void bstree_insert(BStree bst, Key *key, int data);
void bstree_traversal(BStree bst);
void bstree_free(BStree bst);
#endif //ASSIGNMENT4_BST_H
数据.h
#ifndef ASSIGNMENT4_DATA_H
#define ASSIGNMENT4_DATA_H
typedef struct {char *name; int id;} Key;
typedef struct {Key *key; int data;} Node;
Key *key_construct(char *in_name, int in_id);
int key_comp(Key key1, Key key2);
void print_key(Key *key);
void print_node(Node node);
#endif //ASSIGNMENT4_DATA_H
这里是我的实际源文件的代码:
bst.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bst.h"
// Input: ’size’: size of an array
// Output: a pointer of type BStree,
// i.e. a pointer to an allocated memory of BStree_struct type
// Effect: dynamically allocate memory of type BStree_struct
// allocate memory for a Node array of size+1 for member tree_nodes
// allocate memory for an unsigned char array of size+1 for member is_free
// set all entries of is_free to 1
// set member size to ’size’;
void bstree_insert_helper (BStree, Key *, int, int);
BStree bstree_ini(int size) {
BStree bstree = (BStree*) malloc(sizeof(BStree));
bstree->tree_nodes = (Node*) malloc(size+1 * sizeof(Node));
bstree->is_free = (unsigned char *) malloc (size+1 * sizeof(unsigned char));
memset (bstree->is_free, '1', size+1);
for (int i = 0 ; i < size; i++){
// printf("%c", bstree->is_free[i]);
}
return bstree;
}
// Input: ’bst’: a binary search tree
// ’key’: a pointer to Key
// ’data’: an integer
// Effect: ’data’ with ’key’ is inserted into ’bst’
// if ’key’ is already in ’bst’, do nothing
void bstree_insert(BStree bst, Key *key, int data) {
bstree_insert_helper(bst, key, data, 1);
}
void bstree_insert_helper (BStree bst, Key *key, int data, int index){
if (bst->is_free[index] == '1'){
bst->tree_nodes[index].key = key;
bst->tree_nodes[index].data = data;
bst->is_free[index] = '0';
} else {
int value = key_comp(*key, *bst->tree_nodes[index].key);
if (value < 0){
int newIndex = 2*index;
if (bst->is_free[newIndex] == '1'){
bst->tree_nodes[newIndex].key = key;
bst->tree_nodes[newIndex].data = data;
bst->is_free[newIndex] = '0';
} else {
bstree_insert_helper(bst, key, data, newIndex);
}
} else if (value > 0){
int newIndex = 2*index+1;
if (bst->is_free[newIndex] == '1'){
bst->tree_nodes[newIndex].key = key;
bst->tree_nodes[newIndex].data = data;
bst->is_free[newIndex] = '0';
} else {
bstree_insert_helper(bst, key, data, newIndex);
}
} else {
puts("Key already exists... Aborting");
}
}
}
int main (){
BStree bsTree = bstree_ini(10);
bstree_insert_helper(bsTree, key_construct("Ilya", 1), 11, 1);
bstree_insert_helper(bsTree, key_construct("Covin", 2), 12, 1);
bstree_insert_helper(bsTree, key_construct("adolf", 3), 13, 1);
bstree_insert_helper(bsTree, key_construct("henlo", 4), 14, 1);
print_node(bsTree->tree_nodes[1]);
return 0;
}
数据.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "data.h"
// Input: ’in_name’: a string ends with ’\0’
// ’in_id’: an integer
// Output: a pointer of type pointer to Key,
// pointing to an allocated memory containing a Key
// Effect: dynamically allocate memory to hold a Key
// set Key’s id to be in_id
// dynamically allocate memory for the Key’s name
// so that name will contain what is in ’in_name’.
// Note: may use strdup()
Key *key_construct(char *in_name, int in_id) {
Key *key = (Key*) malloc(sizeof(Key));
key->name = strdup(in_name);
key->id = in_id;
return key;
}
// Input: ’key1’ and ’key2’ are two Keys
// Output: if return value < 0, then key1 < key2,
// if return value = 0, then key1 = key2,
// if return value > 0, then key1 > key2,
// Note: use strcmp() to compare key1.name and key2.name
// if key1.name = key2.name, then compare key1.id with key2.id
int key_comp(Key key1, Key key2) {
int value = strcmp(key1.name, key2.name);
if (value < 0 || value > 0){
return value;
} else {
if (key1.id < key2.id){
return -1;
}else if (key1.id > key2.id){
return 1;
} else {
return 0;
}
}
}
// Input: ’key’: a pointer to Key
// Effect: ( key->name key->id ) is printed
void print_key(Key *key) {
char *name = key->name;
int id = key->id;
printf("%s, %i", name, id);
}
// Input: ’node’: a node
// Effect: node.key is printed and then the node.data is printed
void print_node(Node node) {
printf("%s, %d \n", node.key->name, node.data);
}
问题是,当我在 bst.c 的 main 中运行 print_node 函数时,我希望它能够正确打印数组中索引 1、2、3 和 7 的 key.name 和数据。但是,索引 1 和 3 的 key.name 未正确打印。相反,如果我使用 print_node(bsTree->tree_nodes[1])
,我会得到一堆看起来像这样的乱码 - 0+@]�, 11
。我感觉这与我错误地为 data.c 中的键分配内存有关,但我无法弄清楚我到底做错了什么。
最佳答案
我认为错误在这里:
(size+1 * sizeof(Node))
在 BStree bstree_ini(int size)
中:
bstree->tree_nodes = (Node*) malloc(size+1 * sizeof(Node));
bstree->is_free = (unsigned char *) malloc (size+1 * sizeof(unsigned char));
memset (bstree->is_free, '1', size+1);
*
首先执行,然后是 +
(就像在正常数学中一样),参见 operator precedence ,表达式:
size+1 * sizeof(Node)
等于:
size + (1 * sizeof(Node))
这不是你想要的!
使用正确的大括号:
malloc((size + 1) * sizeof(Node));
或者使用calloc
:
calloc(sizeof(Node), size + 1);
一些友好的通知:
bstree->is_free
如果是 bool
类型会更清楚。如果它是 bstree->is_used
会更清楚(对我来说),因为我可以预期它在新对象上被初始化为零,也很容易清除它 memset (bstree->is_used, 0, ...)
。您只需将 '1'
和 '0'
分配给它,我发现 bool 值对于这个缩进更清晰,bstree->us_used[obj_num] = true;
将来你可能会选择一个位容器。关于c - 字符串 C 中打印的奇怪符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53320247/
给定一个字符串,例如 s="##$$$#",我如何找到索引之前的“#”符号数等于“”数的索引$"符号在索引之后? 示例:如果 s="##$$$#",则输出将为 2。 解释:在索引 2 之前我们有 2
在本教程中,您将借助示例了解 JavaScript 符号。 JavaScript 符号 JavaScript ES6 引入了一种新的原始数据类型,称为 Symbol(符号)。符号是不可变的(不能更改)
在“函数编程的工艺”一书中,符号 '>.>' 将函数连接在一起,与 '.' 的方向相反。但是当我使用 ghci 实现它时,它显示了超出范围的错误 '>.>'。为什么?它是不再使用的旧符号吗? 最佳答案
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我需要从向量中删除 \"。这是我的数据: data <- c("\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1
我在 Nginx 配置中使用正则表达式来捕获文件 URL,但如果文件 URL 包含 # 符号,正则表达式模式将不会捕获它。 这里是nginx的配置部分。 location ~ ^/p/(?[\w\-=
如何使 & 符号在此图表的第一组条形/列下正确显示: http://jsfiddle.net/VxbrK/2/ 应该是“Apples & Oranges”而不是“Apples & Oranges”。
**在verilog中是什么意思? 我为测试台提供了以下逻辑 localparam NUM_INPUT_BITS = 1; localparam NUM_OUTPUT_BITS
我有一个使用正则表达式来验证电子邮件地址的方法。 public String searchFormail(String searchWord) { Pattern pattern = Patt
我想将一个字符串拆分为数字部分和文本/符号部分我当前的代码不包含负数或小数,并且表现得很奇怪,在输出的末尾添加了一个空列表元素 import re mystring = 'AD%5(6ag 0.33-
我有一些代码需要从数组中选择一个随机字符串,但它一直返回单个字母或数字。如何解决这个问题? var name = ["Yayek", "Vozarut", "Gezex",
我刚开始使用 Python,我在考虑应该使用哪种表示法。我读过 PEP 8关于 Python 符号的指南,我同意那里的大多数内容,除了函数名称(我更喜欢混合大小写风格)。 在 C++ 中,我使用匈牙利
在用 C# 编写代码时,我错误地在 if 语句中的变量前添加了一个符号(而不是感叹号)。 bool b = false; if (@b) { } 我很惊讶它编译成功,没有任何错误。 我想知道:上面的代
本文实例为大家分享了特殊字符替换电话号码中某一部分的方法,ios利用-号替换电话号码中间四位,供大家参考,具体内容如下 1、效果图 2、代码 rootviewcontroll
当我使用“x”和“z”作为符号时,这段代码没有问题: from sympy import * x, z = symbols('x z') y = -6*x**2 + 2*x*z**0.5 + 50*x
我需要从文本中删除标点符号: data <- "Type the command AT&W enter. in order to save the new protocol on modem;"
我有几个数字是 numeric 类。下面的例子。 df = c(12974,12412,124124,124124,34543,4576547,32235) 现在我想在每个数字前添加 '$' 符号而不
我有一个 highcharts 图例,其中符号以不同的大小显示,因为它们在实际图表中的大小不同。不幸的是,当数据点的大小增加时,它们也会在图例中增加。无论数据点大小如何,我都希望图例符号保持相同的大小
我需要使用包含平均值+-SD的标题。到目前为止,我只能得到以下信息: "Mean +- SD or N (%)" [1] "Mean +- SD or N (%)" 如何直接使用“+-”符号?您知道一
使用 XSLT 和 XPath 1.0,我有一个要转义的字符串以用于 URL,例如: one word & another 因此,描述元素的 text() 应该进行 URL 转义。 我该怎么做
我是一名优秀的程序员,十分优秀!