Checking whether entered singly list is palindrome or not but it showing error that data and next is not declared

Stuti Thakur 41 Reputation points
2021-10-23T06:54:14.857+00:00

include<iostream>

include<bits/stdc++.h>

using namespace std;
class node{
public:
int size;
node()
{
cout<<"Enter the size of aarray : ";
cin>>size;

}

};
template<class T>
class array:public node{
public:
int data,choice=1,tem;
node *newnode;
node next;
bool isPalin(int
data,node *head,int next);
node head;
array()
{
while(choice)
{
head=0;
newnode=(node
)new(sizeof(node));
cout<<"Enter data ";
cin>>data;
&newnode->data;
&newnode->next=0;
if(head==0)
{
head=tem=newnode;
}
else
{
tem->next=newnode;
tem=newnode;
}
cout<<"Want to enter next node";
cin>>choice;
}

}  

};
template<class T>
bool array<T> :: isPalin(int* data,int* next){
node* slow= head; // Temp pointer
stack <int> s; // Declare a stack
while(slow != NULL){ // Push all elements of the list to the stack
s.push(slow->data);
slow = slow->next; // Move ahead
}
while(head != NULL ){ // Iterate in the list again and check by popping from the stack
int i=s.top(); // Get the top most element
s.pop(); // Pop the element
if(head -> data != i) // Check if data is not same as popped element
{
return false;
}
head=head->next; // Move ahead
}
return true;
}
int main()
{
array<bool>l;
l.isPalin(l.data,l.next) ;
}

Developer technologies C++
{count} votes

1 answer

Sort by: Most helpful
  1. YujianYao-MSFT 4,296 Reputation points Microsoft External Staff
    2021-10-25T07:46:31.597+00:00

    Hi @Stuti Thakur ,

    I understand what you mean. Actually, templates are not needed all the time. I modified your code, you could refer to it.

    #include<iostream>  
    #include<vector>  
    #include<stack>  
    using namespace std;  
    class arr {  
    public:  
     int n, a;  
     arr* next;  
     arr* tem;  
     arr* head = nullptr;  
     int data, choice = 1;  
     bool isPalin();  
     void create()  
     {  
     while (choice)  
     {  
     arr* NewNode;  
     NewNode = new arr;  
     cout << "Enter data ";  
     cin >> data;  
     NewNode->data = data;  
     NewNode->next = nullptr;  
     /*head->next = NewNode;*/  
     if (head == nullptr)  
     {  
     head = NewNode;  
     tem = NewNode;  
     }  
     else  
     {  
     tem->next = NewNode;  
     tem = NewNode;  
     }  
     cout << "Want to enter next number";  
     cin >> choice;  
     }  
     }  
    };  
    bool arr::isPalin() {  
     arr* slow = head; // Temp pointer  
     stack <int> s; // Declare a stack  
     while (slow != NULL) { // Push all elements of the list to the stack  
     s.push(slow->data);  
     slow = slow->next; // Move ahead  
     }  
     while (head != NULL) { // Iterate in the list again and check by popping from the stack  
     int i = s.top(); // Get the top most element  
     s.pop(); // Pop the element  
     if (head->data != i) // Check if data is not same as popped element  
     {  
     return false;  
     }  
     head = head->next; // Move ahead  
     }  
     return true;  
    }  
    int main()  
    {  
     arr l;  
     l.create();  
     if (l.isPalin())  
     cout << "yes";  
     else  
     cout << "no";  
    }  
    

    143295-result.png

    Best regards,

    Elya


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.