[C][단순연결리스트] 다항식더하기

stack overflow/C 2012. 8. 10. 04:28 posted by Allen Park


Polynomia_src.cpp












  1. #include <stdio.h>

  2. #include <stdlib.h>

  3. #include <conio.h>



  4. typedef struct node {

  5. int gs;  // 계수

  6. int js;  // 지수

  7. struct node *next;

  8. } NODE;

  9. NODE *start[3] = {NULL};

  10. int cnt[3] = {0};

  11. bool rage_flag = false;



  12. void del(int i);

  13. void init(int i);

  14. void rage();

  15. void insert(int i, int g, int j);

  16. void print();



  17. void init(int i)

  18. {

  19. NODE *del, *cur = start[i];


  20. if(start[i] == NULL)

  21. return;

  22. else {

  23. while(cur->next != NULL) {

  24. del = cur;

  25. cur = cur->next;

  26. free(del);

  27. }

  28. del = cur;

  29. start[i] = NULL;

  30. free(del);

  31. }

  32. }



  33. void del(int i)

  34. {

  35. NODE *del, *cur = start[i];


  36. while(cur->next != NULL) {

  37. if(cur->next->gs == 0) {

  38. del = cur->next;

  39. cur->next = del->next;

  40. free(del);

  41. cnt[i]--;

  42. return;

  43. }

  44. cur = cur->next;

  45. }


  46. if(start[i]->gs == 0) {

  47. del = start[i];

  48. start[i] = start[i]->next;

  49. free(del);

  50. cnt[i]--;

  51. return;

  52. }

  53. }



  54. void rage()

  55. {

  56. int i;

  57. if(start[0] == NULL || start[1] == NULL) {

  58. printf("먼저 A랑 B를 채워\n");

  59. return;

  60. }

  61. init(2);

  62. for(i = 0; i < 2; i++) {

  63. NODE *cur = start[i];

  64. while(cur->next != NULL) {

  65. insert(2, cur->gs, cur->js);

  66. cur = cur->next;

  67. }

  68. insert(2, cur->gs, cur->js);

  69. }

  70. rage_flag = true;

  71. }



  72. void insert(int i, int g, int j)

  73. {

  74. NODE *newnode, *cur;

  75. newnode = (NODE*)malloc(sizeof(NODE));

  76. newnode->next = NULL;

  77. newnode->gs = g;

  78. newnode->js = j;

  79. cur = start[i];

  80. rage_flag = false;


  81. if(start[i] != NULL) {

  82. while(cur->next != NULL) {

  83. if(cur->js == j) {

  84. cur->gs += g;

  85. if(cur->gs == 0)

  86. del(i);

  87. return;

  88. }

  89. cur = cur->next;

  90. }

  91. if(cur->js == j) {

  92. cur->gs += g;

  93. if(cur->gs == 0)

  94. del(i);

  95. return;

  96. }

  97. cur = start[i];

  98. }

  99. if(start[i] == NULL || start[i]->js < newnode->js) {

  100. newnode->next = start[i];

  101. start[i] = newnode;

  102. cnt[i]++;

  103. return;

  104. }


  105. if(cur->js >= newnode->js) {

  106. while(cur->next != NULL) {

  107. if(cur->next->js <= newnode->js) {

  108. newnode->next = cur->next;

  109. cur->next = newnode;

  110. cnt[i]++;

  111. return;

  112. }

  113. cur = cur->next;

  114. }

  115. cur->next = newnode;

  116. cnt[i]++;

  117. }

  118. }



  119. void print()

  120. {

  121. int i, minus;


  122. for(i = 0; i < 3; i++) {

  123. NODE *cur = start[i];


  124. if(i == 0)

  125. printf("A = ");

  126. else if(i == 1)

  127. printf("B = ");

  128. else

  129. printf("C = ");


  130. if(start[i] == NULL) {

  131. if(i == 2) {

  132. printf("아직 합치지 않았어\n");

  133. continue;

  134. }

  135. printf("이 배열은 비어있어\n");

  136. continue;

  137. }

  138. else {

  139. if(i == 2 && !rage_flag) {

  140. printf("다시 합쳐\n");

  141. continue;

  142. }

  143. }

  144. printf("%dX^%d", cur->gs, cur->js);

  145. if(cur->next != NULL)

  146. cur = cur->next;

  147. else{

  148. printf("\n");

  149. continue;

  150. }


  151. while(cur->next != NULL) {

  152. if(cur->gs > 0) {

  153. minus = cur->gs;

  154. printf(" + ");

  155. }

  156. else if(cur->gs < 0) {

  157. minus = -cur->gs;

  158. printf(" - ");

  159. }

  160. printf("%dX^%d", minus, cur->js);

  161. cur = cur->next;

  162. }

  163. if(cur->gs > 0) {

  164. minus = cur->gs;

  165. printf(" + ");

  166. }

  167. else if(cur->gs < 0) {

  168. minus = -cur->gs;

  169. printf(" - ");

  170. }

  171. printf("%dX^%d\n", minus, cur->js);

  172. }

  173. }



  174. int main()

  175. {

  176. int sel, gs, js;


  177. while(1) {

  178. system("cls");

  179. puts("1. Insert A");

  180. puts("2. Insert B");

  181. puts("3. Print");

  182. puts("4. A + B");

  183. puts("5. Initialize");

  184. puts("6. Quit");

  185. printf("골라 : ");

  186. scanf("%d", &sel);

  187. fflush(stdin);


  188. switch(sel) {

  189. case 1:

  190. printf("식 A 입력\n");

  191. printf("계수 : ");

  192. scanf("%d", &gs);

  193. fflush(stdin);

  194. printf("지수 : ");

  195. scanf("%d", &js);

  196. insert(0, gs, js);

  197. printf("식 A에 추가했다\n");

  198. break;

  199. case 2:

  200. printf("식 B 입력\n");

  201. printf("계수 : ");

  202. scanf("%d", &gs);

  203. fflush(stdin);

  204. printf("지수 : ");

  205. scanf("%d", &js);

  206. insert(1, gs, js);

  207. printf("식 B에 추가했어\n");

  208. break;

  209. case 3:

  210. print();

  211. break;

  212. case 4:

  213. if(rage_flag) {

  214. printf("이미 더했어\n");

  215. getch();

  216. continue;

  217. }

  218. rage();

  219. printf("두 식을 더했어\n");

  220. break;

  221. case 5:

  222. init(0);

  223. init(1);

  224. init(2);

  225. printf("모두 초기화했어\n");

  226. break;

  227. case 6:

  228. exit(1);

  229. }

  230. getch();

  231. }

  232. return 0;

  233. }


doubly_linked_list_src.cpp










  1. #include <stdio.h>

  2. #include <stdlib.h>

  3. #include <conio.h>



  4. typedef struct node {

  5. int key;

  6. struct node *prev;

  7. struct node *next;

  8. } DNODE;

  9. DNODE *start = NULL;

  10. int cnt = 0;



  11. void insert()  // 오름차순 정렬삽입

  12. {

  13. DNODE *newnode, *cur;

  14. newnode = (DNODE*)malloc(sizeof(DNODE));

  15. newnode->prev = newnode->next = NULL;

  16. cur = start;


  17. printf("삽입할 정수를 입력해 : ");

  18. scanf("%d", &newnode->key);

  19. fflush(stdin);


  20. if(start == NULL || start->key > newnode->key) {

  21. newnode->next = start;

  22. start = newnode;

  23. printf("맨 앞에 삽입됐다\n");

  24. cnt++;

  25. return;

  26. }


  27. if(cur->key <= newnode->key) {

  28. while(cur->next != NULL) {

  29. if(cur->next->key >= newnode->key) {

  30. newnode->prev = cur;

  31. newnode->next = cur->next;

  32. cur->next->prev = newnode;

  33. cur->next = newnode;

  34. cnt++;

  35. printf("중간에 삽입됐다\n");

  36. return;

  37. }

  38. cur = cur->next;

  39. }


  40. newnode->prev = cur;

  41. cur->next = newnode;

  42. printf("맨 끝에 삽입됐다\n");

  43. cnt++;

  44. }

  45. }



  46. void print()

  47. {

  48. DNODE *cur = start;


  49. if(start == NULL) {

  50. printf("배열이 비어 있어서 삭제못해\n");

  51. return;

  52. }


  53. while(cur->next != NULL) {

  54. printf("%d -> ", cur->key);

  55. cur = cur->next;

  56. }

  57. printf("%d", cur->key);

  58. }



  59. void del()

  60. {

  61. int k;

  62. bool flag = false;

  63. DNODE *del, *cur = start;


  64. if(start == NULL) {

  65. printf("배열이 비어 있어서 출력못해\n");

  66. return;

  67. }


  68. printf("삭제할 정수를 입력해 : ");

  69. scanf("%d", &k);

  70. fflush(stdin);


  71. while(cur->next != NULL) {

  72. if(cur->next->key == k) {

  73. del = cur->next;

  74. cur->next = del->next;

  75. printf("배열의 %d를 삭제한다\n", del->key);

  76. free(del);

  77. cnt--;

  78. flag = true;

  79. }

  80. else

  81. cur = cur->next;

  82. }


  83. if(start->key == k) {

  84. del = start;

  85. start = start->next;

  86. printf("배열의 %d를 삭제한다\n", del->key);

  87. free(del);

  88. cnt--;

  89. flag = true;

  90. }

  91. if(start != NULL)

  92. printf("start 노드의 key 값 : %d\n", start->key);


  93. if(!flag)

  94. printf("%d는 배열에 없어\n", k);

  95. }



  96. int main()

  97. {

  98. int sel;


  99. while(1) {

  100. system("cls");

  101. puts("1. Insert");

  102. puts("2. Print");

  103. puts("3. Delete");

  104. puts("4. Quit");

  105. printf("골라 : ");

  106. scanf("%d", &sel);

  107. fflush(stdin);


  108. switch(sel) {

  109. case 1: insert(); break;

  110. case 2: print();  break;

  111. case 3: del();    break;

  112. case 4: exit(1);

  113. }


  114. getch();

  115. }


  116. return 0;

  117. }





원블럭 비대칭

ㅋ_ㅋ 2012. 8. 9. 01:11 posted by Allen Park





사람들이 하도 투블럭투블럭 거려서


당당히 원블럭 지름


근데 머리가 그나마 사진 모습 처럼이라도 유지되지는 않음 절대


충격과 공포다



'ㅋ_ㅋ' 카테고리의 다른 글

2012/08/07  (0) 2012.08.07