不太确定这里发生了什么,无论我只是一个傻瓜还是编译器有点奇怪。
下面的代码应该,在调用我的 searchList 函数后,从用户那里获取输入,但是程序只是终止,甚至没有段错误,它实际上只是结束了。有什么傻事?
编辑:searchNode 是 searchList,抱歉错字。
干杯。
typedef struct List {
char c;
struct List *next;
}List;
List* insertNode(char c, List* t1);
List* addNode(void);
List* searchList(List *t1);
int main(void) {
List *z = addNode();
List *search_result;
char s;
while ( z != NULL) {
printf("%c", z->c);
z = z->next;
}
search_result = searchList(z);
return 0;
}
List *addNode(void) {
List *head = (List*)calloc(1,sizeof(List));
char c;
while (( c = getchar()) != '.') {
head = insertNode(c, head);
}
return head;
}
List *insertNode(char c, List* t1) {
List *tail = (List*)calloc(1,sizeof(List));
tail->c = c;
tail->next = t1;
return tail;
}
List *searchList(List *t1) {
char c;
printf("Please enter a search term");
scanf("%c", &c);
while (t1 != NULL) {
if (t1->c == c) {
return t1;
}
t1 = t1->next;
}
return 0;
}