forked from rk2279709/DSA-Linked-List-Assignment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ5.c
109 lines (109 loc) · 2.13 KB
/
Q5.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
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
108
109
#include<stdio.h>
#include<malloc.h>
struct node
{
int info;
struct node *next;
}*start = NULL,*last = NULL;
int num = 0;
void insert()
{
struct node *t = (struct node*)malloc(sizeof(struct node));
printf("Enter Data: ");
scanf("%d",&t->info);
t->next = NULL;
if(start==NULL)
{
start = t;
last = t;
num++;
}
else
{
last->next = t;
last = t;
num++;
}
}
void display()
{
if(start ==NULL)
{
printf("LL is Empty!\n");
return;
}
struct node *t = start;
while(t!=NULL)
{
printf("%d ",t->info);
t = t->next;
}
printf("\n");
}
void checkpalin()
{
if(start!=NULL)
{
int flag = 0;
int n = num;
struct node *chk1,*chk2;
chk1 = start;
if(num%2==0)
{
while(n>0)
{
chk2 = chk1;
for(int i=1;i<n;i++)
chk2 = chk2->next;
if(chk1->info == chk2->info)
{
chk1 = chk1->next;
n-=2;
}
else
{
flag = 1;
break;
}
}
}
else
{
while(n>1)
{
chk2 = chk1;
for(int i=1;i<n;i++)
chk2 = chk2->next;
if(chk1->info == chk2->info)
{
chk1 = chk1->next;
n-=2;
}
else
{
flag = 1;
break;
}
}
}
if(flag == 0)
printf("LL is a palindrome!\n");
else
printf("LL is not a palindrome!\n");
}
else
printf("LL is Empty!\n");
}
void main()
{
checkpalin();
insert();
insert();
insert();
insert();
insert();
insert();
insert();
display();
checkpalin();
}