我必须为排序字符串的作业创建二叉搜索树。我运行调试模式,发现分段错误的问题在哪里,但我不知道问题是什么或如何解决它。我得到了
在testbst.c中的#0 0x0000000000400969(node = 0x7fffffffe008,data = 0x7fffffffe010“run \ n”,c_flag = 0)
在testbst.c中的#1 0x0000000000400df8(argc = 1,argv = 0x7fffffffe188):156“
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
typedef struct treeNode
{
char *data;
int count;
struct treeNode *left;
struct treeNode *right;
} treeNode;
void Insert(treeNode** node,char* data,int c_flag)
{
if(node==NULL)
{
treeNode *temp;
temp = (treeNode *)malloc(sizeof(treeNode));
temp -> data = data;
printf("%sdata", data);
printf("\n%stemp", temp -> data);
temp -> left = NULL;
temp -> right = NULL;
*node = temp;
return;
}
if(c_flag == 1)
{
if(caseCompare((*node)->data, data) == 1)
Insert(&(*node)->left, data, c_flag);
if(caseCompare((*node)->data, data) == -1)
Insert(&(*node)->right, data, c_flag);
else
(*node)->count++;
}
else if(compare((*node)->data, data) == 1)
Insert(&(*node)->left, data, c_flag);
if(compare((*node)->data, data) == -1)
Insert(&(*node)->right, data, c_flag);
else
(*node)->count++;
}
void PrintInorder(treeNode *node)
{
if(node==NULL)
{
return;
}
PrintInorder(node->left);
printf("%d ",node->data);
PrintInorder(node->right);
}
void DeletePostorder(treeNode *node)
{
if(node != NULL)
{
DeletePostorder(node->left);
DeletePostorder(node->right);
if(node->left!=NULL)
free(node->left);
if(node->right != NULL)
free(node->right);
free(node);
}
}
int compare(char* string1, char* string2)
{
int str1 = strlen(string1);
int str2 = strlen(string2);
int bigger;
int i = 0;
if(str1 > str2)
bigger = str1;
if(str1 < str2)
bigger = str2;
for(i = 0; i < bigger; i++)
{
if(tolower(string1[i]) > tolower(string1[i]))
return 1; //returns 1 if string 1 is farther letter
if(tolower(string1[i]) < tolower(string2[i]))
return -1; //returns -1 if string 1 letter is behind
}
return 0; //returns 0 if same words
}
int caseCompare(char* string1, char* string2)
{
int str1 = strlen(string1);
int str2 = strlen(string2);
int bigger;
int i = 0;
if(str1 > str2)
bigger = str1;
if(str1 < str2)
bigger = str2;
for(i = 0; i < bigger; i++)
{
if(string1[i] > string1[i])
return 1; //returns 1 if string 1 is farther letter
if(string1[i] < string2[i])
return -1; //returns -1 if string 1 letter is behind
}
return 0; //returns 0 if same words
}
int main(int argc, char **argv)
{
extern char *optarg;
extern int optind;
FILE* infile = stdin;
FILE* outfile = stdout;
char string[100];
treeNode* tree = NULL;
int cflag = 0, oflag = 0, c, err;
static char usage[] = "usage: %s [-c] [-o output_file_name] [input_file_filename]\n";
while ((c = getopt(argc, argv, "co:")) != -1)
switch (c)
{
case 'c':
cflag = 1;
break;
case 'o':
oflag = 1;
outfile = fopen(optarg, "w");
break;
case '?':
err = 1;
printf("%s\n", usage);
break;
}
if((argc - optind) > 1)
{
printf("Input Incorrect \n%s\n", usage);
exit(0);
}
if(argv[optind] != NULL)
if((infile = fopen(argv[optind], "r")) == NULL)
{
printf("File \"%s\" does not exist\nType in your sentences\n", argv[optind]);
infile = stdin;
}
while(fgets(string, sizeof(string), infile) != NULL && string[0] != '\n')
{
printf("%s", string);
Insert(&tree, string, cflag);
}
PrintInorder(tree);
DeletePostOrder(tree);
}