为什么我的 link 列表有错误的数据?

Why does my link list have the wrong data?

当打印出我的链表时,显示的不是我想的那样。如何获得正确的输出?

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 值转换为其整数值,您只需要

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] != '[=11=]')
    {
        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;
}

您有一个以相反顺序结束的链表,这就是为什么您的第二个示例以相反顺序出现的原因。如果你像这样打印链表:

printf("%d ", number->data);

加上space,会更清楚发生了什么,输出是ASCII值

53 52 51 50 49

但是您也将字符值与数值混淆了。如果您更正此问题,

printf("%c ", number->data);

您将得到您原来输入的字符的倒序。

5 4 3 2 1