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";
}
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.