C++ 使用链表添加两个数字
我试图添加两个由链表表示的数字。因此,我插入了数字3->2->8和6->5->4。我试着得到328和654这两个数字,然后将它们相加,并将它们插入第三个列表中。我在计算数字时遇到了一些问题。这是代码C++ 使用链表添加两个数字,c++,C++,我试图添加两个由链表表示的数字。因此,我插入了数字3->2->8和6->5->4。我试着得到328和654这两个数字,然后将它们相加,并将它们插入第三个列表中。我在计算数字时遇到了一些问题。这是代码 #include<bits/stdc++.h> using namespace std; struct Node{ int data; struct Node *next; }; int flag=1; void input(Node **head,int num) {
#include<bits/stdc++.h>
using namespace std;
struct Node{
int data;
struct Node *next;
};
int flag=1;
void input(Node **head,int num)
{
Node *temp=(struct Node*)malloc(sizeof(struct Node));
temp->data=num;
if(!flag)temp->next=*head;
if(flag){temp->next=NULL;flag=0;}
*head=temp;
}
void add(Node *l1,Node *l2,Node *l3)
{
Node *cur1=l1;
Node *cur2=l2;
int size1=0,size2=0;
while(cur1!=NULL)
{
size1++;
cur1=cur1->next;
}
while(cur2!=NULL)
{
size2++;
cur2=cur2->next;
}
int i=size1-1,j=size2-1,sum1=0,sum2=0;
cur1=l1;cur2=l2;
cout<<i<<endl;
while(cur1!=NULL)
{
cout<<cur1->data<<"\t";
cout<<cur1->data*pow(10,i)<<"\t";
sum1=sum1+cur1->data*pow(10,i);
cout<<sum1<<endl;
i--;
cur1=cur1->next;
}
cout<<sum1<<endl;
while(cur2!=NULL)
{ cout<<cur2->data<<"\t";
cout<<cur2->data*pow(10,j)<<"\t";
sum2=sum2+cur2->data*pow(10,j);
cout<<sum2<<endl;
j--;
cur2=cur2->next;
}
cout<<sum2<<endl;
sum1+=sum2;
while(sum1!=0)
{
int r=sum1%10;
input(&l3,r);
sum1/=10;
}
cur1=l3;
while(cur1!=NULL)
{
cout<<cur1->data;
cur1=cur1->next;
}
}
int main()
{
Node *l1=NULL;
Node *l2=NULL;
Node *l3=NULL;
input(&l1,8);
input(&l1,2);
input(&l1,3);
flag=1;
input(&l2,4);
input(&l2,5);
input(&l2,6);
add(l1,l2,l3);
return 0;
}
问题可能是由于截断。
pow
函数返回浮点值。然后将其转换为整数,从而导致截断
例如:
299.99999999 as float will become 299 as int
尝试先加0.5以取整
比如:
正如@viraptor所评论的,最好避免浮动(即pow)。尝试以下方法:
sum1 = 0;
while(cur1 != NULL)
{
sum1 = 10 * sum1;
sum1 = sum1 + cur1->data;
cur1=cur1->next;
}
然后所有的计算都是在整数上完成的,您不会因为float和int之间的转换而遇到问题。问题可能是由于截断。
pow
函数返回浮点值。然后将其转换为整数,从而导致截断
例如:
299.99999999 as float will become 299 as int
尝试先加0.5以取整
比如:
正如@viraptor所评论的,最好避免浮动(即pow)。尝试以下方法:
sum1 = 0;
while(cur1 != NULL)
{
sum1 = 10 * sum1;
sum1 = sum1 + cur1->data;
cur1=cur1->next;
}
然后所有的计算都是在整数上完成的,您不会因为float和int之间的转换而遇到问题。代码有点难以理解。您可以简单地对(简单地)链接列表求和,而不是提取每个列表的值,添加它们,然后存储结果 下面是一个简单的例子。请注意,
carry
必须作为引用传递,以便调用者在展开堆栈时可以访问修改后的值。此外,本例假设两个列表具有相同数量的节点。如果没有,您可以将0
添加到最短列表中
// basic node structure
template <typename T>
struct Node
{
T data = 0;
Node<T>* next = nullptr;
};
// function to sum 2 simply linked lists
Node<int>* sumLists(Node<int>* l1, Node<int>* l2, int& carry, int k = -1)
{
if (!l1 && !l2)
{
// we reached the end of both lists
return nullptr;
}
++k; // index of the largest list
// iterate to the end of both lists, head is the head of the result list
auto head = sumLists(l1 ? l1->next : l1, l2 ? l2->next : l2, carry, k);
// compute the value and carry
int value = carry;
if (l1) value += l1->data;
if (l2) value += l2->data;
carry = value / 10;
// insert the value into the result list
Node<int>* node = new Node<int>();
node->data = value % 10;
if (!head)
{
head = node;
}
else
{
node->next =head;
head = node;
}
// add carry for the last iteration
if ((k == 0) && (carry != 0))
{
Node<int>* node = new Node<int>();
node->data = carry;
node->next = head;
head = node;
}
return head;
}
//基本节点结构
模板
结构体类型
{
T数据=0;
Node*next=nullptr;
};
//函数对2个简单链表求和
节点*sumLists(节点*l1,节点*l2,整数和进位,整数k=-1)
{
如果(!l1&&!l2)
{
//我们到达了两个列表的末尾
返回空ptr;
}
++k、 //最大列表的索引
//迭代到两个列表的末尾,head是结果列表的head
自动头=sumLists(l1?l1->next:l1,l2?l2->next:l2,进位,k);
//计算值并进行进位
int值=进位;
如果(l1)值+=l1->数据;
如果(l2)值+=l2->数据;
进位=值/10;
//将该值插入结果列表
Node*Node=新节点();
节点->数据=值%10;
如果(!头)
{
头部=节点;
}
其他的
{
节点->下一步=头部;
头部=节点;
}
//为最后一次迭代添加进位
如果((k==0)和&(进位!=0))
{
Node*Node=新节点();
节点->数据=进位;
节点->下一步=头部;
头部=节点;
}
回流头;
}
代码有点难理解。您可以简单地对(简单地)链接列表求和,而不是提取每个列表的值,添加它们,然后存储结果
下面是一个简单的例子。请注意,carry
必须作为引用传递,以便调用者在展开堆栈时可以访问修改后的值。此外,本例假设两个列表具有相同数量的节点。如果没有,您可以将0
添加到最短列表中
// basic node structure
template <typename T>
struct Node
{
T data = 0;
Node<T>* next = nullptr;
};
// function to sum 2 simply linked lists
Node<int>* sumLists(Node<int>* l1, Node<int>* l2, int& carry, int k = -1)
{
if (!l1 && !l2)
{
// we reached the end of both lists
return nullptr;
}
++k; // index of the largest list
// iterate to the end of both lists, head is the head of the result list
auto head = sumLists(l1 ? l1->next : l1, l2 ? l2->next : l2, carry, k);
// compute the value and carry
int value = carry;
if (l1) value += l1->data;
if (l2) value += l2->data;
carry = value / 10;
// insert the value into the result list
Node<int>* node = new Node<int>();
node->data = value % 10;
if (!head)
{
head = node;
}
else
{
node->next =head;
head = node;
}
// add carry for the last iteration
if ((k == 0) && (carry != 0))
{
Node<int>* node = new Node<int>();
node->data = carry;
node->next = head;
head = node;
}
return head;
}
//基本节点结构
模板
结构体类型
{
T数据=0;
Node*next=nullptr;
};
//函数对2个简单链表求和
节点*sumLists(节点*l1,节点*l2,整数和进位,整数k=-1)
{
如果(!l1&&!l2)
{
//我们到达了两个列表的末尾
返回空ptr;
}
++k、 //最大列表的索引
//迭代到两个列表的末尾,head是结果列表的head
自动头=sumLists(l1?l1->next:l1,l2?l2->next:l2,进位,k);
//计算值并进行进位
int值=进位;
如果(l1)值+=l1->数据;
如果(l2)值+=l2->数据;
进位=值/10;
//将该值插入结果列表
Node*Node=新节点();
节点->数据=值%10;
如果(!头)
{
头部=节点;
}
其他的
{
节点->下一步=头部;
头部=节点;
}
//为最后一次迭代添加进位
如果((k==0)和&(进位!=0))
{
Node*Node=新节点();
节点->数据=进位;
节点->下一步=头部;
头部=节点;
}
回流头;
}
这是C还是C++?看起来像C++——如果是这样,请相应地编辑你的标签。在这个例子中,你知道这两个数字的数字相同,但一般来说,你可能不会。此外,在本例中,结果的位数与输入的位数相同,但输出可能比输入多出一位数(例如,考虑添加499和501)。你必须能够回溯,以处理携带(在499+501的情况下,你必须回溯两次,并在结果的开头添加一个新元素;在328+654的情况下,你只需要回溯一次。我可能不会告诉你任何你不知道的事情,只是以防万一。对tonysdg说:对不起,我使用了结构并想到了C。对Jonathan说:不,我不知道ea有多少个数字。)ch链表有。这就是为什么我有变量size1和size2,分别计算它们的大小。是的,我检查了它。它也适用于不同大小的列表。不用担心-只是想确保您得到了适当的支持!:-)这是C++还是C++?如果是这样,请相应地编辑你的标签。在这个例子中,你知道两个数字的数字相同,但是一般来说,你可能不会。在这个例子中,结果和输入的数字相同,但是输出可以比输入多一个数字。(例如,考虑添加499和501)。您必须能够回溯以处理进位(在499+501的情况下,您必须回溯两次,并在结果的开头添加一个新元素;在328+654的情况下,您只需回溯一次。我可能不会告诉您我们需要的任何内容