C 添加由链表表示的两个整数

C 添加由链表表示的两个整数,c,linked-list,C,Linked List,我遇到的问题。 以下是我迄今为止所做的工作 #include <stdio.h> #include <stdlib.h> struct node { int digit; struct node *next; }; struct node *make_node(int num, struct node *head); struct node *newNode(int digit); struct node *sum(struct node *num1, str

我遇到的问题。

以下是我迄今为止所做的工作

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

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

struct node *make_node(int num, struct node *head);
struct node *newNode(int digit);
struct node *sum(struct node *num1, struct node *num2);
void print(struct node *node);

int main()
{

  int a, b;
  struct node *new_nodeA1 = NULL, *new_nodeA2 = NULL;
  struct node *new_nodeB1 = NULL, *new_nodeB2 = NULL;
  struct node *res = NULL;

  printf("\nEnter no. of digits for your two numbers (separate with space)  ");
  scanf("%d %d", &a, &b);
  int n1[a], n2[b];

  printf("\n\nEnter first non-negative integer to add:  ");
  for (int j = 0; j < a; j++)
    scanf("%1d", &n1[j]);
  printf("Enter second non-negative integer to add:  ");
  for (int k = 0; k < b; k++)
    scanf("%1d", &n2[k]);

  /* for (int i = 0; i <= a - 1; i++)
    printf("%d\n", n1[i]);

    printf("%d\n", a); */

  for (int z = 0; z < a - 1; z++)
    {
      new_nodeA2 = make_node(n1[z], new_nodeA1);
      /*new_nodeA2 = newNode(n1[z]);*/
      if (new_nodeA1 == NULL)
    new_nodeA1 = new_nodeA2;
    }

  for (int y = 0; y < b - 1; y++)
    {
      new_nodeB2 = make_node(n2[y], new_nodeB1);
      if (new_nodeB1 == NULL)
    new_nodeB1 = new_nodeB2;
    }

  printf("\n");
  print(new_nodeA1);
  printf("\n");
  print(new_nodeB1);
  printf("\n")
  res = sum(new_nodeA2, new_nodeB2);
  printf("Result: ");
  print(res);
  return 0;

}

struct node *make_node(int num, struct node *head)
{

  struct node *temp = malloc(sizeof(struct node));

  if (temp == NULL)
    {
      fprintf(stderr, "Call of malloc() failed\n");
      exit(1);
    }

  if (head != NULL)
    head->next = temp;

  temp->digit = num;
  temp->next = NULL;

  return temp;

}

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

struct node *sum(struct node *num1, struct node *num2)
{
    struct node *res = NULL;
    struct node *temp, *prev = NULL;
    int carry = 0, sum;

    while (num1 != NULL || num2 != NULL)
    {
        sum = carry + (num1? num1->digit: 0) + (num2? num2->digit: 0);
        carry = (sum >= 10)? 1 : 0;
        sum = sum % 10;


        temp = newNode(sum);


        if(res == NULL)
            res = temp;
        else
            prev->next = temp;

        prev  = temp;

        if (num1) 
      num1 = num1->next;
        if (num2) 
      num2 = num2->next;
    }

    if (carry > 0)
      temp->next = newNode(carry);

    return res;
}

void print(struct node *node)
{
    while(node != NULL)
    {
        printf("%d->", node->digit);
        node = node->next;
    }
    printf("\n");
}
#包括
#包括
结构节点
{
整数位数;
结构节点*下一步;
};
结构节点*make_节点(int num,结构节点*head);
结构节点*新节点(整数位数);
结构节点*sum(结构节点*num1,结构节点*num2);
作废打印(结构节点*节点);
int main()
{
INTA,b;
结构节点*new\u nodeA1=NULL,*new\u nodeA2=NULL;
结构节点*new_nodeB1=NULL,*new_nodeB2=NULL;
结构节点*res=NULL;
printf(“\n输入两个数字的位数(用空格分隔)”;
scanf(“%d%d”、&a和&b);
int n1[a],n2[b];
printf(“\n\n输入要添加的第一个非负整数:”;
对于(int j=0;j数字=num;
temp->next=NULL;
返回温度;
}
结构节点*新节点(整数位数)
{
结构节点*新节点=(结构节点*)malloc(sizeof(结构节点));
新建节点->数字=数字;
新建节点->下一步=空;
返回新的_节点;
}
结构节点*sum(结构节点*num1,结构节点*num2)
{
结构节点*res=NULL;
结构节点*temp,*prev=NULL;
整数进位=0,和;
while(num1!=NULL | | num2!=NULL)
{
总和=进位+(num1?num1->位数:0)+(num2?num2->位数:0);
进位=(和>=10)?1:0;
总和=总和%10;
temp=新节点(总和);
如果(res==NULL)
res=温度;
其他的
上一个->下一个=温度;
prev=温度;
if(num1)
num1=num1->next;
如果(num2)
num2=num2->next;
}
如果(进位>0)
临时->下一步=新节点(进位);
返回res;
}
无效打印(结构节点*节点)
{
while(节点!=NULL)
{
printf(“%d->”,节点->数字);
节点=节点->下一步;
}
printf(“\n”);
}
我的输出


我的编译器没有给我任何错误。我试着调试我的
make_node
函数,但我无法理解为什么我的节点跳过某些数字的问题。

你的链表插入代码非常糟糕。对于这种样式的列表,你需要从
头->下一个
向下遍历列表,直到找到一个
null
,然后n插入那里。相反,您总是用新的
temp
节点替换
head->next
,从而打破列表


您还可以向后添加到列表中,每次都将新添加的项目作为标题,从而绕过添加的遍历过程,但请注意,这将使您的数字按相反顺序排列(这在添加时确实有帮助,因此这可能也很好)

然后我是否应该在
make_节点中包含一个
for循环
,并遍历列表,直到我的
head->next==NULL
,然后添加
temp->next=num
?这就是我要开始的地方。您已经必须为打印语句执行此操作,以便可以从已知代码复制。请不要发布屏幕截图。提供文本说明ead。位图不适合显示纯文本。@Gerhardh屏幕截图仅指问题和输出。我的代码是文本。这样可以更清晰地表示问题,使社区更容易提供帮助。不应使用教科书以外的任何资源。