链表实现大数加法
发布时间:2020-12-14 03:11:44 所属栏目:大数据 来源:网络整理
导读:链表实现大数加法 标签: 算法 Description 你的任务是完成一条能实现加法功能的单向链表,需要实现的函数在头文件已给出。 假如现在有 123 与 234 两个数字,那么他们在链表中的存储结构将会是 : 3-2-1与 4-3-2 注意到这里的链表只允许从前端插入,你也可
链表实现大数加法标签: 算法 Description你的任务是完成一条能实现加法功能的单向链表,需要实现的函数在头文件已给出。 代码//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
//ListAdd.cpp
#include "ListAdd.hpp"
List::List() {
head = 0;
_size = 0;
}
List::List(const List& other) {
int* num = new int[other._size];
node* current = other.head;
int i = other._size - 1;
while (current != 0) {
num[i] = current->val;
i--;
current = current->next;
}
head = 0;
_size = 0;
for (int i = 0; i < other._size; i++)
push_front(num[i]);
delete []num;
}
List::List(const string& num) {
head = NULL;
_size = 0;
for (int i = 0; i < num.size(); i++)
push_front(num[i] - 48);
}
void List::clear() {
while (head != 0) {
node* temp = head;
head = head->next;
delete temp;
}
_size = 0;
}
void List::push_front(int val) {
node* temp = new node(val,head);
head = temp;
_size++;
}
List List::operator+(const List& other) {
int max = _size > other._size ? _size : other._size;
char* addNum = new char[max + 1];
int carry = 0;
List result;
if (_size > other._size) {
node* c1 = head,*c2 = other.head;
int i = 0;
while (c2 != 0) {
addNum[i++] = (c1->val + c2->val + carry) % 10 + 48;
carry = (c1->val + c2->val + carry) / 10;
c1 = c1->next;
c2 = c2->next;
}
while (c1 != 0) {
addNum[i++] = (c1->val + carry) % 10 + 48;
carry = (c1->val + carry) / 10;
c1 = c1->next;
}
addNum[i] = carry + 48;
} else {
node* c1 = other.head,*c2 = head;
int i = 0;
while (c2 != 0) {
addNum[i++] = (c1->val + c2->val + carry) % 10 + 48;
carry = (c1->val + c2->val + carry) / 10;
c1 = c1->next;
c2 = c2->next;
}
while (c1 != 0) {
addNum[i++] = (c1->val + carry) % 10 + 48;
carry = (c1->val + carry) / 10;
c1 = c1->next;
}
addNum[i] = carry + 48;
}
if (addNum[max] == '1') {
for (int i = max; i >= 0; i--)
result.push_front(addNum[i] - 48);
} else {
for (int i = max - 1; i >= 0; i--)
result.push_front(addNum[i] - 48);
}
delete []addNum;
return result;
}
List& List::operator=(const List& other) {
node* temp = head;
int* num = new int[other._size];
node* current = other.head;
int i = other._size - 1;
while (current != 0) {
num[i] = current->val;
i--;
current = current->next;
}
head = NULL;
_size = 0;
for (int i = 0; i < other._size; i++)
push_front(num[i]);
while (temp != 0) {
node* t = temp;
temp = temp->next;
delete t;
}
delete []num;
return *this;
}
int List::size() const { return _size; }
List::~List() {
clear();
}
ostream& operator<<(ostream& os,const List& out) {
char* num = new char[out._size + 1];
List::node* current = out.head;
int i = out._size - 1;
num[out._size] = ' ';
while (current != 0) {
num[i] = current->val + 48;
i--;
current = current->next;
}
os << num;
delete []num;
return os;
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |