A class octopus has a dummy head which has four pointers – left, right, top and bottom. You have to build linked list in these 4 directions depending on the input commands. There will be four types of commands – L, R, T and B. Their specifications are given below:
L x: insert x in the left link list // Where x is an integer number.
R x: insert x in the right link list
T x: insert x in the top link list
B x: insert x in the bottom link list
Input: Test case will start with a number n indicating the number of lines following it.
Output: you have to give output that which link list (Left, Right, Top and Bottom) has maximum sum of values. See the sample input and output.
Sample input
14
L 3
L 15
L 1
R 17
T 9
T 10
B 13
T 5
L 8
R 3
R 12
B 2
B 3
B 4
Sample output
Right Link List Has Maximum Sum 32
#include<stdio.h>
struct Node{
int data;
struct Node * left;
struct Node * right;
struct Node * top;
struct Node * bottom;
};
struct Node* insert(int x)
{
struct Node* newNode= (struct Node*)malloc(sizeof(struct Node));
newNode->data = x;
newNode->left = NULL;
newNode->right = NULL;
newNode->top = NULL;
newNode->bottom = NULL;
return (newNode);
}
int main(){
struct Node* root = NULL;
int n;
printf("Enter the number of commands\n");
scanf("%d",&n);
float sumRight, sumLeft, sumTop, sumBottom;
char y;
int x , i;
int m = n * 2;
for(i=0; i<m; i++){
scanf("%c",&y);
scanf("%d",&x);
if(y=='L'){
sumLeft += x;
}
else if(y=='R'){
sumRight += x;
}
else if(y=='T'){
sumTop += x;
}
else if(y=='B'){
sumBottom += x;
}
}
float arr[4] = {sumLeft,sumRight,sumTop, sumBottom };
float max = arr[0];
for(i=1; i<4; i++){
if(arr[i]>arr[0]){
max = arr[i];
}
}
if(max == sumLeft ){
printf("Left Link List has maximum sum %f", sumLeft);
}
else if(max == sumRight){
printf("Right Link List has maximum sum %f", sumRight);
}
else if(max == sumTop){
printf("Top Link List has maximum sum %f", sumTop);
}
else if(max == sumBottom){
printf("Bottom Link List has maximum sum %f", sumBottom);
}
return 0;
}
Comments
Leave a comment