gpt4 book ai didi

用 C 创建并打印一棵树

转载 作者:行者123 更新时间:2023-11-30 15:12:28 25 4
gpt4 key购买 nike

我正在尝试创建一个程序,该程序根据文件中的所有单词创建一棵树,并对相同的单词进行排序。这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SIZE 30


typedef struct node *tree;
typedef struct node {
tree left;
tree right;
struct dictionary {
char *word;
char *sortedWord;
} value;
} node;


void swap(char *a,char *b){
char tmp;
tmp = *a;
*a = *b;
*b = tmp;
}


char * bubble_sort(char *word){
char *ptr = word;
int n = strlen(word);

int i,j;

for(i=0;i<n-1;i++)
for(j=0;j<n-i-1;j++)
if(*(ptr+j)>*(ptr+j+1))
swap((ptr+j),(ptr+j+1));
return word;
}

char *RemoveNewLines(char *word){
char *newString = malloc(SIZE * sizeof(char));
char ch;
int currentLetter = 0;
int len = strlen(word);

while (currentLetter < len){
ch = word[currentLetter];
if (ch != '\n'){
newString[currentLetter] = ch;
}
else {
newString[currentLetter] = '\0';
}
currentLetter++;
}
return newString;
}


void createTree(tree memory){
// FILE *fp = fopen("words.txt","r");
// FILE *fp = fopen("words_no_duplicates1.txt","r");
FILE *fp = fopen("words_no_duplicates2.txt","r");
char *word = (char *)malloc(SIZE * sizeof(char));

if(fp == NULL)
{
perror("Error opening file");
exit(1);
}

// create the top node of the tree
node seedNode;
memory = &seedNode;
// initially just store the seed in the tree
memory->left = NULL;
memory->right = NULL;

fgets (word,SIZE,fp);

memory->value.word = (char *)malloc(SIZE * sizeof(char));
memory->value.word = strcpy(memory->value.word,RemoveNewLines(word));
// printf("%s\n",memory->value.word);

memory->value.sortedWord = (char *)malloc(SIZE * sizeof(char));
memory->value.sortedWord = strcpy(memory->value.sortedWord,bubble_sort(RemoveNewLines(word)));
// printf("%s\n",memory->value.sortedWord);
// printf("\n");

while ( fgets (word,SIZE,fp) != NULL){
memory->left = NULL;
memory->right = NULL;

memory->value.word = (char *)malloc(SIZE * sizeof(char));
memory->value.word = strcpy(memory->value.word,RemoveNewLines(word));


memory->value.sortedWord = (char *)malloc(SIZE * sizeof(char));
memory->value.sortedWord = strcpy(memory->value.sortedWord,bubble_sort(RemoveNewLines(word)));


// printf("%s\t %s\n",memory->value.word,memory->value.sortedWord);
// printf("%s\n",memory->value.word);
// printf("%s\n",memory->value.sortedWord);
// printf("\n");
}

fclose(fp);
}

void printTree(tree memory){
if (memory != NULL){
printTree(memory->left);
printf("%s",memory->value.word);
printf("%s",memory->value.sortedWord);
printf("\n");
printTree(memory->right);
}
}


int main(){
node seedNode;
tree memory = &seedNode;
createTree(memory);
printTree(memory);

return 0;
}

如果我取消注释 createTree 函数中的打印语句,一切看起来都不错并打印我想要的内容。但如果我尝试使用 printTree 函数,则会以“段错误”结束。我哪里出错了?

您可以使用每行包含一个单词的随机文件进行测试。

#

在 Joachim Pileborg 回复后,我更改了代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SIZE 30

typedef struct node *tree;
typedef struct node {
tree left;
tree right;
struct dictionary {
char *word;
char *sortedWord;
} value;
} node;

node seedNode;
tree memory = &seedNode;


void swap(char *a,char *b){
char tmp;
tmp = *a;
*a = *b;
*b = tmp;
}


char * bubble_sort(char *word){
char *ptr = word;
int n = strlen(word);

int i,j;

for(i=0;i<n-1;i++)
for(j=0;j<n-i-1;j++)
if(*(ptr+j)>*(ptr+j+1))
swap((ptr+j),(ptr+j+1));
return word;
}

char *RemoveNewLines(char *word){
char *newString = malloc(SIZE * sizeof(char));
char ch;
int currentLetter = 0;
int len = strlen(word);

while (currentLetter < len){
ch = word[currentLetter];
if (ch != '\n'){
newString[currentLetter] = ch;
}
else {
newString[currentLetter] = '\0';
}
currentLetter++;
}
return newString;
}


tree * createTree(tree *memory){
// FILE *fp = fopen("words.txt","r");
FILE *fp = fopen("words_no_duplicates1.txt","r");
char *word = (char *)malloc(SIZE * sizeof(char));

if(fp == NULL)
{
perror("Error opening file");
exit(1);
}

// create the top node of the tree
//node seedNode;

// initially just store the seed in the tree
(*memory)->left = NULL;
(*memory)->right = NULL;

fgets (word,SIZE,fp);

(*memory)->value.word = (char *)malloc(SIZE * sizeof(char));
(*memory)->value.word = strcpy((*memory)->value.word,RemoveNewLines(word));
// printf("%s\n",memory->value.word);

(*memory)->value.sortedWord = (char *)malloc(SIZE * sizeof(char));
(*memory)->value.sortedWord = strcpy((*memory)->value.sortedWord,bubble_sort(RemoveNewLines(word)));
// printf("%s\n",memory->value.sortedWord);
// printf("\n");

while ( fgets (word,SIZE,fp) != NULL){
(*memory)->left = NULL;
(*memory)->right = NULL;

(*memory)->value.word = (char *)malloc(SIZE * sizeof(char));
(*memory)->value.word = strcpy((*memory)->value.word,RemoveNewLines(word));


(*memory)->value.sortedWord = (char *)malloc(SIZE * sizeof(char));
(*memory)->value.sortedWord = strcpy((*memory)->value.sortedWord,bubble_sort(RemoveNewLines(word)));


// printf("%s\t %s\n",memory->value.word,memory->value.sortedWord);
// printf("%s\n",memory->value.word);
// printf("%s\n",memory->value.sortedWord);
// printf("\n");
}

fclose(fp);

return memory;
}


void printTree(tree memory){
if (memory != NULL){
printTree(memory->left);
printf("%s\n",memory->value.word);
printf("%s\n",memory->value.sortedWord);
printf("\n");
printTree(memory->right);
}
}


int main(){

createTree(&memory);
printTree(memory);

return 0;
}

虽然现在 createTree 函数对我来说看起来很奇怪,但在执行代码后,我已经打印了最后一个单词,并从文件 Words_no_duplicates1.txt 中进行了排序(这些是值内存->value.word 和内存->value.txt)。 sortedWord) 树内存->左和内存->右保持为空。你能帮忙吗?

最佳答案

您的代码有很多问题,我将指出几个对我来说很突出的问题。

createTree 函数中,有这两行

node seedNode;
memory = &seedNode;

这些很麻烦,因为首先参数内存是按值传递的,这意味着调用函数时传递的值被复制并且您拥有的一切函数内部是局部变量及其值的副本。更改变量不会更改调用函数时使用的原始变量。您可以通过返回指针或模拟通过引用传递来解决此问题。

此赋值的第二个问题是使其指向局部变量。一旦函数返回,局部变量就会超出范围,并且将不再存在。保留并取消引用指向此(不存在)变量的指针将导致未定义的行为,并且很可能会崩溃。这可以通过根本不进行赋值而仅依赖于传递参数的指针来解决,或者通过为节点动态分配内存来解决。

关于用 C 创建并打印一棵树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35060774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com