(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.

Expert Answer

answers

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

CAChoggC Cheggfactorial linllist.cc ter no. for factorial to celculated 123 Pro -in rINAL LIST įs-- 1 21463 43670253 29675766243241 831 295855454 21 7a884 83 38231 5 3289131618292358923621 67668831 1 5696 61 26402e21 7a735835 221 2940477 25918915794116514721860295199626164673733997419314952 rocess exited after 5.74 seconds with return value θ ress any key to continue 243 L 244 245 246 㗊Compiler Resources dl compile Log V, Debug鳳Find Results相Close Comp11ation results -Errors: 0 Warnings: o Output Filename DCheggC++ Chegg factorial_linllist.exe - Output Size: 131.8740234375 KEB -Comp1lation Time: 0.303 ine 223 Col 12 Lines: 246 Length: 3779 Insert Done parsing in 0.015 seconds C1:11 AM O Type here to search IND 03-11-2018

iWriteHomework
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.

Professional and Experienced Academic Writers

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.

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!

Calculate the price of your order

Total price:
$0.00

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

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.

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.

× Contact Live Agents