我有一个程序以字母顺序递归地将文本文件中的字母排序到链接列表中。我不确定我的错误在哪里,但它目前无限地打印相同的符号,直到我退出程序。我不确定我的错误可能在哪里,但我猜它与调用int和chars有关,因为我正在尝试打印字符。在确定如何正确打印我的程序方面的任何帮助都会很棒。
这是我的程序打印的内容:
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
但是应该按字母顺序打印文本文件:
abcdefghiijklmnopqrstuvwxyz
以下是代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct list_node_alph{
int key;
struct list_node_alph *rest_old;
}list_node_order;
typedef struct{
list_node_order *the_head;
int size;
}order_list;
list_node_order *rec_alph_order(list_node_order * old_list, int new_key);
void insert_node(order_list *the_alph, int key);
void print_alph(order_list my_list);
list_node_order *rec_alph_order(list_node_order *old_list, int new_key){
list_node_order *new_list;
if(old_list == NULL){
new_list = (list_node_order *)malloc(sizeof (list_node_order));
new_list->key = new_key;
new_list->rest_old = NULL;
}else if (old_list->key >= new_key){
new_list = (list_node_order *)malloc(sizeof (list_node_order));
new_list->key = new_key;
new_list->rest_old = old_list;
}else {
new_list = old_list;
new_list->rest_old = rec_alph_order(old_list->rest_old, new_key$
}
return (new_list);
}
void insert_node(order_list * the_alph, int key){
++(the_alph->size);
the_alph->the_head = rec_alph_order(the_alph->the_head, key);
}
void print_alph(order_list my_list){
printf("Pangram in alphabetical order: ");
while(my_list->head != NULL){ //ERROR
printf("%c", my_list->the_head); //ERROR
}
}
int main(void){
int ch_count;
int count_pangram;
char *pang_arr;
FILE *alph_text;
alph_text = fopen("pangram.txt", "r");
if(alph_text == NULL){
printf("Empty file. \n");
}
order_list my_alph = {NULL, 0};
while(( ch_count = fgetc(alph_text)) != EOF){
putchar(ch_count);
char next_key;
int the_count;
for(the_count = 0; the_count < 100; the_count++){
if(fscanf(alph_text, "%c", &next_key) != ' '){
//order_list my_alph = {NULL, 0};
//for(next_key; next_key != SENT; scanf("&c", &next_key$
insert_node(&my_alph, next_key);
}
}
}
print_alph(my_alph);
fclose(alph_text);
return(0);
}
答案 0 :(得分:0)
请更改您的void print_alph(order_list my_list);
定义,如下所示:
void print_alph(order_list* my_list){
if (my_list == NULL)
{
printf("\nprint_alph received NULL input");
return;
}
printf("\nPangram in alphabetical order: \n");
list_node_order* head = my_list->the_head;
while (head != NULL){
printf("%c", head->key);
head = head->rest_old;
}
}
现在它应该可以正常工作,如果你从主要的地方调用它:
print_alph(&my_alph);
希望这有帮助。
答案 1 :(得分:0)
这是完整的解决方案。
此程序可根据您的要求运行。
我在这里使用了Gopalakrishna Palem的打印功能。
#include <stdio.h>
#include <stdlib.h>
typedef struct list_node_alph{
int key;
struct list_node_alph *rest_old;
}list_node_order;
typedef struct{
list_node_order *the_head;
int size;
}order_list;
list_node_order *rec_alph_order(list_node_order * old_list, int new_key);
void insert_node(order_list *the_alph, int key);
list_node_order *rec_alph_order(list_node_order *old_list, int new_key) {
list_node_order *new_list;
if(old_list == NULL){
new_list = (list_node_order *)malloc(sizeof (list_node_order));
new_list->key = new_key;
new_list->rest_old = NULL;
}else if (old_list->key >= new_key){
new_list = (list_node_order *)malloc(sizeof (list_node_order));
new_list->key = new_key;
new_list->rest_old = old_list;
}else {
new_list = old_list;
new_list->rest_old = rec_alph_order(old_list->rest_old, new_key);
}
return (new_list);
}
void insert_node(order_list * the_alph, int key){
++(the_alph->size);
the_alph->the_head = rec_alph_order(the_alph->the_head, key);
}
void print_alph(order_list* my_list){
if (my_list == NULL)
{
printf("\nprint_alph received NULL input");
return;
}
printf("\nPangram in alphabetical order: \n");
list_node_order* head = my_list->the_head;
while (head != NULL){
printf("%c", head->key);
head = head->rest_old;
}
}
int main(void){
int ch_count;
int count_pangram;
char *pang_arr;
FILE *alph_text;
alph_text = fopen("pangram.txt", "r");
if(alph_text == NULL){
printf("Empty file. \n");
}
order_list my_alph = {NULL, 0};
while(( ch_count = fgetc(alph_text)) != EOF){
char next_key;
int the_count;
printf("Inserted in array %c \n", next_key); //ERROR
insert_node(&my_alph, (char)ch_count);
for(the_count = 0; the_count < 100; the_count++){
if(fscanf(alph_text, "%c", &next_key) > 0){
printf("Inserted in array %c \n", next_key); //ERROR
insert_node(&my_alph, next_key);
}
}
}
print_alph(&my_alph);
fclose(alph_text);
return(0);
}
查看更改并了解编程。!!