-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsgLL3stringpal.cpp
More file actions
88 lines (87 loc) · 1.48 KB
/
sgLL3stringpal.cpp
File metadata and controls
88 lines (87 loc) · 1.48 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//input a string and store it in LL and then through LL check if its palindrome or not
#include<iostream>
#include<string>
#define null 0
using namespace std;
struct palstr
{
char data;
palstr *next;
};
palstr *first,*temp,*ttemp,*p,*revfirst,*revtemp,*rev,*orig;
void init()
{
first=temp=ttemp=null;
}
void addnode(char name)
{
ttemp=new palstr;
ttemp->data=name;
ttemp->next=null;
if(first==null)
first=ttemp;
else
temp->next=ttemp;
temp=ttemp;
}
void disp(palstr* head)
{
temp=head;
while(temp!=null)
{
cout<<temp->data<<"\n";
temp=temp->next;
}
}
void stringLL(string name,int l)
{
init();
for(int i=0;i<l;i++)
{
addnode(name[i]);
}
}
palstr* reversedLL(palstr* head)
{
revfirst=null;
while(head!=null)
{
revtemp=new palstr;
revtemp->data=head->data;
revtemp->next=revfirst;
revfirst=revtemp;
head=head->next;
}
return revfirst;
}
void ispal()
{
rev=reversedLL(first);
orig=first;
bool ispal=true;
while(orig!=null&rev!=null)
{
if(orig->data!=rev->data)
{
ispal=false;
break;
}
orig=orig->next;
rev=rev->next;
}
if( ispal)
cout<<"palindrome\n";
else
cout<<"not a palindrome\n";
}
int main()
{ init();
string name ="star";
int l=name.length();
stringLL(name,l);
disp(first);
reversedLL(first);
cout<<"\n";
disp(revfirst);
ispal();
}