Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How to write these two sections for the following C code, I need to write the analysis and comments etc? The C code is to

How to write these two sections for the following C code, I need to write the analysis and comments etc? The C code is to calculate large factorials using doubly linked list.

1. Algorithms Specification Describe the algorithms used to solve the problem, including specifications of main data structures. 2. Analysis and Comments Analyze the time and space complexities of the algorithms. Discuss the results and give comments on further possible improvements.

Below is the source code of C language.

#include #include 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(" "); 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 %d ",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 %d ",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 calculated "); 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(" in 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(" ----------in FINAL LIST is------------ "); printr_nodes(list); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

3. How would you address the problems that make up the situation?

Answered: 1 week ago

Question

2. What recommendations will you make to the city council?

Answered: 1 week ago