Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segregate even and odd (numeric) Linked List #1583

Merged
merged 4 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions Linked_list/Segregate_Even_and_Odd_(numeric)_node/program.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#include <stdio.h>
#include <stdlib.h>

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;
}
37 changes: 37 additions & 0 deletions Linked_list/Segregate_Even_and_Odd_(numeric)_node/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 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.
- `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.
- 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