将由链表表示的两个数字相加
这里我们将看到如何将存储在单独链表中的两个数字相加。在链表中,存储了数字的每一位数字。如果数字是 512,那么它将像下面一样存储 -
512 = (5)-->(1)-->(2)-->NULL
我们提供了两个这种类型的列表,我们的任务是将它们相加并在计算总和后得到结果。这里我们使用C++ STL链表。让我们看看算法来下注更好的想法。
算法
addListNumbers(l1, l2)
Begin Adjust the l1 and l2 lengths by adding leading 0s with the smaller one carry := 0 res := an empty list for each node n from l1, scan from last to first, do item := (l1.item + l2.item + carry) mod 10 insert item at the beginning of res carry := (l1.item + l2.item + carry) / 10 done if carry is not 0, then add carry at the beginning of res end if return res End
示例
#include<iostream> #include<list> using namespace std; list addListNumbers(list<int> l1, list<int> l2){ //add leading 0s to the shortest number to make them equal length if(l1.size() > l2.size()){ for(int i = l2.size(); i != l1.size(); i++){ l2.push_front(0); } }else if(l1.size() < l2.size()){ for(int i = l1.size(); i != l2.size(); i++){ l1.push_front(0); } } list<int>::reverse_iterator it1 = l1.rbegin(); list<int>::reverse_iterator it2 = l2.rbegin(); list<int> result; int carry = 0; while(it1 != l1.rend()){ result.push_front((*it1 + *it2 + carry) % 10); carry = (*it1 + *it2 + carry) / 10; it1++; it2++; } if(carry != 0){ result.push_front(carry); } return result; } list<int> numToList(int n){ list<int> numList; while(n != 0){ numList.push_front(n % 10); n /= 10; } return numList; } void displayListNum(list<int> numList){ for(list<int>::iterator it = numList.begin(); it != numList.end(); it++){ cout<<*it; } cout << endl; } int main() { int n1 = 512; int n2 = 14578; list<int> n1_list = numToList(n1); list<int> n2_list = numToList(n2); list<int> res = addListNumbers(n1_list, n2_list); cout << "First number: "; displayListNum(n1_list); cout << "Second number: "; displayListNum(n2_list); cout << "Result: "; displayListNum(res); }
输出
First number: 512 Second number: 14578 Result: 15090
以上是将由链表表示的两个数字相加的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

C语言数据结构:树和图的数据表示与操作树是一个层次结构的数据结构由节点组成,每个节点包含一个数据元素和指向其子节点的指针二叉树是一种特殊类型的树,其中每个节点最多有两个子节点数据表示structTreeNode{intdata;structTreeNode*left;structTreeNode*right;};操作创建树遍历树(先序、中序、后序)搜索树插入节点删除节点图是一个集合的数据结构,其中的元素是顶点,它们通过边连接在一起边可以是带权或无权的数据表示邻

文件操作难题的真相:文件打开失败:权限不足、路径错误、文件被占用。数据写入失败:缓冲区已满、文件不可写、磁盘空间不足。其他常见问题:文件遍历缓慢、文本文件编码不正确、二进制文件读取错误。

文章讨论了在C中有效使用RVALUE参考,以进行移动语义,完美的转发和资源管理,重点介绍最佳实践和性能改进。(159个字符)

C 20范围通过表现力,合成性和效率增强数据操作。它们简化了复杂的转换并集成到现有代码库中,以提高性能和可维护性。

本文讨论了使用C中的移动语义来通过避免不必要的复制来提高性能。它涵盖了使用std :: Move的实施移动构造函数和任务运算符,并确定了关键方案和陷阱以有效

本文讨论了C中的动态调度,其性能成本和优化策略。它突出了动态调度会影响性能并将其与静态调度进行比较的场景,强调性能和之间的权衡

C语言函数是代码模块化和程序搭建的基础。它们由声明(函数头)和定义(函数体)组成。C语言默认使用值传递参数,但也可使用地址传递修改外部变量。函数可以有返回值或无返回值,返回值类型必须与声明一致。函数命名应清晰易懂,使用驼峰或下划线命名法。遵循单一职责原则,保持函数简洁性,以提高可维护性和可读性。

本文讨论了编程中的自动类型扣除额,详细介绍了其益处,例如降低代码的冗长和提高的可维护性以及其局限性,例如潜在的混乱和调试挑战。
