struct node

Document Sample

```					Make a structure for a Node with pointer to left link, pointer to right link and one
variable of type long to store data value.

struct node
{
long int data;
node()
{
data=0;
}
};

{
int noofdigit;
char sign;
struct node* left;
struct node* right;
{
noofdigit=0;
sign=0;
left=NULL;
right=NULL;
}
};
Abstract Functions used in Add() and Multiply():

{
}

{
}

{
}

struct node* NextLeft(struct node* nodetosearch)
{
}

{
}

else {
}
}
{
}

else {
}
}

int over_flow(struct node* nodetochk)
{
long int ovrflwdgt = 0;
ovrflwdgt = nodetochk->data / 10000 ;
if(ovrflwdgt != 0){
nodetochk->data = nodetochk->data - (ovrflwdgt * 10000);
}
return ovrflwdgt;
}

{
struct node* n1, *n2, *rslt;
long int sum, ovrflwdgt=0;
if(Sign(oprnd1)==Sign(oprnd2))
{
n1 = FirstRight(oprnd1);
n2 = FirstRight(oprnd2);
while(n1!=NULL && n2!=NULL)
{
sum = n1->data + n2->data + ovrflwdgt;
rslt = new struct node;
rslt->data = sum;
InsertLeft(result, rslt);
ovrflwdgt = over_flow(rslt);
n1 = NextLeft(n1);
n2 = NextLeft(n2);
}
while(n1 != NULL)
{
sum = n1->data + ovrflwdgt;
rslt = new struct node;
rslt->data = sum;
InsertLeft(result, rslt);
ovrflwdgt = over_flow(rslt);
n1 = NextLeft(n1);
}
while(n2 != NULL)
{
sum = n2->data + ovrflwdgt;
rslt = new struct node;
rslt->data = sum;
InsertLeft(result, rslt);
ovrflwdgt = over_flow(rslt);
n2 = NextLeft(n2);
}
if(ovrflwdgt>0)
{
rslt = new struct node;
rslt->data = ovrflwdgt;
InsertLeft(result, rslt);
}
result->sign = Sign(oprnd1);
SetNoofDigits(result);
}
}
C++ Implementation for Multiplication function:

/* This is not the complete multiplication function */

{
struct node* mn1, *mn2, *rsltmlt,*overflownd;
long int mul_rs, ovrflwdgt=0;
mn1 = FirstRight(oprnd1);
mn2 = FirstRight(oprnd2);
while(mn2 != NULL)
{
while(mn1!=NULL )
{
mul_rs = (mn1->data) * (mn2->data) ;
rsltmlt = new struct node;
rsltmlt->data = mul_rs;
InsertLeft(result, rsltmlt);
ovrflwdgt = over_flow(rsltmlt);
if(ovrflwdgt != 0)
{
overflownd = new struct node;
overflownd->data = ovrflwdgt;
InsertLeft(result, overflownd);
}
/* Implement the code for addition of the
results and also for adding the shifting nodes */

mn1 = NextLeft(mn1);
}
mn2 = NextLeft(mn2);
mn1 = FirstRight(oprnd1);
}
result->sign = Sign(oprnd1);
SetNoofDigits(result);
}

```
DOCUMENT INFO
Shared By:
Categories:
Stats:
 views: 44 posted: 2/18/2010 language: English pages: 5
How are you planning on using Docstoc?