我是一名初学者,所以我的理念给了我一个完成的任务,在这个任务中我需要在链表中输入几个字符串,并且在我输入打印之后,他们需要打印按照正确的顺序,从第一个到最后一个。单链表C打印
这里是我的了:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Node {
char data;
struct Node *next;
}node;
char createlist(node *pointer, char data[100]) {
while (pointer->next != NULL) {
pointer = pointer->next;
}
pointer->next = (node*) malloc(sizeof(node));
pointer = pointer-> next;
pointer->data = *data;
pointer->next = NULL;
}
int main() {
node *first, *temp;
first = (node*) malloc(sizeof(node));
temp = first;
temp->next = NULL;
printf("Enter the lines\n");
while (1) {
char data[100];
gets(data);
createlist(first, data);
if (strcmp(data, "print") == 0)
printf("%s\n", first->data);
else if (strcmp(data, "quit") == 0)
return (0);
};
}
当我运行它,我得到: 输入线: asdfasdf 打印 (空)
任何帮助,因为这可以理解为我的第一次使用链接列表。
注意,他们说[你不应该投的malloc'()的结果在'C](http://stackoverflow.com/questions/605845/do -i-铸造了对结果的-的malloc)。 – MikeCAT
你不应该使用'gets()',它具有缓冲区溢出的不可避免的rsik。 – MikeCAT
您应该首次使用调试器。 –