-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_palindrome_in_LL.cpp
72 lines (71 loc) · 1.04 KB
/
check_palindrome_in_LL.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<iostream>
#include<stack>
#include<cstring>
using namespace std;
class node
{
string data;
node* next;
public:
node(string d)
{
d=data;
next=NULL;
}
};
void ins_tail(node* head, string data)
{
if(head==NULL)
{
return new node(data);
}
node* tail=head;
while(tail->next!=NULL)
{
tail=tail->next;
}
tail->next= new node(data);
}
bool check_palindrome(node* head)
{
stack<string> stk;
node* temp=head;
stk.push(temp);
while(temp!=NULL)
{
temp=temp->next;
stk.push(temp->data);
}
while(temp!=NULL)
{
temp=temp->next;
string s=stk.top();
stk.pop(s);
if(s==temp->data)
{
return true;
}
else
return false;
}
}
// for printing the linked list
void print_LL(node* head)
{
while(head!=NULL)
{
cout<<head->data<<"-->>";
head=head->next;
}
}
int main()
{
node* head=NULL;
ins_tail(head,"R");
ins_tail(head,"A");
ins_tail(head,"D");
ins_tail(head,"A");
ins_tail(head,"R");
print_LL(head);
return 0;
}