-
Notifications
You must be signed in to change notification settings - Fork 0
/
ODD_EVEN_LLIST.C
128 lines (123 loc) · 2.81 KB
/
ODD_EVEN_LLIST.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
}*head=NULL,*temp,*endnode;
void insert(int item,int j,int n);
void segregate();
void print();
int main()
{
int n;
int item,t,j;
head=NULL;
endnode=NULL;
printf("Enter no. of elements: ");
scanf("%d",&n);
for(j=0;j<n;j++)
{
scanf("%d",&item);
insert(item,j,n);
}
segregate();
print();
printf("\n");
return 0;
}
void insert(int item,int j,int n)
{
if(head==NULL)
{
temp=(struct node*)malloc(sizeof(struct node));
temp->data=item;
temp->next=NULL;
head=temp;
}
else
{
temp->next=(struct node*)malloc(sizeof(struct node));
temp=temp->next;
temp->data=item;
temp->next=NULL;
if(j==n-1)
{
endnode=temp;
}
}
}
void segregate()
{
struct node *current,*prev=NULL;
struct node *end=endnode;
temp=NULL;
current=head;
while(current!=NULL)
{
if(current->data%2!=0)
{
if(current->next==NULL)
{
return;
}
else
{
current=current->next;
}
}
else
{
break;
}
}
current=head;
while(current!=end && current->data%2!=0)
{
temp=current;
current=current->next;
endnode->next=temp;
endnode->next->next=NULL;
endnode=endnode->next;
}
head=current;
if(current->data%2==0)
{
while(current!=end)
{
if(current->data%2==0)
{
prev=current;
current=current->next;
}
else
{
temp=current;
prev->next=current->next;
current=current->next;
endnode->next=temp;
endnode->next->next=NULL;
endnode=endnode->next;
}
}
}
else
{
prev=current;
}
if(endnode!=end && end->data%2!=0)
{
prev->next=end->next;
end->next=NULL;
endnode->next=end;
}
}
void print()
{
temp=head;
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
}