Check whether a Link list is a palindrome or not?
Question:- You have been given a linked list of numbers.
The size is not know. You need to find out if the number stored
in the list is a palindrome in the best possible optimized manner.
Check whether a Link list is a palindrome or not?
#include<stdio.h>
#include<stdlib.h>
#define FALSE 0
#define TRUE 1
typedef struct node_tag
{
int data;
struct node_tag *next;
}node_type;
void print_link_list(node_type *root);
node_type *create_link_list(int n);
int check_palindrome(node_type *root);
node_type *hroot;
main()
{
int n;
int ret_val=FALSE;
node_type *head;
printf("Give the number of node\n");
scanf("%d",&n);
head=create_link_list(n);;
print_link_list(head);
hroot=head;
ret_val=check_palindrome(head);
printf("ret_val=%d\n",ret_val);
}
int check_palindrome(node_type *root)
{
int ret_val;
if(root)
{
ret_val=check_palindrome(root->next);
if(root->data != hroot->data)
return FALSE;
hroot=hroot->next;
return ret_val;
}
return TRUE;
}
void print_link_list(node_type *root)
{
node_type *temp=root;
while(temp)
{
printf("%d-->",temp->data);
temp=temp->next;
}
printf("\n");
}
node_type *create_link_list(int n)
{
int i,a[400];
node_type *head,*temp,*root;
head = NULL;
printf("Give the link list elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
temp=(node_type *)malloc(sizeof(node_type));
temp->data=a[i];
temp->next=NULL;
if(i==0)
{
root=temp;
head=temp;
}
else
{
head->next=temp;
head=head->next;
}
}
return root;
}
Author: Gour Ch. Saha
Contact for any query:: gour_ch_saha@yahoo.co.in