C Programming – HackerRank Solution | Withdrawal from Bank |

(HackerRank) Write a Modular C Programming code to solve Withdrawal from Bank.There is a district named Singrauli in MP. People from neighbouring villages come at this district bank in the district to withdraw money.

There is a district named Singrauli in MP. People from neighbouring villages come at this district bank in the district to withdraw money. Everyone made a huge crowd in front of the bank. By seeing the crowd bank manager is annoyed as it is disturbing other activities. So, the manager plans to divide the crowd into 2 different lines. First-line includes those who wish to withdraw through cheque. Second-line includes withdrawal through withdrawal slip. Assume codes for withdrawing through cheque as C, through withdrawal slip as W, can you help the Manager to divide the crowd?

Input Format

First Line indicates N number of customers visiting the Bank.

Next N lines indicate Name of account holder and Code for type of Withdrawal (separated by a space).

Constraints

0 < N <= 100

Name of Account holder is String. Code for Type of Withdrawal is single character.

Output Format

First Line indicates Names of account holders with code for withdrawing through cheque.

Second Line indicates Names of account holders with code for withdrawing through slip.

Sample Input 0

7
Ajay C
Raj W
Rani C
Rahul C
Aditya W
Deepa W
Vani C

Sample Output 0

Ajay Rani Rahul Vani
Raj Aditya Deepa

Sample Input 1

3
Ram W
Kumar W
Biswas W

Sample Output 1

Line 1 Empty.
Ram Kumar Biswas

Refer : C Programming HackerRank all solutions for Loops | Arrays | strings | Data Structures | Linked lists | Stacks | Queues | Binary Trees

 

CODE:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

struct node{
  char name[20];
  char code;
    struct node *link;
};
typedef struct node *NODE;
NODE insert_end(NODE head);
void display(NODE head,int i);
NODE separate_w(NODE head);
NODE delete_w(NODE head);
int main() {
    int n;
    NODE head1=NULL,head2=NULL;
    scanf("%d",&n);
    for(int i=0;i<n;i++) { head1=insert_end(head1); } head2=separate_w(head1); head1=delete_w(head1); display(head1,1); printf("\n"); display(head2,2); return 0; } NODE insert_end(NODE head) { NODE cur=head; NODE newnode=malloc(sizeof(struct node)); scanf("%s %c",newnode->name,&newnode->code);
    //printf("%s %c\n",newnode->name,newnode->code);
    if(cur==NULL)
    {
        head=newnode;
    }
    else{
        while(cur->link!=NULL)
        {
            cur=cur->link;
        }
        cur->link=newnode;
    }
    newnode->link=NULL;
    return head;
}
void display(NODE head,int i)
{
    NODE cur=head;
    if(cur==NULL)
    {
      printf("Line %d Empty.",i);  
    }
    else{
    while(cur!=NULL)
    {
        printf("%s ",cur->name);
        cur=cur->link;
    }
    }
}
NODE separate_w(NODE head1)
{
    NODE cur=head1,cur2;
    NODE head2=NULL;
    while(cur!=NULL)
    {
        if(cur->code=='W')
        {
            NODE newnode=malloc(sizeof(struct node));
            strcpy(newnode->name,cur->name);
            newnode->code=cur->code;
            if(head2==NULL)
            {
                head2=newnode;
            }
            else{
                cur2=head2;
                while(cur2->link!=NULL)
                {
                    cur2=cur2->link;
                }
                cur2->link=newnode;
            }
            newnode->link=NULL;
        }
        cur=cur->link;
    }
    return head2;
}
NODE delete_w(NODE head)
{
    NODE cur=head,prev=NULL;
    while(cur!=NULL)
    {
        if(cur->code=='W')
        {
            if(cur==head)
            {
                head=cur->link;
                free(cur);
                cur=head;
            }
            else{
                prev->link=cur->link;
                free(cur);
                cur=prev->link;
            }
        }
        else{
            prev=cur;
            cur=cur->link;
        }
    }
    return head;
}

OUTPUT

Congratulations, you passed the sample test case.

Click the Submit Code button to run your code against all the test cases.

Input (stdin)

7
Ajay C
Raj W
Rani C
Rahul C
Aditya W
Deepa W
Vani C
Your Output (stdout)

Ajay Rani Rahul Vani 
Raj Aditya Deepa 
Expected Output

Ajay Rani Rahul Vani
Raj Aditya Deepa

 

Please find some more codes of Loops, Condition Statements, 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, Stacks, Queues, Binary Trees, MISC, Solved model question papers & Hacker Rank all solutions on the below page:

Top 100+ C Programming codes – KLE Technological University

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

Welcome to FactsPrime

Sorry, We have detected that you have activated Ad-Blocker. Please Consider supporting us by disabling your Ad Blocker, It helps us in maintaining this website. To View the content, Please disable adblocker and refresh the page.

Thank You !!!