1. 程式人生 > >11.用連結串列模擬大整數加法運算

11.用連結串列模擬大整數加法運算

例如:9->9->9->NULL

+                      1->NULL

      1->0->0->0->NULL

思路:

使用遞迴,能夠實現從前往後計算。

// LinkTable.cpp : 定義控制檯應用程式的入口點。
//

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

//連結串列的結構體
struct node
{
	char val;
	node * next;
};

//建立連結串列
struct node * create( string & str_link )
{
	int len = str_link.length();

	struct node * phead = new node();     //帶有表頭的連結串列,表頭中不儲存任何元素
	struct node * preNode = phead;
	for( int i=0; i<len; i++ )
	{
		struct node * pNode = new node();
		pNode->val = str_link[i];
		pNode->next = NULL;
		preNode->next = pNode;
		preNode = pNode;
	}
	return phead;
}

//輸出連結串列
void out_link( struct node * phead )
{
	if( phead == NULL )
		return;
	struct node * pNode = phead->next;
	while( pNode )
	{
		cout <<pNode->val;
		pNode = pNode->next;
	}
	cout << endl;
}

//求無表頭連結串列的長度
//返回-1為連結串列不存在
int link_length( struct node* pNode )
{
	if(!pNode) return -1;

	int len=0;
	while( pNode )
	{
		pNode = pNode->next;
		len++;
	}
	return len;
}


//大數相加遞迴演算法
//pNode1, pNode2為兩個中間運算結點,但不是頭結點
struct node * add( struct node * pNode1, struct node * pNode2, int & carry )
{
	if( !pNode1  ) return pNode2;
	if( !pNode2  ) return pNode1;

//為了引數簡潔,這裡增大了計算量,可以將連結串列長度作為引數傳進來
	int len1 = link_length( pNode1 );
	int len2 = link_length( pNode2 );


	if( len1 == len2 )
	{
		if( len1==1 )             //遞迴終止條件
		{
			struct node * pNode = new node();
			int sum = (pNode1->val - '0' ) + ( pNode2->val -'0');
			carry = sum/10;
			pNode->val = sum%10 + '0';
			pNode->next = NULL;
			return pNode;
		}
		else
		{
			int carry_cur=0;
			struct node * pNode = new node();
			struct node * pNext = add( pNode1->next, pNode2->next, carry_cur );
			int sum = (pNode1->val - '0' ) + ( pNode2->val -'0') + carry_cur;
			carry = sum/10;
			pNode->val = sum%10 + '0';
			pNode->next = pNext;
			return pNode;
		}
	}

	if( len1>len2 )
	{
		int carry_cur=0;
			struct node * pNode = new node();
			struct node * pNext = add( pNode1->next, pNode2, carry_cur );
			int sum = (pNode1->val - '0' ) + carry_cur;
			carry = sum/10;
			pNode->val = sum%10 + '0';
			pNode->next = pNext;
			return pNode;
	}

	if( len1<len2 )
	{
		int carry_cur=0;
			struct node * pNode = new node();
			struct node * pNext = add( pNode1, pNode2->next, carry_cur );
			int sum = (pNode2->val - '0' ) + carry_cur;
			carry = sum/10;
			pNode->val = sum%10 + '0';
			pNode->next = pNext;
			return pNode;
	}

	return NULL;
}

struct node * add( struct node * phead1, struct node * phead2 )
{
	if( !phead1 || !phead1->next ) return phead2;
	if( !phead2 || !phead2->next ) return phead1;

	int carry = 0;
	struct node * pNode = add( phead1->next, phead2->next, carry );

	if( carry > 0 )                     //有進位,則需要多一個結點
	{
		struct node * pCarry = new node();
		pCarry->val = '0' + carry;
		pCarry->next = pNode;
		pNode = pCarry;
	}

	struct node * phead = new node();
	phead->next = pNode;
	return phead;
}

void test()
{
	string str;
	cout << "Input the first link:"<<endl;
	cin >> str;
	struct node *phead1 = create( str );
	
	cout << "Input the second link:"<<endl;
	cin >> str;
	struct node *phead2 = create( str );

	struct node * phead = add( phead1, phead2);
	
	cout<< "The result is:" <<endl;
	out_link( phead );

}

int _tmain(int argc, _TCHAR* argv[])
{
	test();
	return 0;
}