- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试构建一个二叉树,它将按字母顺序保存文件中的单词,并计算文件中单词出现的次数。然后我必须能够替换原始文本文件中的单词。现在我只是尝试设置我的二叉树并将单词放入其中。字符串标记有效,它将打印每行的单词和标点符号。我还必须将标点符号存储在字符数组中并计算其出现次数。我的插入功能遇到问题,但我不确定我做错了什么。我遇到段错误。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
Name: Marcus Lorenzana
*/
//binary tree struct to hold left and right node
//as well as the word and number of occurrences
typedef struct node
{
char* word;
int count;
struct node *left;
struct node *right;
}
node;
node *insert(node *item, char *word);
char* readFile(char* filename);
int main()
{
FILE *fin;
char *word;
fin = fopen("data.txt", "r");
char* filecontents = readFile("data.txt");
//create dictionary node
node *dictionary;
dictionary = NULL;
//read words and punctuation in from the text file
word = strtok (filecontents, " \n");
int i = 0;
while (word != NULL)
{
printf("%s\n",word);
insert(dictionary,word);
printf("%s",dictionary->word);
word = strtok (NULL, " \n");
i++;
}
return 0;
}
//not sure if works
node *insert(node *item, char *word)
{
if(item==NULL)
{
item= (node*) malloc(sizeof(node));
strcpy(item->word, word);
item->left=NULL;
item->right=NULL;
item->count++;
}
else
{
if(strcmp(word, item->word)<0)
{
item->left=insert(item->left, word);
item->count++;
}
else if(strcmp(word, item->word)>0)
{
item->right=insert(item->right, word);
item->count++;
}
else
{
item->count++;
}
}
return item;
}
char* readFile(char* filename)
{
FILE* file = fopen(filename,"r");
if(file == NULL)
{
return NULL;
}
fseek(file, 0, SEEK_END);
long int size = ftell(file);
rewind(file);
char* content = calloc(size + 1, 1);
fread(content,1,size,file);
return content;
}
最佳答案
您的 insert
函数存在两个问题。
结构节点
,否则,如果您打算改变,则应在每次递归调用时返回
使用指向结构节点
的单个指针。malloc
内存。要查找代码其他部分的问题,请使用 valgrind
( here )。它是调试内存泄漏或段错误的绝佳工具。
为了解决问题 1,我将展示传递单个指针并返回
的示例(仍在变化)。您的插入函数(问题 2 已解决)应如下所示:
node *insert( node *item, char *word ) {
if ( item == NULL ) {
node *new_item = malloc( sizeof( struct node ) );
new_item->word = malloc( sizeof( char ) * ( strlen( word ) + 1 ) ); // Note, this line (p2).
strcpy( new_item->word, word );
new_item->count = 1; // << Note change here.
new_item->left = NULL;
new_item->right = NULL;
return new_item;
} else {
int cmp_result = strcmp( word, item->word );
if ( cmp_result < 0 ) {
item->left = insert( item->left, word );
item->count++;
} else if ( cmp_result > 0 ) {
item->right = insert( item->right, word );
item->count++;
} else {
// Node already exists, do what you see fit here.
}
}
return item;
}
为了解决问题 2,错误位于创建新节点的代码块中。看这里:
item = ( node* )malloc( sizeof( node ) );
strcpy( item->word, word ); // << Here, invalid (error).
...您没有为该字malloc
内存块。您正在做的是覆盖结构内的内存,并可能覆盖您尚未分配的其他内存地址(取决于垃圾值何时为 0 以模拟 NULL
终止符)。这是未定义的行为。
解决方案是执行以下操作:
item = ( node* ) malloc( sizeof( node ) );
item->word = malloc( sizeof( char ) * ( strlen( word ) + 1 ) ); // << Fix.
strcpy( item->word, word ); // << Now, valid.
...注意 + 1
以确保有 NULL
终止符的空间,因为 strlen
返回数组的字符串长度传递给它的字符数。
备注:
malloc
的结果也不是一个好主意,但这完全取决于您,因为它不会导致错误(但可能会在出现错误时不显示错误消息)应该)。main( void )
而不是 main( )
,除非您打算使用该功能。关于无法让我的插入函数在字母二叉字符串树中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18245845/
在 MySQL 数据库中,我在表中有一列既有纯数字也有混合数字/字母。没有模式,如果是纯数字我想区分,标记为true,否则为false。有什么好的方法可以使用吗?我试过: ID REGEXP '^[[
这个问题在这里已经有了答案: Numbers as column names of data frames (2 个回答) Why am I getting X. in my column names
尝试提出一个正则表达式来捕获诸如 AB1234 或 BA2321 之类的组。本质上需要捕获以 AB 或 BA 开头并后跟 4 位数字的任何内容。 目前,我有类似的东西,但这似乎没有考虑数字 (AB|B
var z = []; for(var i = 1; i len) z.push("a".repeat(len-i%len)) console.log(z.join("\n")); 关于jav
我需要一个仅用于数字、字母、空格和连字符的正则表达式。 像这样的 ^[a-zA-Z0-9]+$ 得到字母和数字,但我需要一个用于上述。这些真的很难理解! 最佳答案 这是你需要的: /^[0-9A-Za
有没有人可以帮助我解决 PDFBox 中的字母问题我正在尝试打印字母“ń”(波兰语字母)并且我得到了类似 þÿ J 的东西。 Dı B R O W 2S0 :K0 3I. 请帮忙! 最佳答案 我遇到了
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 5 年前。 Improve this ques
我尽量不让我的文本 block 把一个词分成几个部分并跳到下一行。对于每种设备尺寸,文本都会中断并造成可读性问题。我尝试将 marring-right 与 % 一起使用,但并没有太大帮助。 这是我的哈
这是我第一次向 Stack Overflow 发帖提问。我是编程新手,所以如果我说的奇怪或错误,请原谅。 在下面的文件中;它读取目录并将其保存到变量 nAddress 中。然后删除文件扩展名;将文件分
我希望当用户将鼠标悬停在页面上时,我的页面上的某些文本会重新排列字母。例如,将鼠标悬停在“WORK”上,它就会变成“OWKR”。我怀疑需要 js,但我对 js 还是很陌生。下面是我的 html:
我已经为此工作了几个小时,现在我有点卡住了....请帮助我。我是一个完全的编程障碍。除字母表方法外,所有方法都可以正常工作。 它将接收两个字符(大写或小写)并返回由给定 char 值范围组成的字符串。
我想编写一个程序,在输入的同一行中读取 n 个不同化学元素的名称(其中 1 ≤ n ≤ 17 和 n 也在输入中读取)(名称由空格分开)。化学元素的名称应存储在不同的字符串中以供进一步使用。 由于 n
我想隐藏一个字母,并在链接中显示另一个字母,当然,悬停字母的样式不同。例如: 这是一个... ...normal link. 这是一个... ...hovêrêd lînk. 如何实现?谢谢。 编辑:
我一直被这个相当愚蠢的想法所挑战。 所以我可以用 Blabla[span class=superI]i[/span]rest 替换所有出现的“i”:) 我的想法是在真正的 i“后面”添加一个额外的(红
本文以实例演示5种验证码,并介绍生成验证码的函数。PHP生成验证码的原理:通过GD库,生成一张带验证码的图片,并将验证码保存在Session中。 ?
下面给大家介绍下JS正则表达式 必须包含数字、字母、特殊字符 js正则表达式要求: 1. 必须包含数字、英文字母、特殊符号且大于等于8位 2. 特殊符号包括: ~!@#$%^&* 正
我在这里和网上四处寻找解决方案。 问题是我只想接受信件。但是,如果我至少输入一个字母,无论是否有符号或数字,它都会接受。如何获得仅 封信? if (!preg_match("/[a-zA-Z]/",
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 去年关闭。 Improve th
制表符分隔的文本文件,实际上是数据库表的导出(使用 bcp),具有以下形式(前 5 列): 102 1 01 e113c 3224.96 12 102 1 01 e185
我需要循环遍历数据数组并为每个数组值打印一个“递增”字母。我知道我可以做到这一点: $array = array(11, 33, 44, 98, 1, 3, 2, 9, 66, 21, 45); //
我是一名优秀的程序员,十分优秀!