我正在通过读取文本文件并在列表中按字母顺序插入字母来创建一个链表。我需要打印列表,但似乎无法获得正确的功能。我一直收到错误
error: invalid type argument of ‘->’ (have ‘order_list’)
error: invalid type argument of ‘->’ (have ‘order_list’)
我知道这是不正确的,但我对正确地陈述print_alph函数感到不知所措。如果您能帮助我找到正确打印清单的方法,我们将不胜感激。
#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);
}
在print_alph
()中,您正在传递类型为order_list
的实例因此,要访问其成员,您应该使用.
而不是->
所以更改
while(my_list->head != NULL){
至
while(my_list.the_head != NULL){
但我认为应该在print_alph()
中传递该对象的指针,而不是传递它的实例在这种情况下,->
可以访问其成员。
void print_alph(order_list *my_list)
并称之为
print_alph(&my_alph);
您需要使用。而不是print_alph函数内部的->,因为您没有将order_list作为指针传递
void print_alph(order_list my_list){
printf("Pangram in alphabetical order: ");
while(my_list.head != NULL){
printf("%c", my_list.the_head);
}
}