From bd82854745b5048552b22431ceb0b8d9ca5cee5c Mon Sep 17 00:00:00 2001 From: riddhi <91315409+Riddhi12349@users.noreply.github.com> Date: Sat, 2 Nov 2024 23:10:27 +0530 Subject: [PATCH 1/4] Create program.c --- .../program.c | 125 ++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 Linked_list/Segregate_Even_and_Odd_(numeric)_node/program.c diff --git a/Linked_list/Segregate_Even_and_Odd_(numeric)_node/program.c b/Linked_list/Segregate_Even_and_Odd_(numeric)_node/program.c new file mode 100644 index 00000000..2ad50d8f --- /dev/null +++ b/Linked_list/Segregate_Even_and_Odd_(numeric)_node/program.c @@ -0,0 +1,125 @@ +#include +#include + +struct Node { + int data; + struct Node* next; +}; + +// Function to create a new node +struct Node* newNode(int x) { + struct Node* temp = + (struct Node*)malloc(sizeof(struct Node)); + temp->data = x; + temp->next = NULL; + return temp; +} + +// Function to segregate even and odd nodes +// and return the head of the new list +struct Node* segregateEvenOdd(struct Node* head) { + + // Result list to hold even nodes + struct Node* resStart = NULL; + struct Node* resEnd = NULL; + + // Pointers for the original list + struct Node* curr = head; + struct Node* prev = NULL; + + // Move all even nodes from original to result + while (curr != NULL) { + + // If current node is even + if (curr->data % 2 == 0) { + + // Remove the current even node + // from the original list + if (prev != NULL) { + prev->next = curr->next; + } else { + // If the even node is at the head + head = curr->next; + } + + // Add the current even node to the result list + if (resStart == NULL) { + resStart = curr; + resEnd = resStart; + } else { + resEnd->next = curr; + resEnd = resEnd->next; + } + + curr = curr->next; + } else { + // If the node is odd, just move to the next + prev = curr; + curr = curr->next; + } + } + + // If there are no even nodes, return the original list + if (resStart == NULL) { + return head; + } + + // Append the remaining original list + // (odd nodes) to the result list + resEnd->next = head; + + // Return the result list (starting with even nodes) + return resStart; +} + +// Helper function to insert nodes at the end of the list +void insertAtEnd(struct Node **head_ref, int new_data) { + struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); + new_node->data = new_data; + + if (*head_ref == NULL) { + new_node->next = new_node; + *head_ref = new_node; + return; + } + + struct Node* temp = *head_ref; + while (temp->next != *head_ref) { + temp = temp->next; + } + + temp->next = new_node; + new_node->next = *head_ref; +} + +// Function to print the linked list +void printList(struct Node* node) { + while (node != NULL) { + printf("%d ", node->data); + node = node->next; + } + printf("\n"); +} + +int main() { + // Create a sample linked list: head->1->4->6->9->10->11 + struct Node* head = NULL; + + // Insert elements into the list + insertAtEnd(&head, 1); + insertAtEnd(&head, 4); + insertAtEnd(&head, 6); + insertAtEnd(&head, 9); + insertAtEnd(&head, 10); + insertAtEnd(&head, 11); + + printf("Original Linked list: "); + printList(head); + + head = segregateEvenOdd(head); + + printf("Modified Linked list: "); + printList(head); + + return 0; +} From ab86a774df251ef2a56dd53882c3eca62cd6b196 Mon Sep 17 00:00:00 2001 From: riddhi <91315409+Riddhi12349@users.noreply.github.com> Date: Sat, 2 Nov 2024 23:15:11 +0530 Subject: [PATCH 2/4] Create readme.md --- .../readme.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Linked_list/Segregate_Even_and_Odd_(numeric)_node/readme.md diff --git a/Linked_list/Segregate_Even_and_Odd_(numeric)_node/readme.md b/Linked_list/Segregate_Even_and_Odd_(numeric)_node/readme.md new file mode 100644 index 00000000..8dcc8db0 --- /dev/null +++ b/Linked_list/Segregate_Even_and_Odd_(numeric)_node/readme.md @@ -0,0 +1,36 @@ +# Segregate Even and Odd Nodes in a Linked List + +This project provides a C program to segregate even and odd nodes in a singly linked list. The program separates all even nodes and moves them to the beginning of the list while retaining the relative order of the odd nodes that follow. + +## Code Structure + +1. **Node Structure** + - Each node has: + - `data`: Stores the integer value of the node. + - `next`: Pointer to the next node in the list. + +2. **Functions** + - `newNode`: Creates a new node with a specified integer value. + - `segregateEvenOdd`: Segregates even and odd nodes in the list, placing all even nodes at the start of the list followed by the odd nodes. Returns the head of the modified list. + - `printList`: Prints the entire linked list. + +3. **Main Function** + - Creates a sample linked list and displays it. + - Calls `segregateEvenOdd` to segregate even and odd nodes. + - Displays the modified list. + +## Usage + +To compile and run the code, use the following commands: + +```bash +gcc segregate_even_odd.c -o segregate_even_odd +./segregate_even_odd + +``` +## Example Output + +The program will output the original list and the modified list with even nodes first: + +Original Linked list: 1 4 6 9 10 11 +Modified Linked list: 4 6 10 1 9 11 From 815c5b5819d39528fe8fe04abc9df216bbedf5b3 Mon Sep 17 00:00:00 2001 From: riddhi <91315409+Riddhi12349@users.noreply.github.com> Date: Sat, 2 Nov 2024 23:17:03 +0530 Subject: [PATCH 3/4] Update readme.md --- Linked_list/Segregate_Even_and_Odd_(numeric)_node/readme.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Linked_list/Segregate_Even_and_Odd_(numeric)_node/readme.md b/Linked_list/Segregate_Even_and_Odd_(numeric)_node/readme.md index 8dcc8db0..bc038413 100644 --- a/Linked_list/Segregate_Even_and_Odd_(numeric)_node/readme.md +++ b/Linked_list/Segregate_Even_and_Odd_(numeric)_node/readme.md @@ -13,7 +13,8 @@ This project provides a C program to segregate even and odd nodes in a singly li - `newNode`: Creates a new node with a specified integer value. - `segregateEvenOdd`: Segregates even and odd nodes in the list, placing all even nodes at the start of the list followed by the odd nodes. Returns the head of the modified list. - `printList`: Prints the entire linked list. - + - `insertAtEnd` : Inserts the Linked list node at the end of the list. + 3. **Main Function** - Creates a sample linked list and displays it. - Calls `segregateEvenOdd` to segregate even and odd nodes. From 309f80da4750d1006a71489e7b2662ec8c8d725f Mon Sep 17 00:00:00 2001 From: riddhi <91315409+Riddhi12349@users.noreply.github.com> Date: Sat, 2 Nov 2024 23:17:30 +0530 Subject: [PATCH 4/4] Update readme.md --- Linked_list/Segregate_Even_and_Odd_(numeric)_node/readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Linked_list/Segregate_Even_and_Odd_(numeric)_node/readme.md b/Linked_list/Segregate_Even_and_Odd_(numeric)_node/readme.md index bc038413..dd5718e7 100644 --- a/Linked_list/Segregate_Even_and_Odd_(numeric)_node/readme.md +++ b/Linked_list/Segregate_Even_and_Odd_(numeric)_node/readme.md @@ -33,5 +33,5 @@ gcc segregate_even_odd.c -o segregate_even_odd The program will output the original list and the modified list with even nodes first: -Original Linked list: 1 4 6 9 10 11 -Modified Linked list: 4 6 10 1 9 11 +- Original Linked list: 1 4 6 9 10 11 +- Modified Linked list: 4 6 10 1 9 11