-
Notifications
You must be signed in to change notification settings - Fork 0
/
palin.cpp
97 lines (95 loc) · 1.62 KB
/
palin.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include<iostream>
#include<cstdlib>
using namespace std;
typedef struct node
{
int data;
struct node *next;
}node;
void create(node ** head,int n)
{
node *temp;
if(*head == NULL) {
temp = (node *)malloc(sizeof(node));
temp->data = n;
*head = temp;
(*head)->next = NULL;
}else {
create((&(*head)->next),n);
}
}
void print(node *head)
{
while(head != NULL) {
cout << head->data << "-->";
head = head->next;
}
}
void reverse(node **head)
{
int count = -1;
node *next;
node *temp = *head;
node *prev = NULL;
count++;
while(temp->next != NULL) {
next = (temp)->next;
//cout << "next is " << next->data << endl;
(temp)->next = prev;
prev = (temp);
//cout << "prev is " << prev->data <<endl;
temp = next;
//cout << "new temp is " << temp->data << endl;
}
temp->next = prev;
*head = temp;
//cout << "head value is " << (*head)->data << endl;
}
void compare(node *head,node *head1)
{
node *t = head;
node *t1 = head1;
int flag = 0;
int count = 0;
while(t && t1) {
//cout << t->data << " " << t1->data << endl;
if(t->data == t1->data) {
t = t->next;
t1 = t1->next;
}else {
flag = 1;
break;
cout << "flag is " << flag << endl;
}
count++;
}
cout << "count is " << count << endl;
if(flag == 0) {
cout << "given list is a palindrome\n";
}else {
cout << "given list is not a palindrome\n";
}
}
int main()
{
node *head = NULL;
node *head1;
while(1) {
//create(&head,n);
int n;
cin >> n;
if(n == 999) {
break;
}else {
create(&head,n);
}
}
print(head);
cout << endl;
head1 = head;
reverse(&head);
print(head);
cout << endl;
compare(head,head1);
return 0;
}