Multiplication Of Two Numbers With Help Of Linked List

Multiplication Of Two Numbers With Help Of Linked List

 
 
 
It's A Multiplication Program Using Linked List That Means It Can Multiply Numbers That Are Bigger Than Range Of int , long int etc.. .............


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct ll
{
int digit;
struct ll *next;
}number;
void add(number **hd,int i);
void traverse(number *hd);
void reverse(number **hd);
void adds(char a[],number **hd);
void addhead(number *hd1,number *hd);
void mul(number *hd1,number **hd2);
void multiply(number *hd1,int attempt,int x);
void copy(number *hd);
void freeh(number **hd);
number *oh=NULL,*head3=NULL,*head4=NULL;
void main()
{
number *head1=NULL,*head2=NULL;
char a[500],b[500];
clrscr();
gets(a);
gets(b);
adds(a,&head1);
adds(b,&head2);
printf("___________");
printf("\nAnswer Is :|");
printf("\n-----------");
mul(head1,&head2);
freeh(&head1);
freeh(&head2);
freeh(&oh);
freeh(&head4);
traverse(head3);
freeh(&head3);
getch();
}
void copy(number *hd)
{
if(hd==NULL)
return;
while(hd!=NULL)
{
add(&head3,hd->digit);
hd=hd->next;
}
reverse(&head3);
}
///////////////////////////////////////////////////
void mul(number *hd1,number **hd2)
{
int attempt=0,x;number *temp;
while(*hd2!=NULL)
{
temp=*hd2;
x=(*hd2)->digit;
multiply(hd1,attempt,x);
attempt++;
*hd2=(*hd2)->next;
free(temp);
}
}
void multiply(number *hd1,int attempt,int x)
{
int a,sum=0,carry=0;
while(hd1!=NULL)
{
a=hd1->digit*x+carry;
sum=a%10;
carry=a/10;
add(&oh,sum);
hd1=hd1->next;
}
if(carry>0)
add(&oh,carry);    //till here no change
if(attempt==0)
{
copy(oh);
freeh(&oh);
return;
}
  //reverse should be of head4
reverse(&oh);
while(attempt)
{
add(&oh,0);
attempt--;
}
reverse(&head3);
addhead(oh,head3);  //addition of head4 and oh should be done
freeh(&head3);
copy(head4);
freeh(&head4);
freeh(&oh);
}
void freeh(number **hd)
{
number *temp;
while(*hd!=NULL)
{
temp=*hd;
*hd=(*hd)->next;
free(temp);
}
*hd=NULL;
}
void add(number **hd,int i)
{
number *t;
t=(number*)malloc(sizeof(number));
t->next=NULL;
t->digit=i;
if(*hd==NULL)
{
*hd=t;
return;
}
t->next=*hd;
*hd=t;
}
///////////////////////////////////////////////////
void traverse(number *hd)
{
printf("\n");
while(hd!=NULL)
{
printf("%d",hd->digit);
hd=hd->next;
}
}
//////////////////////////////////////////////////
void reverse(number **hd)
{
number *p,*q,*r;
if(*hd==NULL)
return;
p=*hd;
if(p->next==NULL)
return;
q=p->next;
if(q->next==NULL)
{
q->next=p;
p->next=NULL;
*hd=q;
return;
}
r=q->next;
while(r->next!=NULL)
{
q->next=p;
p=q;
q=r;
r=r->next;
}
q->next=p;
r->next=q;
(*hd)->next=NULL;
*hd=r;
}
void addhead(number *hd1,number *hd2)
{
int x,carry=0,sum=0;
while(hd1!=NULL&&hd2!=NULL)
{
x=(hd1)->digit+(hd2)->digit + carry;
sum=x % 10;
carry=x / 10;
add(&head4,sum);
hd1=(hd1)->next;
hd2=(hd2)->next;
}
if(hd1==NULL&&hd2!=NULL)
{
while(hd2!=NULL)
{
x=(hd2)->digit + carry;
sum=x % 10;
carry=x / 10;
add(&head4,sum);
hd2=(hd2)->next;
}
}
else if(hd1!=NULL&&hd2==NULL)
{
while(hd1!=NULL)
{
x=(hd1)->digit + carry;
sum=x % 10;
carry=x / 10;
add(&head4,sum);
hd1=(hd1)->next;
}
}
if(carry>0)
add(&head4,carry);
//reverse(hd3);
}
void adds(char a[],number **hd)
{
int i=0;
while(a[i]!= '\0')
{
add(hd,a[i]- '0');
i++;
}
}
 
 
___________________________________________________________________________________

                                                          !!! HOPE U LIKE IT !!!
                                                  !!!  THANKS FOR READING !!!
                                                  !!!!!HAPPY PROGRAMMING !!!!!

U Can catch me at http://www.facebook.com/salokr

 

 

Comments

Popular posts from this blog

QUEUE IMPLEMENTATION USING LINKED LIST

DATA STRUCTURES INTERVIEW QUESTIONS

Program To Evaluate Square Root Of A Number