fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "list.h"
  4. #include <bits/stdc++.h>
  5.  
  6.  
  7. list_datatype *list;
  8. int number_element = 0;
  9. void init_list() {
  10. list = (list_datatype*)malloc(sizeof(list_datatype));
  11. list->first = NULL;
  12. list->last = NULL;
  13. }
  14. void insert_head_node(int x) {
  15. Node *newnode = (Node*)malloc(sizeof(Node));
  16. newnode->data = x;
  17. if(list->first == NULL) {
  18. list->first = newnode;
  19. list->last = newnode;
  20. number_element++;
  21. } else {
  22. newnode->next = list->first;
  23. list->first = newnode;
  24. number_element++;
  25. }
  26. }
  27. void insert_last_node(int x) {
  28. Node *newnode = (Node*)malloc(sizeof(Node));
  29. Node *temperate = (Node*)malloc(sizeof(Node));
  30. newnode->data = x;
  31. if(list->last == NULL) {
  32. list->last = newnode;
  33. list->first = newnode;
  34. number_element++;
  35. } else {
  36. temperate = list->last;
  37. temperate->next = newnode;
  38. list->last = newnode;
  39. number_element++;
  40. }
  41. }
  42.  
  43. void insert_middle_node(int x, int location) {
  44. // neu vi tri can chen la 1 thi thanh chen dau, nen can location >= 2 moi chen dc
  45.  
  46. if(number_element < 2 || location < 2 || location >= number_element) {
  47. printf("ERORR INSERT\n");
  48. exit(1);
  49. }
  50. int count_element = 1;
  51. Node *newnode = (Node*)malloc(sizeof(Node));
  52. newnode->data = x;
  53. Node *temperate = list->first;
  54. while(count_element != location - 1) {
  55. temperate = temperate->next;
  56. count_element++;
  57. }
  58. newnode->next = temperate->next;
  59. temperate->next = newnode;
  60. number_element++;
  61. }
  62.  
  63. void delete_head() {
  64. Node *temperate = list->first;
  65. list->first = temperate->next;
  66. number_element--;
  67. free(temperate);
  68. }
  69. void delete_last() {
  70. Node *temperate1 = list->first;
  71. Node *temperate2 = list->first;
  72. while(temperate1->next != NULL) {
  73. temperate2 = temperate1;
  74. temperate1 = temperate1->next;
  75. }
  76. temperate2->next = NULL;
  77. list->last = temperate2;
  78. number_element--;
  79. free(temperate1);
  80. }
  81. void delete_middle_node(int location) {
  82. if(number_element < 2 || location < 2 || location >= number_element) {
  83. printf("ERORR DELETE\n");
  84. exit(1);
  85. }
  86. int count_element = 1;
  87. Node *temperate1 = list->first;
  88. Node *temperate2 ;
  89. while(count_element != location - 1) {
  90. temperate1 = temperate1->next;
  91. count_element++;
  92. }
  93. temperate2 = temperate1->next;
  94. temperate1->next = temperate2->next;
  95. number_element--;
  96. free(temperate2);
  97.  
  98. }
  99. void display_list() {
  100. Node *tmp = list->first;
  101. while(tmp) {
  102. // printf("%p %d ",tmp,tmp->data);
  103. printf("%d ",tmp->data);
  104. tmp = tmp->next;
  105. }
  106. printf("\nnumber_element = %d",number_element );
  107. printf("\n");
  108. }
  109.  
  110. ////////////////////////////////////////////////////////////////////////////////
  111. ////////////////////////////////////////////////////////////////////////////////
  112. ////////////////////////////////////////////////////////////////////////////////
  113. ////////////////////////////////////////////////////////////////////////////////
  114. ////////////////////////////////////////////////////////////////////////////////
  115.  
  116. Node_Poly* creat_node(int coeff, int pow) {
  117. Node_Poly *temporary = (Node_Poly*)malloc(sizeof(Node_Poly));
  118. if(!temporary) {
  119. printf("OVERFLOW STACK\n");
  120. exit(1);
  121. }
  122. temporary->coeff = coeff;
  123. temporary->pow = pow;
  124. temporary->next = NULL;
  125. return temporary;
  126. }
  127. void add_node_poly(Poly *_Poly,Node_Poly *node){
  128. Node_Poly *temporary ;
  129. if(_Poly->head == NULL) {
  130. _Poly->head = node;
  131.  
  132. }
  133. else {
  134. temporary = _Poly->head;
  135. // duyet den phan tu cuoi cung cua list
  136. while(temporary->next != NULL) {
  137. temporary = temporary->next;
  138. }
  139. temporary->next = node;
  140. }
  141. }
  142.  
  143. void add_list_poly(List_Poly *_List_Poly,Poly *_Poly){
  144. Poly *temporary ;
  145. if(_List_Poly->head == NULL) {
  146. _List_Poly->head = _Poly;
  147.  
  148. }
  149. else {
  150. temporary = _List_Poly->head;
  151. // duyet den phan tu cuoi cung cua list
  152. while(temporary->next != NULL) {
  153. temporary = temporary->next;
  154. }
  155. temporary->next = _Poly;
  156. }
  157. }
  158.  
  159. void display_polynominal(Poly _Poly) {
  160. Node_Poly *temporary = _Poly.head;
  161. // printf("aaaaaaaaaaa\n");
  162. while(temporary->next!=NULL) {
  163. printf("%dxX^%d + ",temporary->coeff, temporary->pow);
  164. temporary = temporary->next;
  165. }
  166. printf("%dxX^%d ",temporary->coeff, temporary->pow);
  167. printf("\n");
  168. }
  169.  
  170. int min(int a, int b) {
  171. return a > b ? b : a;
  172. }
  173. int max(int a, int b) {
  174. return a < b ? b : a;
  175. }
  176.  
  177. void add_two_polynominal(Poly _Poly1, Poly _Poly2, Poly *_Poly3) {
  178. Node_Poly *temporary1 = _Poly1.head;
  179. Node_Poly *temporary2 = _Poly2.head;
  180. while(temporary1!= NULL && temporary2 != NULL) {
  181. if(temporary1->pow > temporary2->pow) {
  182. Node_Poly *a = (Node_Poly*)malloc(sizeof(Node_Poly));
  183. a->pow = temporary1->pow;
  184. a->coeff = temporary1->coeff;
  185. temporary1 = temporary1->next;
  186. add_node_poly(_Poly3,a);
  187. } else if (temporary1->pow < temporary2->pow) {
  188. Node_Poly *a = (Node_Poly*)malloc(sizeof(Node_Poly));
  189. a->pow = temporary2->pow;
  190. a->coeff = temporary2->coeff;
  191. temporary2 = temporary2->next;
  192. add_node_poly(_Poly3,a);
  193. } else {
  194. Node_Poly *a = (Node_Poly*)malloc(sizeof(Node_Poly));
  195. a->pow = temporary2->pow;
  196. a->coeff = (temporary2->coeff + temporary1->coeff);
  197. temporary2 = temporary2->next;
  198. temporary1 = temporary1->next;
  199. add_node_poly(_Poly3,a);
  200. }
  201. }
  202. // nghia la temporary2 van con
  203. if(temporary1 == NULL) {
  204. while(temporary2 != NULL) {
  205. add_node_poly(_Poly3,temporary2);
  206. temporary2 = temporary2->next;
  207. }
  208. }
  209.  
  210. // nghia la temporary1 van con
  211. if(temporary2 == NULL) {
  212. while(temporary1 != NULL) {
  213. add_node_poly(_Poly3,temporary1);
  214. temporary1 = temporary1->next;
  215. }
  216. }
  217. }
  218. // add voi 2 poly la con tro
  219. void add_two_polynominal_2(Poly *_Poly1, Poly *_Poly2, Poly *_Poly3) {
  220. Node_Poly *temporary1 = _Poly1->head;
  221. Node_Poly *temporary2 = _Poly2->head;
  222. // printf("%d",temporary2->next->pow);
  223. while(temporary1!= NULL && temporary2 != NULL) {
  224. if(temporary1->pow > temporary2->pow) {
  225. Node_Poly *a = (Node_Poly*)malloc(sizeof(Node_Poly));
  226. a->pow = temporary1->pow;
  227. a->coeff = temporary1->coeff;
  228. temporary1 = temporary1->next;
  229. add_node_poly(_Poly3,a);
  230. } else if (temporary1->pow < temporary2->pow) {
  231. Node_Poly *a = (Node_Poly*)malloc(sizeof(Node_Poly));
  232. a->pow = temporary2->pow;
  233. a->coeff = temporary2->coeff;
  234. temporary2 = temporary2->next;
  235. add_node_poly(_Poly3,a);
  236. } else {
  237. Node_Poly *a = (Node_Poly*)malloc(sizeof(Node_Poly));
  238. a->pow = temporary2->pow;
  239. a->coeff = (temporary2->coeff + temporary1->coeff);
  240. temporary2 = temporary2->next;
  241. temporary1 = temporary1->next;
  242. add_node_poly(_Poly3,a);
  243. }
  244. }
  245. // nghia la temporary2 van con
  246. if(temporary1 == NULL) {
  247. while(temporary2 != NULL) {
  248. add_node_poly(_Poly3,temporary2);
  249. temporary2 = temporary2->next;
  250. }
  251. }
  252.  
  253. // nghia la temporary1 van con
  254. if(temporary2 == NULL) {
  255. while(temporary1 != NULL) {
  256. add_node_poly(_Poly3,temporary1);
  257. temporary1 = temporary1->next;
  258. }
  259. }
  260.  
  261. }
  262.  
  263. int length_poly(Poly _Poly) {
  264. Node_Poly *a = _Poly.head;
  265. int length = 0;
  266. while(a!=NULL) {
  267. a = a->next;
  268. length++;
  269. }
  270. return length;
  271. }
  272.  
  273. void multiple_two_polynominal(Poly _Poly1, Poly _Poly2, Poly *_Poly3) {
  274. // printf("%d\n",length_poly(_Poly1));
  275. // printf("%d\n",length_poly(_Poly2));
  276. int min_pow = min(_Poly1.head->pow,_Poly2.head->pow);
  277. List_Poly lp;
  278. lp.head = NULL;
  279. // tao cac don thuc con
  280. if(min_pow == _Poly1.head->pow ) {
  281. Node_Poly *a = _Poly1.head;
  282. while(a!=NULL) {
  283. Poly *tmp = (Poly*)malloc(sizeof(Poly));
  284. tmp->head = NULL;
  285. Node_Poly *b = _Poly2.head;
  286. // da thuc poly2 nhan voi nhan tu dau tien cua da thuc poly1
  287. while(b!=NULL) {
  288. Node_Poly *c = (Node_Poly*)malloc(sizeof(Node_Poly));
  289. c->coeff = (b->coeff)*(a->coeff);
  290. c->pow = (b->pow)+(a->pow);
  291. b = b->next;
  292. add_node_poly(tmp,c);
  293. }
  294. add_list_poly(&lp,tmp);
  295. a = a->next;
  296. }
  297. }
  298. else if( min_pow == _Poly2.head->pow) {
  299. Node_Poly *a = _Poly2.head;
  300. while(a!=NULL) {
  301. Poly *tmp = (Poly*)malloc(sizeof(Poly));
  302. tmp->head = NULL;
  303. Node_Poly *b = _Poly1.head;
  304. // da thuc poly2 nhan voi nhan tu dau tien cua da thuc poly1
  305. while(b!=NULL) {
  306. Node_Poly *c = (Node_Poly*)malloc(sizeof(Node_Poly));
  307. c->coeff = (b->coeff)*(a->coeff);
  308. c->pow = (b->pow)+(a->pow);
  309. b = b->next;
  310. add_node_poly(tmp,b);
  311. }
  312. add_list_poly(&lp,tmp);
  313. a = a->next;
  314. }
  315. }
  316.  
  317. //test ket qua cua cai tren
  318. Poly *aa = lp.head;
  319. printf("\\\\\\\\\\\\\\\\\n");
  320. while(aa!=NULL) {
  321. Node_Poly *temporary = aa->head;
  322. while(temporary->next!=NULL) {
  323. printf("%dxX^%d + ",temporary->coeff, temporary->pow);
  324. temporary = temporary->next;
  325. }
  326. printf("%dxX^%d ",temporary->coeff, temporary->pow);
  327. printf("\n");
  328. aa = aa->next;
  329. }
  330. //
  331.  
  332. // gio cong cac da thuc con lai voi nhau
  333. Poly *aaaa = lp.head;
  334. Poly b_b;
  335. b_b.head = NULL;
  336. add_two_polynominal_2(aaaa,aaaa->next,&b_b);
  337. // display_polynominal(b_b);
  338. aaaa = aaaa->next;
  339. aaaa = aaaa->next;
  340. while(aaaa!=NULL) {
  341. add_two_polynominal_2(&b_b,aaaa,&b_b);
  342. aaaa = aaaa->next;
  343. }
  344. // display_polynominal(b_b);
  345.  
  346. }
  347.  
  348. int main() {
  349. // init_list();
  350. // printf("CHEN VAO DAU DANH SACH\n");
  351. // insert_head_node(2);
  352. // insert_head_node(3);
  353. // insert_head_node(4);
  354. // display_list();
  355. // printf("CHEN VAO SAU DANH SACH\n");
  356. // insert_last_node(1);
  357. // insert_last_node(2);
  358. // insert_last_node(3);
  359. // insert_last_node(4);
  360. // insert_last_node(5);
  361. // // printf("%p\n",list->last->next->next->next);
  362. // display_list();
  363. // printf("CHEN VAO GIUA DANH SACH\n");
  364. // insert_middle_node(10,3);
  365. // insert_middle_node(20,3);
  366. // // insert_middle_node(50,9);
  367. // display_list();
  368. // printf("XOA DAU DANH SACH\n");
  369. // delete_head();
  370. // delete_head();
  371. // delete_head();
  372. // delete_head();
  373. // delete_head();
  374. // display_list();
  375. // printf("XOA CUOI DANH SACH\n");
  376. // delete_last();
  377. // delete_last();
  378. // display_list();
  379. // printf("XOA GIUA DANH SACH\n");
  380. // delete_middle_node(2);
  381. // // delete_middle_node(2);
  382. // display_list();
  383. // free(list);
  384. Poly l1,l2,l_resul,l_multiple_resul;
  385. l1.head = NULL;
  386. l2.head = NULL;
  387. l_resul.head = NULL;
  388. l_multiple_resul.head = NULL;
  389. Node_Poly *a1 = creat_node(2,2);
  390. Node_Poly *a2 = creat_node(4,1);
  391. Node_Poly *a3 = creat_node(3,0);
  392. add_node_poly(&l1,a1);
  393. add_node_poly(&l1,a2);
  394. add_node_poly(&l1,a3);
  395. display_polynominal(l1);
  396.  
  397. Node_Poly *a4 = creat_node(3,6);
  398. Node_Poly *a5 = creat_node(4,1);
  399. Node_Poly *a6 = creat_node(3,0);
  400. add_node_poly(&l2,a4);
  401. add_node_poly(&l2,a5);
  402. // add_node_poly(&l2,a6);
  403. display_polynominal(l2);
  404. add_two_polynominal(l1,l2,&l_resul);
  405. display_polynominal(l_resul);
  406. multiple_two_polynominal(l1,l2,&l_multiple_resul);
  407. free(a1);
  408. free(a2);
  409. free(a3);
  410. free(a4);
  411. free(a5);
  412. free(a6);
  413. return 0;
  414. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:3:10: fatal error: list.h: No such file or directory
 #include "list.h"
          ^~~~~~~~
compilation terminated.
stdout
Standard output is empty