# (Homework Solution): Question: How to calculate large factorials using linked list (not array) in C language? I can only find C+…

How to calculate large factorials using linked list (not array) in C language? I can only find C++ version of it , there’s only array version for C on the internet.

C code

=============================================================================

#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node *next;
struct node *prev;
};
typedef struct node *nodeptr;
nodeptr good=NULL,good1,good2,result=NULL;
void insert_start(nodeptr *p,int x) //insert node at start
{
nodeptr q;
q=(nodeptr)malloc(sizeof(struct node));
//q=new node();
if(q==NULL)
{
printf(“memory not allocated”);
getch();
//exit(1);
}
q->data=x;
q->next=q->prev=*p;
*p=q;
}
void insert_after(nodeptr p,int x) //insert node after p
{
nodeptr q;
while(p->next!=NULL)
{
p=p->next;
}
q=(nodeptr)malloc(sizeof(struct node));
if(q==NULL)
{
printf(“memory not allocated!!!!! “);
getch();
//exit(1);
}
q->data=x;
q->next=p->next;
q->prev=p;
p->next=q;
}
void free_list(nodeptr *p) //free the whole list
{
nodeptr q;
while((*p)!=NULL)
{
q=*p;
(*p)=(*p)->next;
free(q);
}
*p=NULL;
}
void printr_nodes(nodeptr p) //print data in list
{
nodeptr q;
q=p;
printf(“n”);
if(q==NULL)
printf(“List is empty”);
while(q->next!=NULL)
q=q->next;
while(q!=NULL)
{
printf(“%d”,q->data);
q=q->prev;
}
}
/* copy list copy1 into list copy2 */
void copy5(nodeptr copy1,nodeptr *copy2)
{
int a;
while(copy1!=NULL)
{
a=copy1->data;
if(*copy2==NULL)
insert_start(&(*copy2),a);
else
insert_after((*copy2),a);
copy1=copy1->next;
}
}
/* add 3lists coming from multiply function */
{
int value,var,var1,var2,var3,temp=0;
while(list3!=NULL||list1!=NULL||list2!=NULL)
{
if(list1==NULL)
var1=0;
else
{
var1=list1->data;
list1=list1->next;
}
if(list2==NULL)
var2=0;
else
{
var2=list2->data;
list2=list2->next;
}
if(list3==NULL)
var3=0;
else
{
var3=list3->data;
list3=list3->next;
}
var=var1+var2+var3+temp;
if(var>9)
{
value=var%10;
temp=var/10;
}
else
{
value=var;
temp=0;
}

if(result==NULL)
{
insert_start(&result,value);
}
else
insert_after(result,value);

}
if(temp!=0)
insert_after(result,temp);
}
/* multiply the two lists */
void multiply(nodeptr mul1,nodeptr mul2)
{
int a,b,value,d,temp=0,place=0;
nodeptr mult1;
insert_start(&good1,0);
insert_start(&good2,0);
insert_after(good2,0);
while(mul2!=NULL)
{
mult1=mul1;
temp=0;
a=mul2->data;
//printf(“a is %dn”,a);
while(mult1!=NULL)
{
b=(mult1->data)*a;
b=temp+b;
if(b>9)
{
value=b%10;
temp=b/10;
}
else
{
value=b;
temp=0;
}
//printf(“value of VALUE is %dn”,value);
if(place==0)
{
if(good==NULL)
insert_start(&good,value);
else
insert_after(good ,value);
}
else if(place==1)
{
insert_after(good1,value);
}
else if(place==2)
{
insert_after(good2,value);
}
mult1=mult1->next; //point to next node
}
if(place==0 &&temp!=0)
insert_after(good,temp);
else if(place==1 &&temp!=0)
insert_after(good1,temp);
else if(place==2 &&temp!=0)
insert_after(good2,temp);
mul2=mul2->next;
place++;
}
}

void main()
{
nodeptr list=NULL,list1=NULL;
int i ,val,val1,num,div;

printf(“enter no. for factorial to calculatedn”);
scanf(“%d”,&num);
val1=num;
while(num>0)
{
div=num%10;
num=num/10;
if(list==NULL)
insert_start(&list,div);
else
insert_after(list ,div);
}
//printf(“in 1st operand”);
//printr_nodes(list);

/* loop upto n-1 to 1*/
for(i=val1-1;i>1;i–)
{
val=i;
while(val>0)
{
div=val%10;
val=val/10;
if(list1==NULL)
insert_start(&list1,div);
else
insert_after(list1,div);
}
//printf(“nin 2nd operand”);
//printr_nodes(list1);
multiply(list,list1);
free_list(&list1);
free_list(&list);
copy5(result,&list);
free_list(&result);
free_list(&good);
free_list(&good1);
free_list(&good2);
}
printf(“n———-in FINAL LIST is————n”);
printr_nodes(list);

}

============================================================================

Output

Order NOW for a 10% Discount
Pages (550 words)
Approximate price: -

Why Us?

Top Quality and Well-Researched Papers

All ourbpapers are written from scratch. In fact, Clients who ask for paraphrasing services are highly discouraged. We have writers ready to craft any paper from scratch and deliver quality ahead of time.

Our writers keeps you posted on your papers progress - providing you with paper outline/draft. You are also at liberty to communicate directly with your writer.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time, in many cases quite ahead of time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

## Calculate the price of your order

Total price:
\$0.00

How it works?

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.

## Essay Writing Service

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.