(HackerRank) Write a modular C program using DLL to check whether Harry returned passing by same cities in reverse direction?
Harry travels to a destination city during the night for emergency work. He passes through N intermediate cities during the travel. He completes his work and returns the next day morning. After reaching home, he was wondering whether he returned through the same route passing across the same cities in the reverse direction.
Write a modular C program using DLL to check whether Harry returned passing by the same cities in reverse direction?
Input Format
First line is N indicating number of cities.
Second line onwards is 2*N names of cities passed through from source to destination and back destination to source city. (includes 1st city, that is source and Nth city, that is destination city names)
Constraints
Number of cities should be more than or equal to 2.
Output Format
Print cities tavelled only in reverse direction.
Print “Palindrome.” if Harry has travelled through same cities. Otherwise “Not Palindrome.”
Sample Input 0
3
hubli
dharwad
belgaum
belgaum
dharwad
hubli
Sample Output 0
belgaum
dharwad
hubli
Palindrome.
Sample Input 1
1
Sample Output 1
Cities should be more than 2.
Sample Input 2
3
denver
aurora
lakewood
lakewood
denver
aurora
Sample Output 2
lakewood
denver
aurora
Not Palindrome.
CODE:
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> struct node { char city[30]; struct node *rl; struct node *ll; }; struct node * create_node(); struct node * insert_end(struct node * head); void check(struct node * head,int n); void display(struct node * head, int n); struct node *cur; struct node *prev; int main() { struct node * head=NULL; int n,i; scanf("%d",&n); if(n>=2) { for(i=0;i<n*2;i++) { head=insert_end(head); } display(head,n); check(head,n); } else { printf("Cities should be more than 2."); exit(0); } return 0; } struct node * create_node() { struct node *newnode; newnode=(struct node *)malloc(sizeof(struct node)); if(newnode==NULL) { printf("NO memory allocated"); } scanf("%s",newnode->city); newnode->rl=NULL; newnode->ll=NULL; return newnode; } struct node * insert_end(struct node * head) { struct node * newnode; newnode=create_node(); if(head==NULL) { head=newnode; } else { cur=head; while(cur->rl!=NULL) { cur=cur->rl; } newnode->ll=cur; cur->rl=newnode; } return head; } void check(struct node * head,int n) { int i,status=1; cur=head; for(i=0;i<n;i++) { cur=cur->rl; } prev=cur->ll; while(cur!=NULL) { if(strcmp(cur->city,prev->city)!=0) { status=0; break; } prev=prev->ll; cur=cur->rl; } if(status==1) { printf("\nPalindrome.\n"); } else { printf("\nNot Palindrome.\n"); } } void display(struct node * head, int n) { int i; cur=head; for(i=0;i<n;i++) { cur=cur->rl; } while(cur!=NULL) { printf("%s\n",cur->city); cur=cur->rl; } }
OUTPUT
Congratulations, you passed the sample test case. Click the Submit Code button to run your code against all the test cases. Input (stdin) 3 hubli dharwad belgaum belgaum dharwad hubli Your Output (stdout) belgaum dharwad hubli Palindrome. Expected Output belgaum dharwad hubli Palindrome.
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: