Click here to Skip to main content
16,003,462 members
Articles / Programming Languages / C++
Tip/Trick

Move all Odd numbers after Even numbers in Singly Linked List

Rate me:
Please Sign up or sign in to vote.
3.50/5 (5 votes)
4 Dec 2019CPOL 6.7K   1  
Given a Singly Linked List, we have to modify it such that all Even numbers appear before Odd

Linked List in C++ Series

Given a singly linked list, we have to modify it such that all even numbers appear before odd numbers.
For example:

Given Linked List : 1, 2, 3, 4, 5, 6, 7
After Modification : 2, 4, 6, 1, 3, 5, 7

Move Odd numbers after Even numbers in a Singly Linked List

Background

From the above figure, we can see how the function will work.
While the head of the list is odd, the head is copied to an auxiliary node and element next to the head will become the new head. The auxiliary node is added after tail and tail is updated. Similarly, all odd value nodes are removed from their position and added after the tail of the list.

Here are the advance, getLastNode, isOdd and exchangeEvenOdd functions.

C#
static void advance(Node*& node)
{
	assert (node != nullptr);
	node = node->next;
}

Using this function, node advances to the next node.

C#
Node* getLastNode()
{
	Node *node = head;
	while (node->next != nullptr)
	node = node->next;
	return node;
}

This function returns the last node of the linked list.

C#
bool isOdd(int num)
{
	if (num % 2 != 0)
		return true;
	else
		return false;
}

This function checks whether the entered value is odd or even.

C#
void exchangeEvenOdd()
{
	Node *node = nullptr;
	Node *lastNodeToTest = getLastNode();
	Node *tail = lastNodeToTest;
	while (isOdd(head->data) == true)
	{
		node = head;
		advance(head);
		tail->next = node;
		advance(tail);
	}
	Node *tmp = head;
	Node *curr = head;

	while (tmp->next != lastNodeToTest)
	{
		if (isOdd(curr->next->data) == true)
		{
			node = curr->next;
			curr->next = node->next;
			tail->next = node;
			advance(tail);
		}
		else
		{
		//advance "curr" and "tmp" only when next node to it is even
			advance(curr);
			advance(tmp);
		}
	}
	if (isOdd(curr->next->data) == true && tmp->next == lastNodeToTest)
	{
		node = lastNodeToTest;
		curr->next = lastNodeToTest->next;
		tail->next = lastNodeToTest;
		advance(tail);
	}
	tail->next = nullptr;
	lastNodeToTest = nullptr;
	node = nullptr;
}

C++ Implementation

C#
#include <iostream>
#include <utility>
#include <cassert>

class LinkedList
{
    struct Node
    {
        int data;
        Node * next = nullptr;
        Node(int value)   : data(std::move(value)), next(nullptr) {}
    };
    Node *head;

  public:
    LinkedList() : head(nullptr) {}
    ~LinkedList()
    {
        Node *tmp = nullptr;
        while (head)
        {
            tmp = head;
            head = head->next;
            delete tmp;
        }
        head = nullptr;
    }

    void insert(int);
    void exchangeEvenOdd();
    void printList() const;

  private:
    static void advance(Node*& node)
    {
        assert (node != nullptr);
        node = node->next;
    }

    Node* getLastNode()
    {
        Node *node = head;
        while (node->next != nullptr)
              node = node->next;

        return node;
    }

    bool isOdd(int num)
    {
        if (num % 2 != 0)
            return true;
        else
            return false;
    }
};

void LinkedList::insert(int value)
{
    Node *node = new Node(std::move(value));
    Node *tmp = head;
    if (tmp == nullptr)
    {
        head = node;
    }
    else
    {
        tmp = getLastNode();
        tmp->next = node;
    }
}

void LinkedList::exchangeEvenOdd()
{
    Node *node = nullptr;
    Node *lastNodeToTest = getLastNode();
    Node *tail = lastNodeToTest;

    while (isOdd(head->data) == true)
    {
        node = head;
        advance(head);
        tail->next = node;
        advance(tail);
    }

    Node *tmp = head;
    Node *curr = head;

    while (tmp->next != lastNodeToTest)
    {
        if (isOdd(curr->next->data) == true)
        {
            node = curr->next;
            curr->next = node->next;
            tail->next = node;
            advance(tail);
        }
        else
        {
            //advance "curr" and "tmp" only when next node to it is even
            advance(curr);
            advance(tmp);
        }
    }

    if (isOdd(curr->next->data) == true && tmp->next == lastNodeToTest)
    {
        node = lastNodeToTest;
        curr->next = lastNodeToTest->next;
        tail->next = lastNodeToTest;
        advance(tail);
    }
    tail->next = nullptr;
    lastNodeToTest = nullptr;
    node = nullptr;
}

void LinkedList::printList() const
{
    if (head == nullptr)
    {
        std::cout << "Empty List \n";
        return;
    }

    Node *node = head;

    while (node != nullptr)
    {
        std::cout << node->data << " ";
        advance(node);
    }

    std::cout << "\n";
}

int main()
{
    LinkedList ll1;
    ll1.insert(1);
    ll1.insert(2);
    ll1.insert(3);
    ll1.insert(4);
    ll1.insert(5);
    ll1.insert(6);
    ll1.insert(7);
    std::cout << "Original List : ";
    ll1.printList();

    ll1.exchangeEvenOdd();
    std::cout << "New List : ";
    ll1.printList();
}</cassert></utility></iostream>

History

  • 4th December, 2019: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --