Last active
December 13, 2016 12:18
-
-
Save hygull/3b876604318c3da9683548268c9a1c9c to your computer and use it in GitHub Desktop.
To check whether the linked list is palindrome or not created by hygull - https://repl.it/Eoig/1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
{ | |
"cretaed_after" : "13 Dec 2016" | |
"aim_of_program" : "To check whether the linked list is palindrome or not" | |
"coded_by" : "Rishikesh Agrawani" | |
"geeksforgeeks_problem_link":"http://www.practice.geeksforgeeks.org/problem-page.php?pid=700391" | |
"source_lang":"C" | |
"Compiler":"gcc(4.8.4)" | |
} | |
*/ | |
#include<stdlib.h> | |
#include<stdio.h> | |
/*The structure of the Node (Given)*/ | |
struct Node | |
{ | |
int data; | |
struct Node* next; | |
}; | |
//To set an array filled with the data of linked list's nodes. | |
void setArr(struct Node *ptr,int** arrPtr) | |
{ | |
if (ptr==NULL) | |
{ | |
return; | |
} | |
int i=0; | |
while(ptr!=NULL) | |
{ | |
* (*(arrPtr)+i ) = ptr->data; //Segmentation fault...* (*(arrPtr+i) )...So illegal | |
ptr=ptr->next; | |
i++; | |
} | |
} | |
//Counting number of nodes available on the linked list | |
int getNumberOfNodes(struct Node *ptr) | |
{ | |
if (ptr==NULL){ | |
return 0; | |
} | |
int n=0; //n denotes number of nodes in linked list | |
while(ptr!=NULL) | |
{ | |
n+=1; | |
ptr=ptr->next; | |
} | |
return n; | |
} | |
//Function that checks whether linked list is empty of not (Function name is given and we have to design it according to the requirement) | |
bool isPalindrome(Node *head) | |
{ | |
int l; | |
l=getNumberOfNodes(head); | |
if (l==0){ | |
return false; | |
} | |
int *a=(int*)malloc(l*sizeof(int)); | |
setArr(head,&a); | |
int mid=l/2; //To stop re-caluculation | |
for (int i=0;i<mid;i++) | |
{ | |
if (*(a+i)!=*(a+l-1-i)){ | |
return false; | |
} | |
} | |
return true; | |
} | |
//A function that inserts Node at the end of linked list | |
struct Node* insertNodeAtEnd(struct Node *ptr,int data){ | |
struct Node* head=ptr; | |
struct Node* node=(struct Node*)malloc(sizeof(struct Node)); | |
node->data=data; | |
node->next=NULL; | |
if(ptr==NULL){ | |
ptr=node; | |
return ptr; | |
} | |
while(ptr->next!=NULL){ | |
ptr=ptr->next; | |
} | |
ptr->next=node; | |
return head; | |
} | |
//Driver function for starting the application | |
int main(){ | |
int t,n; | |
scanf("%d",&t); //Number of test cases | |
struct Node *head=NULL; | |
for (int i=0;i<t;++i){ | |
scanf("%d",&n); //Number of nodes in the linked list | |
for (int j=0;j<n;++j){ | |
int data; | |
scanf("%d",&data); //Data for the nodes of each linked list | |
head=insertNodeAtEnd(head,data); | |
} | |
if (isPalindrome(head)==true){ | |
printf("%d\n",1); | |
}else{ | |
printf("%d\n",0 ); | |
} | |
free(head); | |
head=NULL; | |
} | |
} | |
/* 1st RUN: | |
1 | |
3 | |
1 2 3 | |
0 | |
*/ | |
/* 2nd RUN: | |
1 | |
5 | |
1 2 3 2 1 | |
1 | |
*/ | |
/*3rd RUN: | |
5 | |
6 | |
1 2 3 4 5 6 | |
5 | |
1 2 3 2 1 | |
3 | |
1 4 2 | |
2 | |
1 1 | |
7 | |
1 2 4 67 4 2 1 | |
0 | |
1 | |
0 | |
1 | |
1 | |
*/ | |
/* 4TH RUN | |
2 | |
5 | |
1 2 3 4 5 | |
5 | |
1 2 3 2 1 | |
0 | |
1 | |
*/ | |
/* 5TH RUN | |
5 | |
5 | |
1 2 3 2 1 | |
2 | |
1 2 | |
2 | |
1 1 | |
4 | |
1 2 2 1 | |
3 | |
1 2 3 | |
1 | |
0 | |
1 | |
1 | |
0 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment