-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20051761palindrome.c
82 lines (80 loc) · 1.68 KB
/
20051761palindrome.c
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
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
int Palindrome();
struct node
{
int data;
struct node* next;
};
struct node* start = NULL;
int main()
{
int n,i,a;
printf("\n Enter Total no. of nodes: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter the Linked List data %d: ",i);
scanf("%d",&a);
struct node *ptr;
struct node *new=(struct node *)malloc(sizeof(struct node));
new->next=NULL;
new->data=a;
if(start==NULL)
{
start=new;
ptr=start;
}
else
{
ptr->next=new;
ptr=new;
}
}
struct node* temp;
if (start == NULL)
printf("\nList is empty\n");
else
{
temp = start;
while (temp != NULL)
{
printf(" Data : %d\n",temp->data);
temp = temp->next;
}
}
if (Palindrome())
printf("The linked list is a palindrome\n");
else
printf("The linked list is not a palindrome\n");
}
int Palindrome()
{
struct node *prev, *rev ,*next, *temp;
prev = next = NULL;
temp = start;
if (temp == NULL)
printf(" !!! Linked List is empty !!!\n");
else
{
while (temp != NULL)
{
next = temp->next;
temp->next = prev;
prev = temp;
temp = next;
}
rev=prev;
while (rev != NULL && start != NULL)
{
if(start->data == rev->data)
{
rev = rev->next;
start = start->next;
}
else
return 0;
}return 1;
}
}