Advertisement
rafid_shad

rabbi prob 1

Nov 23rd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct node
  4. {
  5.     int data;
  6.     struct node *right,*left,*top,*bottom;
  7. };
  8.  
  9.  
  10. struct node *head=NULL,*temp,*s,*s1,*s2,*s3;
  11.  
  12. int main()
  13. {
  14.     int n,a,top=0,bottom=0,right=0,left=0;
  15.     char d;
  16.  
  17.     cin>>n;
  18.  
  19.     head=(struct node*) malloc (sizeof(struct node));
  20.  
  21.    
  22.     head->right=NULL;
  23.     head->left=NULL;
  24.     head->top=NULL;
  25.     head->bottom=NULL;
  26.  
  27.     s=head;
  28.  
  29.     s1=head;
  30.  
  31.     s2=head;
  32.  
  33.     s3=head;
  34.  
  35.     for(int i=1; i<=n; i++)
  36.     {
  37.         struct node *newnode;
  38.         newnode=(struct node*) malloc (sizeof(struct node));
  39.         cin>>d;
  40.         cin>>a;
  41.         if(d=='L')
  42.         {
  43.             newnode->data=a;
  44.             left+=a;
  45.             newnode->left=NULL;
  46.             s->left=newnode;
  47.             s=newnode;
  48.  
  49.         }
  50.         else if(d=='R')
  51.         {
  52.             newnode->data=a;
  53.             right+=a;
  54.             newnode->right=NULL;
  55.  
  56.             s1->right=newnode;
  57.             s1=newnode;
  58.         }
  59.         else if(d=='T')
  60.         {
  61.             newnode->data=a;
  62.             top+=a;
  63.             newnode->left=NULL;
  64.  
  65.             s2->top=newnode;
  66.             s2=newnode;
  67.         }
  68.         else if(d=='B')
  69.         {
  70.             newnode->data=a;
  71.             bottom+=a;
  72.             newnode->bottom=NULL;
  73.  
  74.             s3->bottom=newnode;
  75.             s3=newnode;
  76.         }
  77.  
  78.  
  79.     }
  80.     int b[4];
  81.     b[0]=top;
  82.     b[1]=bottom;
  83.     b[2]=left;
  84.     b[3]=right;
  85.     sort(b,b+4);
  86.     if(b[3]==top)
  87.     {
  88.         cout<<"Top link list has maximum sum "<<b[3]<<endl;
  89.     }
  90.     if(b[3]==bottom)
  91.     {
  92.         cout<<"Bottom link list has maximum sum "<<b[3]<<endl;
  93.     }
  94.     if(b[3]==right)
  95.     {
  96.         cout<<"Right link list has maximum sum "<<b[3]<<endl;
  97.     }
  98.     if(b[3]==left)
  99.     {
  100.         cout<<"Left link list has maximum sum "<<b[3]<<endl;
  101.     }
  102.  
  103.     return 0;
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement