-
Notifications
You must be signed in to change notification settings - Fork 237
/
evenodd
90 lines (87 loc) · 1.82 KB
/
evenodd
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
/Even Odd according to position/
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *next;
} *start = NULL;
void create(int n)
{
if (n <= 0)
printf("U can't able to store LL");
else
{
struct node *ptr;
ptr = (struct node *)malloc(sizeof(struct node));
printf("Enter data: ");
scanf("%d", &ptr->info);
ptr->next = NULL;
start = ptr;
int i;
for (i = 1; i < n; i++)
{
ptr->next = (struct node *)malloc(sizeof(struct node));
ptr = ptr->next;
printf("Enter data: ");
scanf("%d", &ptr->info);
ptr->next = NULL;
}
printf("\n");
}
}
void EvenOddAccordingToPostion()
{
struct node *odd1, *odd2, *even1, *even2;
odd1 = start;
even1 = start->next;
if (odd1 == NULL || even1 == NULL || even1->next == NULL)
{
return;
}
else
{
odd2 = even1->next;
even2 = odd2->next;
while (even2 != NULL)
{
even1->next = even2;
odd2->next = even1;
odd1->next = odd2;
odd1 = odd1->next;
even1 = even2;
if (even2->next == NULL)
return;
else
odd2 = even2->next;
even2 = odd2->next;
}
if (odd2->next == NULL)
{
odd2->next = odd1->next;
even1->next = NULL;
odd1->next = odd2;
}
}
}
void display()
{
struct node *p;
p = start;
while (p != NULL)
{
printf("%d ", p->info);
p = p->next;
}
printf("\n\n");
}
int main()
{
int n;
printf("Enter total number of element ");
scanf("%d", &n);
create(n);
EvenOddAccordingToPostion();
display();
return 0;
}