50181. Calculate the area

I'm a slow walker, but I never walk backwards.

Task Description

Implement a function int calArea(NODE*) to calculate the area within two lists as its boundary. Each node has two pointers - one goes right and one goes down.

Both lists start from the same left upper node. The first list goes right first, and the second list goes down first. Each step goes distance 1 in either direction. Eventually these two lists will go into the same node, so we can define the area bounded by these two lists. Note that at the end of two list both right and down pointers are NULL.

The following is calArea.h file. You have to implement int calArea(NODE*).

#include<stdio.h>
#include<stdlib.h>
typedef struct node{
    struct node* rightPtr;
    struct node* downPtr;
}NODE;
int calArea(NODE* head);

You can test your function with this main function.

#include<stdio.h>
#include<stdlib.h>
#include"calArea.h"
 
NODE* initList(NODE* pos, int len){
    NODE* tail = pos;
    char direction;
    for(int i = 0; i < len-1; i++){
        scanf("%c", &direction);
        getchar();
        NODE* pNew = (NODE*) malloc(sizeof(NODE));
        pNew->downPtr = NULL;
        pNew->rightPtr = NULL;
        if(direction == 'r'){
            pos->rightPtr = pNew;
            pos = pos->rightPtr;
        }
        else{
            pos->downPtr = pNew;
            pos = pos->downPtr;
        }
        if(i == len-3)
            tail = pos;
    }
    return tail;
}
 
int main(){
    int len;
 
    NODE* head = (NODE*) malloc(sizeof(NODE));
 
    scanf("%d\n", &len);
    getchar();
    NODE *pos;
    char direction;
    scanf("%c", &direction);
    head->rightPtr = (NODE*) malloc(sizeof(NODE));
    pos = head->rightPtr;
    pos->downPtr = NULL;
    pos->rightPtr = NULL;
    NODE* tailR = initList(pos, len);
 
    head->downPtr = (NODE*) malloc(sizeof(NODE));
    pos = head->downPtr;
    pos->downPtr =NULL;
    pos->rightPtr = NULL;
    scanf("%c", &direction);
    getchar();
    NODE* tailD = initList(pos, len);
    free(tailD->rightPtr);
    tailD->rightPtr = tailR->downPtr;
 
    printf("%d", calArea(head));
}

Input Format

The first line is length L of both lists. The following 2L lines are the direction of two lists. The first L lines are for the first list and the second L lines are for second list.

Output Format

Output the area within the lists which is the return value of int calArea(NODE*).

Sample Input 1

4
r
r
d
d
d
d
r
r

Sample Output 1

4

Sample Input 2

4
r
d
d
d
d
d
d
r

Sample Output 2

3

Sample Input 3

6
r
d
r
d
r
d
d
d
r
d
r
r

Sample Output 3

5

Discussion