Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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 %d\n",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\n",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\n");
- 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);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement