我是编程新手,想学习链表。我决定通过编写一个简单的程序来实验链表,该程序将从文件中读取,每次只读一个字符,并将每个字符插入链表中。然后我打印出链表。很简单,对吧?好吧,如果这是你第一次,也许不会。我在网上教程哦,如此小心,但我的输出不是它应该是。 (程序编译并运行时没有错误或警告,我正在使用代码块。)我没有获取任何字符,而是得到两个数字,就这些了。c中的链表,意想不到的结果
这里是我写的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Tokens_read_in{
char character;
int number;
char whichOne[5];
struct Tokens_read_in* next;
}Tokens;
int main()
{
//declare variables
char c;
//create a struct for the array that will hold the tokens
Tokens* token_array;
token_array = malloc(sizeof(Tokens));
token_array->next = NULL;
//open the input file
FILE *ifp; //input file pointer
char *filename = "input.txt";
ifp = fopen(filename, "r");
if(!ifp){
printf("Error in opening '%s' for reading!", filename);
exit(0);
}
while(!feof(ifp)){
//prepare to read in file one character at a time
c = getc(ifp);
//create a struct for the current token that is read in
Tokens* current_token = token_array;
//let the current_token point to the beginning of token_array
current_token = token_array;
//let the current_token point to the LAST of token_array
while(current_token->next != NULL){
current_token = current_token->next;
}
//create a node at the end of token_array
current_token->next = malloc(sizeof(Tokens));
//move the current_token to the last (new) of token_array
current_token = current_token->next;
if(current_token == NULL){
printf("Out of memory");
exit(0);
}
//plug character into current_token
current_token->next = NULL;
//letter
if(isalpha(c)){
printf("%c", c);
current_token->character = c;
strcpy(current_token->whichOne, "char");
}
//number
else if(isdigit(c))
{
printf("%d", (int)c);
current_token->number = (int)c;
strcpy(current_token->whichOne, "num");
}
//space
//this does not need to go into the token array
else if (c == ' '){
printf(" ");
}
//newline
//this does not need to go into the token array
else if (c == '\n'){
printf("\n");
}
//anything else
else if ((!isdigit(c) && !isalpha(c))){
printf("%c", c);
current_token->character = c;
strcpy(current_token->whichOne, "char");
}
//now that the current_token is plugged into token_array, free current_token
free(current_token);
}//end while(!feof(ifp))
//print the token_array
Tokens* conductor;
conductor = token_array;
while(conductor != NULL){
if(strcmp(conductor->whichOne, "num")){
printf("%d ", conductor->number);
}
else if(strcmp(conductor->whichOne, "char")){
printf("%c ", conductor->character);
}
conductor = conductor->next;
}
//done printing, so free conductor
free(conductor);
//done with program, so free token_array
free(token_array);
//close input file
fclose(ifp);
return 0;
}//end main
这里是我使用的输入文件(名为input.txt中):
<I don't know why every beginner program says hello world,
but hello world anyway.>
我真的很感激任何人谁看这个并指出我正确的方向。
每个初学者程序
说“你好世界”,因为经典文本* C程序设计语言*开始于一个程序,打印“Hello ,世界!“。 – Caleb 2012-02-13 17:27:12
您的程序有几个主要缺陷需要修复,然后才能继续。第一个缺陷是你要通过每次迭代收集一个新角色来重置和遍历你的列表。如果你只是坚持根(第一个)节点和终端(最后一个)节点,这是完全不必要的。接下来,您声明一个堆栈变量指向根节点并将其分配两次。最后,释放刚刚初始化的节点,从而创建无效指针的链接列表。你需要更好地理解C和完成你的任务的指针。 – BitBank 2012-02-13 17:37:47