Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct node
  5. {
  6. int data;
  7. struct node *next;
  8. struct node *prev;
  9. };
  10. typedef struct node *nodeptr;
  11. nodeptr good=NULL,good1,good2,result=NULL;
  12. void insert_start(nodeptr *p,int x) //insert node at start
  13. {
  14. nodeptr q;
  15. q=(nodeptr)malloc(sizeof(struct node));
  16. //q=new node();
  17. if(q==NULL)
  18. {
  19. printf("memory not allocated");
  20. getch();
  21. //exit(1);
  22. }
  23. q->data=x;
  24. q->next=q->prev=*p;
  25. *p=q;
  26. }
  27. void insert_after(nodeptr p,int x) //insert node after p
  28. {
  29. nodeptr q;
  30. while(p->next!=NULL)
  31. {
  32. p=p->next;
  33. }
  34. q=(nodeptr)malloc(sizeof(struct node));
  35. if(q==NULL)
  36. {
  37. printf("memory not allocated!!!!! ");
  38. getch();
  39. //exit(1);
  40. }
  41. q->data=x;
  42. q->next=p->next;
  43. q->prev=p;
  44. p->next=q;
  45. }
  46. void free_list(nodeptr *p) //free the whole list
  47. {
  48. nodeptr q;
  49. while((*p)!=NULL)
  50. {
  51. q=*p;
  52. (*p)=(*p)->next;
  53. free(q);
  54. }
  55. *p=NULL;
  56. }
  57. void printr_nodes(nodeptr p) //print data in list
  58. {
  59. nodeptr q;
  60. q=p;
  61. printf("\n");
  62. if(q==NULL)
  63. printf("List is empty");
  64. while(q->next!=NULL)
  65. q=q->next;
  66. while(q!=NULL)
  67. {
  68. printf("%d",q->data);
  69. q=q->prev;
  70. }
  71. }
  72. /* copy list copy1 into list copy2 */
  73. void copy5(nodeptr copy1,nodeptr *copy2)
  74. {
  75. int a;
  76. while(copy1!=NULL)
  77. {
  78. a=copy1->data;
  79. if(*copy2==NULL)
  80. insert_start(&(*copy2),a);
  81. else
  82. insert_after((*copy2),a);
  83. copy1=copy1->next;
  84. }
  85. }
  86. /* add 3lists coming from multiply function */
  87. void add(nodeptr list1,nodeptr list2,nodeptr list3)
  88. {
  89. int value,var,var1,var2,var3,temp=0;
  90. while(list3!=NULL||list1!=NULL||list2!=NULL)
  91. {
  92. if(list1==NULL)
  93. var1=0;
  94. else
  95. {
  96. var1=list1->data;
  97. list1=list1->next;
  98. }
  99. if(list2==NULL)
  100. var2=0;
  101. else
  102. {
  103. var2=list2->data;
  104. list2=list2->next;
  105. }
  106. if(list3==NULL)
  107. var3=0;
  108. else
  109. {
  110. var3=list3->data;
  111. list3=list3->next;
  112. }
  113. var=var1+var2+var3+temp;
  114. if(var>9)
  115. {
  116. value=var%10;
  117. temp=var/10;
  118. }
  119. else
  120. {
  121. value=var;
  122. temp=0;
  123. }
  124.  
  125. if(result==NULL)
  126. {
  127. insert_start(&result,value);
  128. }
  129. else
  130. insert_after(result,value);
  131.  
  132.  
  133. }
  134. if(temp!=0)
  135. insert_after(result,temp);
  136. }
  137. /* multiply the two lists */
  138. void multiply(nodeptr mul1,nodeptr mul2)
  139. {
  140. int a,b,value,d,temp=0,place=0;
  141. nodeptr mult1;
  142. insert_start(&good1,0);
  143. insert_start(&good2,0);
  144. insert_after(good2,0);
  145. while(mul2!=NULL)
  146. {
  147. mult1=mul1;
  148. temp=0;
  149. a=mul2->data;
  150. //printf("a is %d\n",a);
  151. while(mult1!=NULL)
  152. {
  153. b=(mult1->data)*a;
  154. b=temp+b;
  155. if(b>9)
  156. {
  157. value=b%10;
  158. temp=b/10;
  159. }
  160. else
  161. {
  162. value=b;
  163. temp=0;
  164. }
  165. //printf("value of VALUE is %d\n",value);
  166. if(place==0)
  167. {
  168. if(good==NULL)
  169. insert_start(&good,value);
  170. else
  171. insert_after(good ,value);
  172. }
  173. else if(place==1)
  174. {
  175. insert_after(good1,value);
  176. }
  177. else if(place==2)
  178. {
  179. insert_after(good2,value);
  180. }
  181. mult1=mult1->next; //point to next node
  182. }
  183. if(place==0 &&temp!=0)
  184. insert_after(good,temp);
  185. else if(place==1 &&temp!=0)
  186. insert_after(good1,temp);
  187. else if(place==2 &&temp!=0)
  188. insert_after(good2,temp);
  189. mul2=mul2->next;
  190. place++;
  191. }
  192. add(good,good1,good2);
  193. }
  194.  
  195. void main()
  196. {
  197. nodeptr list=NULL,list1=NULL;
  198. int i ,val,val1,num,div;
  199.  
  200. printf("enter no. for factorial to calculated\n");
  201. scanf("%d",&num);
  202. val1=num;
  203. while(num>0)
  204. {
  205. div=num%10;
  206. num=num/10;
  207. if(list==NULL)
  208. insert_start(&list,div);
  209. else
  210. insert_after(list ,div);
  211. }
  212. //printf("in 1st operand");
  213. //printr_nodes(list);
  214.  
  215. /* loop upto n-1 to 1*/
  216. for(i=val1-1;i>1;i--)
  217. {
  218. val=i;
  219. while(val>0)
  220. {
  221. div=val%10;
  222. val=val/10;
  223. if(list1==NULL)
  224. insert_start(&list1,div);
  225. else
  226. insert_after(list1,div);
  227. }
  228. //printf("\nin 2nd operand");
  229. //printr_nodes(list1);
  230. multiply(list,list1);
  231. free_list(&list1);
  232. free_list(&list);
  233. copy5(result,&list);
  234. free_list(&result);
  235. free_list(&good);
  236. free_list(&good1);
  237. free_list(&good2);
  238. }
  239. printf("\n----------in FINAL LIST is------------\n");
  240. printr_nodes(list);
  241.  
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement