Advertisement
Niloy007

SolutionA

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