LeetCode题目地址:两数相加
给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储一位数字。如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。您可以假设除了数字 0 之外,这两个数都不会以 0 开头。
示例:
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807
本文在该问题的基础上将题目进行了更改,首先不再限定两个整数的位数,其次考虑了整数相加是可能发生进位的情况。本程序一共有三个文件,包括一个头文件,一个函数定义文件以及一个测试文件。其中头文件源码如下:
//
// Node_Sum.hpp
// 两数相加
//
// Created by lq on 2019/9/4.
// Copyright © 2019 Mr.liang. All rights reserved.
//
#ifndef Node_Sum_hpp
#define Node_Sum_hpp
#include <iostream>
struct node
{
node* next;
node* last;
int data;
};
class node_sum
{
private:
node* head;
node* tail;
int length;
public:
node_sum();
node_sum(const node_sum & s);
~node_sum()
{
}
bool empty_check();
int size();
void remove_all();
void add_element(int data);
void show_list();
node_sum & addition(const node_sum & mirror1,const node_sum & mirror2);
int pointer_null(const node* mirror1, const node* mirror2);
};
#endif /* Node_Sum_hpp */
函数定义文件如下:
//
// Node_Sum.cpp
// 两数相加
//
// Created by lq on 2019/9/4.
// Copyright © 2019 Mr.liang. All rights reserved.
//
#include "Node_Sum.hpp"
#include <iostream>
#include <algorithm>
using namespace std;
node_sum::node_sum()//构造函数
{
head = new node;
tail = new node;
head->next = tail;
head->last = nullptr;
tail->next = nullptr;
tail->last = head;
length = 0;
}
node_sum::node_sum(const node_sum & s )//复制构造函数
{
head = new node;
tail = new node;
head->last = nullptr;
head->next = tail;
tail->last = head;
length = 0;
node* temp = s.head;
while(temp->next != tail)
{
temp = temp->next;
tail->data = temp->data;
tail->next = new node;
node* q = tail;
tail = tail->next;
tail->last = q;
length++;
}
tail->next = nullptr;
}
bool node_sum::empty_check()
{
if(length == 0)
{
return true;
}
else
{
return false;
}
}
int node_sum::size()
{
return length;
}
void node_sum::add_element(int data)
{
tail->data = data;
tail->next = new node;
node* mirror = tail;
tail = tail->next;
tail->last = mirror;
tail->next = nullptr;
length++;
}
void node_sum::remove_all()
{
if(length == 0)
{
cout<<"list is empty"<<endl;
}
else
{
node* temp = head->next;
while(temp != tail)
{
node* q = temp;
temp = temp->next;
delete q;
}
head->next = tail;
tail->last = head;
length = 0;
}
}
void node_sum::show_list()
{
if(length == 0)
{
cout<<"list is empty"<<endl;
}
node* temp = head->next;
while(temp != tail)
{
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
}
node_sum & node_sum::addition(const node_sum & mirror1, const node_sum & mirror2)
{
node* temp1 = mirror1.head;
node* temp2 = mirror2.head;
int total = 0,total1 = 0,total2 = 0;
bool point = false;
int i = 0;
if(mirror1.length == mirror2.length)
{
for(i = 0;i<mirror1.length;i++)
{
temp1 = temp1->next;
temp2 = temp2->next;
total1 = temp1->data;
total2 = temp2->data;
total = total1 + total2 +point;
if(total<10)
{
add_element(total);
point = false;
}
else
{
add_element(total-10);
point = true;
}
}
if((i == mirror1.length)&&(point == true))
{
add_element(true);
}
}
else
{
int i = 0;
for(i = 0;i<max(mirror1.length,mirror2.length);i++)
{
if(i<min(mirror1.length,mirror2.length))
{
temp1 = temp1->next;
temp2 = temp2->next;
total1 = temp1->data;
total2 = temp2->data;
total = total1 + total2 + point;
}
else
{
pointer_null(temp1,temp2);
temp1 = temp1->next;
temp2 = temp2->next;
(pointer_null(temp1,temp2) == 1)?total = point + temp2->data:total = point + temp1->data;
}
if(total<10)
{
add_element(total);
point = false;
}
else
{
add_element(total-10);
point = true;
}
}
if((i == max(mirror1.length,mirror2.length))&&(point == true))
{
add_element(true);
}
}
return *this;
}
int node_sum::pointer_null(const node* mirror1, const node* mirror2)
{
if(mirror1->next == nullptr)
return 1;
else if(mirror2->next == nullptr)
{
return 2;
}
else
return 0;
}
测试文件如下:
//
// main.cpp
// 两数相加
//
// Created by lq on 2019/9/4.
// Copyright © 2019 Mr.liang. All rights reserved.
//
#include <iostream>
#include "Node_Sum.hpp"
using namespace std;
int main(int argc, const char * argv[]) {
// insert code here...
node_sum _mirror1;
node_sum _mirror2;
node_sum totalnodes;
node_sum * sum = &totalnodes;
_mirror1.add_element(9);
_mirror1.add_element(9);
_mirror1.add_element(9);
_mirror1.add_element(9);
_mirror2.add_element(9);
_mirror2.add_element(9);
_mirror2.add_element(9);
//_mirror1.show_list();
sum = &sum->addition(_mirror1,_mirror2);
sum->show_list();
return 0;
}
运行结果如下:
8 9 9 0 1
Program ended with exit code: 0