c - 为什么我的链接列表有错误的数据?

标签 c string linked-list output character-arrays

打印链接列表时,显示的内容与我想象的不同。如何获得正确的输出?

struct node{ 
    int data; 
    struct node *next; 
};

struct node *newNode(int data){ 
    struct node *new_node=(struct node *) malloc(sizeof(struct node)); 
    new_node->data=data; 
    new_node->next=NULL; 
    return new_node; 
} 

void push(struct node*** head, int data){ 
    struct node* new_node=newNode(data); 
    new_node->next=(**head); 
    (**head)=new_node; 
} 

void create(struct node **number, char num[]){ 
    int x=0; 
    while(x<strlen(num)){ 
        int d=(int)(num[x]); 
        push(&number, d); 
        x++; 
   } 
}

void printList(struct node *number){ 
    while(number!=NULL){ 
       printf("%d", number->data); 
       number=number->next; 
    } 
    printf("\n"); 
} 

int main (void){ 
    struct node *first; 
    char num1[10]; 
    scanf("%s", num1); 
    create(&first, num1); 
    printList(first); 
    return 0; 
}

示例

Input          : 1
Expected Output: 1
Actual Output  : 49
<小时/>
Input          : 12345
Expected Output: 12345
Actual Output  : 5352515049

我认为它是打印存储值的位置,而不是值本身。 如果这是错误的,请纠正我。无论如何,我如何获得我想要的预期输出。

最佳答案

问题是您正在读取 ascii 值,然后尝试打印整数,并且由于您需要存储整数而不是 ascii 值,因此您需要的只是一个简单的数学运算,即减去数字 '0' 的 ascii 值,因此要将数字的 ascii 值转换为其整数值,您所需要的就是

integer = ascii - '0';

在这里我修复了您的代码,因为您将值附加到列表的头部而不是尾部

#include <stdlib.h>
#include <stdio.h>

struct node{
    int          data;
    struct node *next;
};

struct node *newNode(int data)
{
    struct node *new_node;

    new_node = malloc(sizeof(struct node));
    if (new_node == NULL) /* always check that malloc succeeded */
        return NULL;
    new_node->data = data;
    new_node->next = NULL;

    return new_node;
}

struct node *push(struct node *tail, int data)
{
    struct node *new_node;

    new_node = newNode(data);
    if (tail != NULL)
        return tail->next = new_node;
    return new_node;
}

struct node *create(char *numbers)
{
    size_t       i;
    struct node *head;
    struct node *tail;

    i    = 0;
    head = NULL;
    tail = NULL;
    /* since strings are 'nul' terminated, you just need to loop,
     * until you find the 'nul' byte, strlen() expects that byte
     * anyway.
     */
    while (numbers[i] != '\0')
    {
        tail = push(tail, numbers[i++] - '0');
        if (head == NULL)
            head = tail;
    }

    return head;
}

void printList(struct node *number)
{
    while (number != NULL)
    {
       printf("%d", number->data);
       number = number->next;
    }
    printf("\n");
}

void freeList(struct node *number)
{
    while (number != NULL)
    {
        struct node *last;

        last   = number;
        number = number->next;

        free(last);
    }
}

int main(void)
{
    struct node *first;
    char         numbers[10];

    /* '%9s' prevents buffer overflow */
    if (scanf("%9s", numbers) != 1)
        return -1;
    first = create(numbers);

    printList(first);
    freeList(first);

    return 0;
}

关于c - 为什么我的链接列表有错误的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28521573/

相关文章:

c++ - C/C++ 中定义的(XXXX) 宏?

java - 为什么LinkedList中的类Node定义为静态而不是普通类

c - 链接一个特殊的共享库

c - Visual Studio - 编译错误

c - 在被调用函数中 malloc 数组但在调用函数中释放它是否可以?

c - JSON 格式字符串上的 strtok 分隔符问题

python - 如何用零填充字符串?

python - 如果它出现在 b 之后,则删除 a 的正则表达式

C++ 泛型链表

c++ - 使用 2 个指针反转链表