首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >c中的链接列表,意外结果

c中的链接列表,意外结果
EN

Stack Overflow用户
提问于 2012-02-14 01:25:31
回答 5查看 207关注 0票数 2

我是编程新手,正在尝试学习链表。我决定通过编写一个简单的程序来尝试使用链表,该程序将从文件中读取数据,一次读取一个字符,并将每个字符插入到一个链表中。然后我把链表打印出来。很简单,对吧?好吧,如果这是你的第一次,也许不会。我正在学习在线教程哦,非常仔细,但是我的输出并不是它应该的样子。(程序编译和运行时不会出现错误或警告。我正在使用代码块。)我得到的不是任何字符,而是两个数字,仅此而已。

下面是我写的代码:

代码语言:javascript
运行
复制
#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):

代码语言:javascript
运行
复制
<I don't know why every beginner program says hello world,
but hello world anyway.>

我真的很感谢看到这一点并为我指明了正确方向的人。

EN

回答 5

Stack Overflow用户

发布于 2012-02-14 01:31:44

你不应该在读循环结束时释放current_token。这将导致问题,因为您正在释放位于链表内的节点的内存。

此外,作为附注,当给定一个表示数字的字符时,isdigit成功,例如字符'1‘。你应该仍然使用%c来打印出来,因为%d会给你字符'1‘的ascii编号。

票数 5
EN

Stack Overflow用户

发布于 2012-02-14 01:38:25

你需要if (strcmp(a,b)==0)来测试等价性。

票数 4
EN

Stack Overflow用户

发布于 2012-02-14 01:40:54

你绝对不想free(current_token).

  • If你想把一个字符转换成一个整数,一个简单的方法(在ASCII语言中)是:

代码语言:javascript
运行
复制
    char c = '3';
    int i = c - '0';
    printf("As a char: %c\n", c); // Prints 3.
    printf("As an int: %d\n", i); // Prints 3.

  • 当您锁定数据时,可能且很可能会在该区域中存在随机数据。一旦你使用memset将整个结构锁定,你就应该清除它。
  • 正如在其他答案中提到的,strcmp在匹配时返回0。当字符是空格或换行符时,
  • 结构中的数据是非常奇怪的。这是因为数据开始时处于未确定的状态。

如果你修正了上面的错误(不包括memset),你的程序的输出会更像你想要的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9264976

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档