2610 List Add
来源:互联网 发布:农村淘宝下载安装 编辑:程序博客网 时间:2024/06/06 23:55
你的任务是完成一条能实现加法功能的单向链表,需要实现的函数在头文件已给出。
假如现在有 123 与 234 两个数字,那么他们在链表中的存储结构将会是 : 3->2->1与 4->3->2
注意到这里的链表只允许从前端插入,你也可以把这个特殊的链表当做栈来处理。
输入的数字不会有负数且不会有前导0的输入。也就是说,输入的数字中不会出现0,更不会有0001这样的输入
Sample input
123123
Sample output
Input two NumbersAfter 123 + 123246After 246 + 123369
Provided Codes
main.cpp
#include "ListAdd.hpp"using std::cout;using std::endl;using std::cin;int main() { string s1, s2; // 0 < s1.length(),s2.length() <= 10000 cout << "Input two Numbers" << endl; cin >> s1 >> s2; List l1(s1), l2(s2); List l3 = l1 + l2; cout << "After " << l1 << " + " << l2 <<endl; cout << l3 << endl << "Size : " << l3.size() << endl; List l4; l4 = l3 + l1; cout << "After " << l3 << " + " << l1 << endl; cout << l4 << endl << "Size : " << l4.size() << endl;}
ListAdd.hpp
#ifndef _LIST_ADD_#define _LIST_ADD_#include <iostream>using std::string;using std::ostream;class List { private: struct node { int val; node* next; node(int v, node* n = 0):val(v), next(n){} }; node* head; int _size; public: List(); List(const List& other); List(const string & num); void clear(); void push_front(int val); // 在头部插入数值 List operator+(const List& other); //在这里进行链表加法实现 List& operator=(const List& other); // 赋值操作重载 int size() const; ~List(); friend ostream& operator<<(ostream & os, const List & out); // 输出数字,无需换行};#endif
Submission
ListAdd.cpp
#include "ListAdd.hpp"#include <iostream>#include <sstream>#include <string>using namespace std;List::List(){ head=NULL; _size=0;}List::List(const List& other){ head=NULL; _size=0; node* tem=other.head; while(tem!=NULL){ push_front(tem->val); tem=tem->next; }}List::List(const string & num){ head=NULL; _size=0; int length=num.length(); for(int i=0;i<length;i++){ stringstream in; in<<num[i]; int get; in>>get; push_front(get); }}void List::clear(){ node* tem=head; while(tem!=NULL){ node* next=tem->next; delete tem; tem=next; } head=NULL; _size=0;}void List::push_front(int val){ node* newHead=new node(val,head); head=newHead; ++_size;}List List::operator+(const List& other){ List tem; node* mytem=head; node* othertem=other.head; node* temtem=tem.head; int U=0; while(mytem!=NULL&&othertem!=NULL){ int up=0; int val=mytem->val+othertem->val+U; if(val>=10){ up=1; val-=10; } //push_back if(temtem==NULL) temtem=tem.head=new node(val); else{ temtem->next=new node(val); temtem=temtem->next; } ++tem._size; U=up; mytem=mytem->next; othertem=othertem->next; } while(mytem!=NULL){ int up=0; int val=mytem->val+U; if(val>=10){ up=1; val-=10; } //push_back if(temtem==NULL) temtem=tem.head=new node(val); else{ temtem->next=new node(val); temtem=temtem->next; } U=up; ++tem._size; mytem=mytem->next; } while(othertem!=NULL){ int up=0; int val=othertem->val+U; if(val>=10){ up=1; val-=10; } //push_back if(temtem==NULL) temtem=tem.head=new node(val); else{ temtem->next=new node(val); temtem=temtem->next; } U=up; ++tem._size; othertem=othertem->next; } if(U){ temtem->next=new node(U); ++tem._size; } return tem;}List& List::operator=(const List& other){ clear(); node* tem=other.head; node* temtem=head; while(tem!=NULL){ //push_back if(temtem==NULL) temtem=head=new node(tem->val); else{ temtem->next=new node(tem->val); temtem=temtem->next; } ++_size; tem=tem->next; } return *this;}int List::size() const{ return _size;}List::~List(){ clear();}ostream& operator<<(ostream & os, const List & me){ int out[me._size]; int i=0; List::node* tem=me.head; while(tem!=NULL){ out[i++]=tem->val; tem=tem->next; } for(int i=me._size-1;i>=0;i--) os<<out[i]; return os;}
阅读全文
0 0
- 2610 List Add
- list.add addAll
- list add test
- list add方法
- java list add addall
- Java-List.add
- List set 与 add
- Add to List 67
- ubuntu add sourse.list
- List add方法覆盖
- Add to List 67. Add Binary
- list.add(user)的问题
- TortoiseSVN add to ignore list
- add source.list in ubuntu
- List add Map的用法
- C# List<>.Add一个细节
- Leetcode - List - Add Two Numbers
- Add two numbers-list,string
- 大数据:PHP职位统计(20170530)
- 【Caffe的C++接口使用说明二)】caffe_windows下的C++接口的使用
- Opencup 010352 Problem 5. Autocomplete 哈希+bitset
- Zend Studio 0x80070666错误解决
- 动态打点
- 2610 List Add
- 类加载器
- Android 颜色使用总结
- SDUT-树结构练习——排序二叉树的中序遍历
- Codeforces Round #191 (Div. 2) C. Magic Five
- Spring中通过FactoryBean来配置bean
- 二叉树递归与非递归遍历
- 用python在windows下备份
- Axure学习及实战整理--之组件