-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linkedlist.cpp
107 lines (98 loc) · 1.92 KB
/
Linkedlist.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
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
using namespace std;
struct Node
{
int data;
Node* next;
};
void initNode(struct Node* head, int x)
{
head->data = x;
head->next = NULL;
}
void addNode(struct Node* head, int x)//add to the end
{
Node *newNode = new Node;
newNode->data = x;
newNode->next = NULL;
Node *curNode = head;
while (curNode->next)
{
curNode = curNode->next;
}
curNode->next = newNode;
}
void addNode2front(struct Node** head, int x)//change the head
{
Node *newNode = new Node;
newNode->next = *head;
newNode->data = x;
*head = newNode;
}
int printList(struct Node* head)
{
int length = 0;
Node *curNode = head;
while (curNode)
{
length++;
cout << curNode->data << "-> " ;
curNode = curNode->next;
}
cout << "NULL" << endl;
return length;
}
//Node *newHead = new Node; //New head of reverse list
Node* reverseList(struct Node* head)
{
Node *newHead = new Node;
newHead->data = head->data;//New head of reverseList
newHead->next = NULL;
Node *curNode = new Node;
curNode = head->next;
while (curNode)
{
addNode2front(&newHead,curNode->data);
curNode = curNode->next;
}
return newHead;
}
int checkPalindrome(struct Node* head, struct Node* newhead, int length)
{
int check = 0;
int i;
for (i=1;i<=length/2;i++)
{
if (head->data!=newhead->data)
{
return check;
break;
}
head = head->next;
newhead = newhead->next;
}
check = 1;
return check;
}
int main()
{
Node *head = new Node;
initNode(head, 2);
addNode(head, 1);
addNode(head, 1);
addNode(head, 2);
//addNode2front(&head, 3);
int length = printList(head);
Node *newhead = new Node;
newhead = reverseList(head);
printList(newhead);
int check;
check = checkPalindrome(head, newhead, length);
if (check==1)
cout << "Palindrome" << endl;
else
cout << "Not Palindrome" << endl;
//cout << newhead->data << endl;
getchar();
return 0;
}