how to check word is palindrome or not using linklist in c++->
#include<iostream>using namespace std;
struct node{
char data;
node * next=NULL;
node(char data){
this->data=data;
next=NULL;
}
};
//node* createlinklist(int n);
node* createlinklist(int n){
char data;
cin>>data;
node * head=NULL;
if(n==0){
return head;
}
head=new node (data);
node * tail=head;
cin>>data;
while(n>1){
node * temp=NULL;
temp=new node (data);
tail->next=temp;
tail=temp;
cin>>data;
n--;
}
return head;
}
bool palindrome(node *&head,node * temp){
if(temp==NULL&&head==NULL){
return false;
}
if(temp==NULL){
return true;
}
bool k= palindrome(head,temp->next);
if(k==false){
return false;
}
if(temp->data==head->data){
head=head->next;
return true;
}
return false;
}
int main(){
int n;
cin>>n;
node * head=createlinklist(n);
if(palindrome(head,head)){
cout<<"True"<<endl;
return 0;
}
cout<<"False"<<endl;
return 0;
}
Thank you so much for sharing these codes with us. This will be helpful to a lot of people.
ReplyDelete