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.
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 */
void add(nodeptr list1,nodeptr list2,nodeptr list3)
{
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++;
}
add(good,good1,good2);
}
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
Why Work with Us
Top Quality and Well-Researched Papers
We always make sure that writers follow all your instructions precisely. You can choose your academic level: high school, college/university or professional, and we will assign a writer who has a respective degree.
Professional and Experienced Academic Writers
We have a team of professional writers with experience in academic and business writing. Many are native speakers and able to perform any task for which you need help.
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 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.
24/7 Customer Support
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!
How it works?
Follow these simple steps to get your paper done
Place your order
Fill in the order form and provide all details of your assignment.
Proceed with the payment
Choose the payment system that suits you most.
Receive the final file
Once your paper is ready, we will email it to you.
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.
Essays
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.
Admissions
Admission Essays & Business Writing Help
An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.
Reviews
Editing Support
Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.
Reviews
Revision Support
If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.