【问题标题】:C break int to digits and store it in linked list [closed]C将int分解为数字并将其存储在链表中[关闭]
【发布时间】:2017-04-07 12:48:35
【问题描述】:

我在链表上有这个练习。

我已成功管理create_list()print_number() 功能,但我似乎无法管理listnode_t *int_to_list(int num)。 请注意,该练习基于代码模板,因此您看到的任何内容(main 和带有参数的额外函数)都带有前缀。

我们只需要在函数内部填写代码,这样代码就可以顺利编译运行。

代码如下:

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

typedef struct node {
    int digit;
    struct node *next;
} listnode_t;

listnode_t *create_list(int numdigits);
void print_number(listnode_t *head);
listnode_t *int_to_list(int num);


void freelist(listnode_t * head) {
    listnode_t * currentNode = head;
    listnode_t * node_to_free = NULL;
    while(currentNode->next != NULL) {
        node_to_free = currentNode;
        currentNode = currentNode->next;
        free(node_to_free);
    }
}

int main(int argc, char** argv)
{
    listnode_t *number1, *number2, *result;

    printf("Insert a number\n");
    number1 = create_list(3);
    printf("Insert another number\n");
    number2 = create_list(5);


    printf("First number: ");
    print_number(number1);
    printf("\n");

    printf("Second number: ");
    print_number(number2);
    printf("\n");



    //  convert int to list
    number1 = int_to_list(1024);
    print_number(number1);
    printf("\n");

freelist(number1);
    freelist(number2);

    return 0;
}

listnode_t * create_list(int numdigits)
{
    int number[numdigits];
        int i;
    for(i=0; i < numdigits; i++) {
        printf("Dose to %d stoixeio tou akeraiou: ", i + 1);
        scanf("%d", &number[i]);
    }

    listnode_t * head = (listnode_t *)malloc(sizeof(listnode_t));
    if(head == NULL) {
        return NULL;
    }
    head->digit = number[numdigits - 1];
    head->next = NULL;

    listnode_t * currentNode = head;

    for(i = numdigits - 2; i >= 0; i--) {
        listnode_t * node = (listnode_t *)malloc(sizeof(listnode_t));
        node->digit = number[i];
        node->next = NULL;
        currentNode->next = node;
        currentNode = node;
    }

    return head;
}


void print_number(listnode_t * head)
{
    listnode_t * currentNode = head;
    while(currentNode != NULL) {
        printf("%d", currentNode->digit);
        currentNode = currentNode->next;
    }

}

/* The function takes an integer and converts it to a list.
   The functio returns a pointer to the first node of the list.*/

listnode_t *int_to_list(int num)
{
        int newnum;

        listnode_t * head = malloc(sizeof(listnode_t));
        listnode_t * current =  head;
        current->next = malloc(sizeof(listnode_t));
        while (current->next != NULL) {
            current = current->next;
        }

                newnum = num % 10;
                head->digit=newnum;
                head->next=current;

                num = num / 10;
                while (num > 1 && num < 9) {
                        current->digit=newnum;
                        current->next=NULL;
                }
                newnum = num % 10;

    return head;
}

感谢所有帮助。

【问题讨论】:

  • 你已经有了一个从数字序列创建列表的函数 (create_list)。您没有创建该功能吗?如果您没有创建 create_list 函数,也许您可​​以查看它以获取有关如何创建列表的灵感
  • 1) current 未在 int_to_list 中声明
  • 2) 原型listnode_t *int_to_list(int num) 添加; 到最后。
  • 3) freelist 不会释放最后一个节点。`
  • 对不起我的错..代码没有正确执行。我正在纠正它

标签: c struct linked-list


【解决方案1】:

给定输入 1024,您将创建一个 new 列表,如下所示:

(1, *)---->(0, *)---->(2, *)---->(4, NULL)

所以你需要这样的东西来构建列表:

listnode_t *int_to_list(int num)
{
    listnode_t * head = NULL;
    listnode_t * current;
    int first_time = 1;

    while( NOT_DONE )
    {
        if (first_time)
        {
            first_time = 0;
            head =  malloc(sizeof(listnode_t));
            current = head;
        }
        else
        {
            current->next =  malloc(sizeof(listnode_t));
            current = current->next;
        }
        current->next = NULL;

        current->digit = CURRENT_DIGIT;
    }
    return head;
}

这里缺少的是NOT_DONECURRENT_DIGIT,我将留给 OP 来解决。它可以通过多种方式完成。一种方法是使用sprintfint 转换为字符串(例如"1024"),然后从那里获取数字,例如str[i] - '0'

【讨论】:

  • 谢谢!我被逻辑困住了!
猜你喜欢
  • 2020-09-01
  • 2011-12-24
  • 2015-07-05
  • 2021-12-16
  • 2010-10-20
  • 2021-12-12
  • 1970-01-01
  • 2013-07-28
  • 2016-03-03
相关资源
最近更新 更多