-
Notifications
You must be signed in to change notification settings - Fork 1k
/
SegregateOddEven.c
162 lines (131 loc) · 3.46 KB
/
SegregateOddEven.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
Code Description :
A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations.
It consists of nodes.A Node has 2 parts.
One is the data part and the other is the address part. Address part contains the address of the next or the
successor node and data part contains value of the node.
Through this C program,Nodes of a linked list are arranged such that the odd and the even nodes are segregated.
Here,all even nodes appear before all the odd nodes in the modified linked list.
*/
#include <stdio.h>
#include <malloc.h>
/* Defining the structure of a Node */
struct node
{
int data;
struct node *next;
};
/* Header to point to the first node and Last to point to the Last one */
struct node *header, *last;
/* Function Declarations */
void SegregateOddEven();
void create();
void show();
/* Driver Function */
void main()
{
int i, num;
printf("Enter the number of nodes : ");
scanf("%d", &num);
for (i = 1; i <= num; i++)
{
create();
}
printf("\nLinked List is : \n");
show();
SegregateOddEven();
printf("\nLinked List after calling SegregateOddEven() : \n");
show();
}
/* Function for Creation of a Linked List */
void create()
{
struct node *temp = (struct node*) malloc(sizeof(struct node));
printf("Enter value of Node: ");
scanf("%d", &temp->data);
temp->next= NULL;
if (header == NULL)
{
header = temp;
last = temp;
return;
}
else
{
last->next = temp;
last = temp;
return;
}
}
/* Function to Display Nodes of a Linked List */
void show()
{
struct node *temp = header;
while (temp != NULL)
{
printf("-->%d", temp->data);
temp = temp->next;
}
}
/* Function to Segregate Even and Odd Nodes of a Linked List */
void SegregateOddEven()
{
struct node *temp = header, *p1 = last, *next_node = NULL, *prev;
/* Traversing of Nodes till the second last node */
while (temp != p1)
{
/* If the data of node is ODD */
if (temp->data % 2 != 0)
{
/* If the first node is odd,set prev as header and then update header to the next node */
if (temp == header)
{
prev = header;
header = header->next;
}
/* Store value of current odd node's next in next_node */
next_node = temp->next;
/* Update previous odd node's next to next_node */
prev->next = next_node;
/* Make current odd node as the last node of the list */
last->next = temp;
/* Make the last node to point to NULL */
temp->next = NULL;
/* Make last equal to current odd node */
last = temp;
/* Update current pointer to next of the moved node */
temp = next_node;
}
/* If data of node is EVEN */
else
{
prev = temp;
temp = temp->next;
}
}
/* If the last node is ODD */
if (p1->data % 2 != 0)
{
next_node = temp->next;
prev->next = next_node;
last->next = temp;
temp->next = NULL;
last = temp;
}
}
/*
COMPLEXITY:
Space Complexity : O(n)
Time Complexity : O(n)
OUTPUT:
Enter the number of nodes : 5
Enter value of Node: 1
Enter value of Node: 6
Enter value of Node: 8
Enter value of Node: 7
Enter value of Node: 9
Linked List is :
-->1-->6-->8-->7-->9
Linked List after calling SegregateOddEven() :
-->6-->8-->1-->7-->9
*/