Advertisement
Naimul_X

Untitled

Dec 9th, 2020 (edited)
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. ****************************************** PROBLEM A *************************************
  2.  
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5.  
  6. struct node {
  7. int data;
  8. struct node *left;
  9. struct node *right;
  10. struct node *top;
  11. struct node *bottom;
  12. };
  13.  
  14. typedef struct node Node;
  15.  
  16. int sumLeft = 0;
  17. int sumRight = 0;
  18. int sumTop = 0;
  19. int sumBottom = 0;
  20.  
  21. void countLeft(Node *head) {
  22. Node *temp;
  23. temp = head;
  24.  
  25. while (temp->left != NULL) {
  26. int sum = temp->left->data;
  27. sumLeft += sum;
  28. temp = temp->left;
  29. }
  30. }
  31.  
  32. void countRight(Node *head) {
  33. Node *temp;
  34. temp = head;
  35.  
  36. while (temp->right != NULL) {
  37. int sum = temp->right->data;
  38. sumRight += sum;
  39. temp = temp->right;
  40. }
  41. }
  42.  
  43. void countTop(Node *head) {
  44. Node *temp;
  45. temp = head;
  46.  
  47. while (temp->top != NULL) {
  48. int sum = temp->top->data;
  49. sumTop += sum;
  50. temp = temp->top;
  51. }
  52. }
  53.  
  54. void countBottom(Node *head) {
  55. Node *temp;
  56. temp = head;
  57.  
  58. while (temp->bottom != NULL) {
  59. int sum = temp->bottom->data;
  60. sumBottom += sum;
  61. temp = temp->bottom;
  62. }
  63. }
  64.  
  65. void insertBottom(Node *head, int value) {
  66. Node *flag, *temp;
  67.  
  68. if(head->bottom == NULL) {
  69. flag = new Node();
  70. flag->data = value;
  71. flag->left = NULL;
  72. head->bottom = flag;
  73. } else {
  74. temp = head;
  75. while (temp->bottom != NULL) {
  76. temp = temp->bottom;
  77. }
  78. flag = new Node();
  79. flag->data = value;
  80. flag->left = NULL;
  81. temp->bottom = flag;
  82. }
  83. }
  84.  
  85. void insertTop(Node *head, int value) {
  86. Node *flag, *temp;
  87.  
  88. if(head->top == NULL) {
  89. flag = new Node();
  90. flag->data = value;
  91. flag->left = NULL;
  92. head->top = flag;
  93. } else {
  94. temp = head;
  95. while (temp->top != NULL) {
  96. temp = temp->top;
  97. }
  98. flag = new Node();
  99. flag->data = value;
  100. flag->left = NULL;
  101. temp->top = flag;
  102. }
  103. }
  104.  
  105.  
  106. void insertRight(Node *head, int value) {
  107. Node *flag, *temp;
  108.  
  109. if(head->right == NULL) {
  110. flag = new Node();
  111. flag->data = value;
  112. flag->left = NULL;
  113. head->right = flag;
  114. } else {
  115. temp = head;
  116. while (temp->right != NULL) {
  117. temp = temp->right;
  118. }
  119. flag = new Node();
  120. flag->data = value;
  121. flag->left = NULL;
  122. temp->right = flag;
  123. }
  124. }
  125.  
  126. void insertLeft(Node *head, int value) {
  127. Node *flag, *temp;
  128.  
  129. if(head->left == NULL) {
  130. flag = new Node();
  131. flag->data = value;
  132. flag->left = NULL;
  133. head->left = flag;
  134. } else {
  135. temp = head;
  136. while (temp->left != NULL) {
  137. temp = temp->left;
  138. }
  139. flag = new Node();
  140. flag->data = value;
  141. flag->left = NULL;
  142. temp->left = flag;
  143. }
  144. }
  145.  
  146.  
  147. void Max(int sLeft, int sRight, int sTop, int sBottom) {
  148. if((sLeft > sRight) && (sLeft > sTop) && (sLeft > sBottom))
  149. {
  150. printf("LEFT LIST HAS MAX: %d\n",sLeft);
  151. }
  152. else if((sRight > sLeft) && (sRight > sTop) && (sRight > sBottom))
  153. {
  154. printf("RIGHT LIST HAS MAX: %d\n",sRight);
  155.  
  156. }
  157. else if((sTop > sLeft) && (sTop > sRight) && (sTop > sBottom))
  158. {
  159. printf("Top LIST HAS MAX: %d\n",sTop);
  160.  
  161. }
  162. else
  163. {
  164. printf("BOTTOM LIST HAS MAX: %d\n",sBottom);
  165.  
  166. }
  167.  
  168. }
  169.  
  170.  
  171.  
  172. int main() {
  173. cout<<"THIS CODE IS CONTRIBUTED BY NAIMUL KARIM HREDOY"<<endl;
  174. Node *head = new Node();
  175. int n;
  176. scanf("%d",&n);
  177.  
  178.  
  179. while (n--) {
  180. char choice;
  181. int value;
  182.  
  183. cin >> choice;
  184. scanf("%d",&value);
  185.  
  186.  
  187. if (choice == 'L') {
  188. insertLeft(head, value);
  189. } else if (choice == 'R') {
  190. insertRight(head, value);
  191. } else if (choice == 'T') {
  192. insertTop(head, value);
  193. } else if (choice == 'B') {
  194. insertBottom(head, value);
  195. }
  196. }
  197. countLeft(head);
  198. countRight(head);
  199. countTop(head);
  200. countBottom(head);
  201. Max(sumLeft, sumRight, sumTop, sumBottom);
  202. }
  203.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement