gpt4 book ai didi

C 将 int 分解为数字并将其存储在链表中

转载 作者:行者123 更新时间:2023-11-30 20:21:17 25 4
gpt4 key购买 nike

我在链接列表上进行了这个练习。

我已成功管理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;
}

我感谢所有的帮助。

最佳答案

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

(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'

关于C 将 int 分解为数字并将其存储在链表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43278624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com