你的任务是完成一条能实现加法功能的单向链表,需要实现的函数在头文件已给出。
假如现在有 123 与 234 两个数字,那么他们在链表中的存储结构将会是 : 3->2->1与 4->3->2
注意到这里的链表只允许从前端插入,你也可以把这个特殊的链表当做栈来处理。
输入的数字不会有负数且不会有前导0的输入。也就是说,输入的数字中不会出现0,更不会有0001这样的输入
123
123
Sample output
Input two Numbers
After 123 + 123
246
After 246 + 123
369
Provided Codes
main.cpp
#include "ListAdd.hpp"
using std::cout;
using std::endl;
using std::cin;
int main() {
string s1, s2;
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
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;
}
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;
}
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;
}
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){
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;
}