-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOdd_Even_Doubly_Linked_List.c
119 lines (99 loc) · 2.85 KB
/
Odd_Even_Doubly_Linked_List.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
#include <stdio.h>
#include <stdlib.h>
// Node structure for the doubly linked list
typedef struct Node {
int data;
struct Node* prev;
struct Node* next;
} Node;
// Function to insert a new node at the end of the doubly linked list
void insert(Node** head, int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
} else {
Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
newNode->prev = current;
}
}
// Function to display all elements from the list at even places
void displayEvenPlaces(Node* head) {
int count = 1;
while (head != NULL) {
if (count % 2 == 0) {
printf("%d -> ", head->data);
}
count++;
head = head->next;
}
printf("NULL\n");
}
// Function to split the list into two lists (ODD and EVEN numbers)
void splitList(Node* head, Node** oddList, Node** evenList) {
while (head != NULL) {
if (head->data % 2 == 0) {
// Even number, add to evenList
insert(evenList, head->data);
} else {
// Odd number, add to oddList
insert(oddList, head->data);
}
head = head->next;
}
}
// Function to display elements of a doubly linked list
void displayList(Node* head) {
while (head != NULL) {
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
}
// Function to free the memory used by the doubly linked list
void freeList(Node** head) {
Node* current = *head;
Node* next;
while (current != NULL) {
next = current->next;
free(current);
current = next;
}
*head = NULL;
}
int main() {
// Create a sample doubly linked list
Node* head = NULL;
insert(&head, 1);
insert(&head, 2);
insert(&head, 3);
insert(&head, 4);
insert(&head, 5);
// Display the original doubly linked list
printf("Original List: ");
displayList(head);
// 1. Display all elements from the list at even places
printf("\nElements at Even Places: ");
displayEvenPlaces(head);
// 2. Split the list into two lists (ODD and EVEN numbers)
Node* oddList = NULL;
Node* evenList = NULL;
splitList(head, &oddList, &evenList);
// Display the ODD numbers list
printf("\nODD Numbers List: ");
displayList(oddList);
// Display the EVEN numbers list
printf("EVEN Numbers List: ");
displayList(evenList);
// Free the memory used by the doubly linked lists
freeList(&head);
freeList(&oddList);
freeList(&evenList);
return 0;
}