链表实现大数加法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/A_bigUncle/article/details/72353286

链表实现大数加法

标签: 算法


Description

你的任务是完成一条能实现加法功能的单向链表,需要实现的函数在头文件已给出。
假如现在有 123 与 234 两个数字,那么他们在链表中的存储结构将会是 : 3->2->1与 4->3->2
注意到这里的链表只允许从前端插入,你也可以把这个特殊的链表当做栈来处理。
输入的数字不会有负数且不会有前导0的输入。也就是说,输入的数字中不会出现0,更不会有0001这样的输入

代码

//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] = '\0';
  while (current != 0) {
    num[i] = current->val + 48;
    i--;
    current = current->next;
  }
  os << num;
  delete []num;
  return os;
}

猜你喜欢

转载自blog.csdn.net/A_bigUncle/article/details/72353286