Shift vowels towards the end of a Single Linked List We have a linklist which is made of alphabets. We have to shift the vowels towards the end of the linklist, without changing the order in which they appear. So,

A-&gt;C-&gt;E-&gt;F-&gt;G-&gt;O


should become

C-&gt;F-&gt;G-&gt;A-&gt;E-&gt;O


This is the code I have written

#include&lt;iostream&gt;

using namespace std;

struct node
{
    char ch;
    node *next;
};

void enqueue (node **head, node **tail, char val)
{
    node *newn = (node *)malloc(sizeof(node));
    newn-&gt;ch = val;
    newn-&gt;next = NULL;

    if (*head == NULL)
    {
        *head = newn;
        *tail = newn;
    }

    (*tail)-&gt;next = newn;
    (*tail) = newn;

}

void print (node *head)
{
    while (head!=NULL)
    {
        cout&lt;&lt;head-&gt;ch&lt;&lt;" ";
        head = head-&gt;next;
    }
    cout&lt;&lt;endl;
}

bool isVowel (char ch)
{
    ch = ch | 32;

    if (ch == 'a' || ch =='e' || ch=='i' || ch=='o' || ch=='u')
        return true;
    return false;

}



node* segregateVowels (node *head, node *tail)
{
    if (head == NULL || head-&gt;next == NULL)
        return head;


    node *temp = head;
    node *fin = tail;

    while (temp-&gt;next!=fin)
    {
        cout&lt;&lt;temp-&gt;ch&lt;&lt;" "&lt;&lt;fin-&gt;ch&lt;&lt;endl;
        if (isVowel(temp-&gt;next-&gt;ch))
        {
            node *shift = temp-&gt;next;
            temp-&gt;next = temp-&gt;next-&gt;next;
            tail-&gt;next = shift;
            shift-&gt;next = NULL;
            tail = shift;
        }
        else
            temp = temp-&gt;next;

    }

    if (temp-&gt;next!=NULL &amp;&amp; isVowel(temp-&gt;next-&gt;ch))   /* Handling the case where the last element is a vowel   */
    {
        node *toMove = temp-&gt;next;
        temp-&gt;next = temp-&gt;next-&gt;next;
        tail-&gt;next = toMove;
        toMove-&gt;next = NULL;

    }
    else
        temp = temp-&gt;next;

    if (isVowel(head-&gt;ch))                  /*  Handling the case where the first element is a vowel    */
    {
        node *toMove = head;
        head = head-&gt;next;
        toMove-&gt;next = temp-&gt;next;
        temp-&gt;next = toMove;
    }

    return head;

}


int main()
{
    srand(time(NULL));

    node  *head = NULL, *tail = NULL;

    int i = 20;

    while (i&gt;=0)
    {
        enqueue (&amp;head, &amp;tail, rand()%26+65);
        i--;
    }

    print(head);


    head = segregateVowels (head, tail);


    print(head);
}


Although this does seem to give me correct results, but I have to explicitly check for two edge cases. Also I feel there is a better solution for this problem or a better approach which I seem to be missing. The problem can be handled by creating two separate Link Lists too, and then joining them together. But what modifications do you suggest in the above program?
Don't do using namespace std

Here's a SO question which should provide you more information about this.

For loops are here to help you

You could write

int i = 20;
while (i&gt;=0)
{
    enqueue (&amp;head, &amp;tail, rand()%26+65);
    i--;
}


in a much clearer way using a for-loop : for (int i=20; i&gt;=0; i--) or for (int i=0; i&lt;21; i++). I prefer the latter because I tend to count forward and not backward and it makes much clearer the fact that you want 21 iterations.

Avoid boilerplate and useless code

if (ch == 'a' || ch =='e' || ch=='i' || ch=='o' || ch=='u')
    return true;
return false;


could be simply written :

return (ch == 'a' || ch =='e' || ch=='i' || ch=='o' || ch=='u');


Avoid magic numbers

Even if I can guess them, I don't want to learn the ascii code for the different characters.
You could rewrite enqueue (&amp;head, &amp;tail, rand()%26+65); in a more explicit way : enqueue (&amp;head, &amp;tail, rand()%26+'A');

Be consistent with your brackets

Using brackets for single statement is a personal preference but if you do use them for the then-block, then please use them for the else-block as it makes things look "balanced".

Add a bit of documentation
I know it's tedious and not so interesting and I usually try to avoid to ask for it but here, I am getting a but confused by the two functions taking two lists as parameters and trying to understand what is intended for both of them.

This being said, I have to go and I'll try to have a deeper look later on.

In the meantime, here's your code with the comments above taken into account :

#include&lt;iostream&gt;

struct node
{
    char ch;
    node *next;
};

void enqueue (node **head, node **tail, char val)
{
    node *newn = (node *)malloc(sizeof(node));
    newn-&gt;ch = val;
    newn-&gt;next = NULL;

    if (*head == NULL)
    {
        *head = newn;
        *tail = newn;
    }

    (*tail)-&gt;next = newn;
    (*tail) = newn;
}

void print (node *head)
{
    while (head!=NULL)
    {
        std::cout&lt;&lt;head-&gt;ch&lt;&lt;" ";
        head = head-&gt;next;
    }
    std::cout&lt;&lt;std::endl;
}

bool isVowel (char ch)
{
    ch |= 32; // make it lowercase
    return (ch == 'a' || ch =='e' || ch=='i' || ch=='o' || ch=='u');
}

node* segregateVowels (node *head, node *tail)
{
    if (head == NULL || head-&gt;next == NULL)
        return head;

    node *temp = head;
    node *fin = tail;

    while (temp-&gt;next!=fin)
    {
        std::cout&lt;&lt;temp-&gt;ch&lt;&lt;" "&lt;&lt;fin-&gt;ch&lt;&lt;std::endl;
        if (isVowel(temp-&gt;next-&gt;ch))
        {
            node *shift = temp-&gt;next;
            temp-&gt;next = temp-&gt;next-&gt;next;
            tail-&gt;next = shift;
            shift-&gt;next = NULL;
            tail = shift;
        }
        else
        {
            temp = temp-&gt;next;
        }
    }

    if (temp-&gt;next!=NULL &amp;&amp; isVowel(temp-&gt;next-&gt;ch))   /* Handling the case where the last element is a vowel   */
    {
        node *toMove = temp-&gt;next;
        temp-&gt;next = temp-&gt;next-&gt;next;
        tail-&gt;next = toMove;
        toMove-&gt;next = NULL;

    }
    else
    {
        temp = temp-&gt;next;
    }

    if (isVowel(head-&gt;ch))                  /*  Handling the case where the first element is a vowel    */
    {
        node *toMove = head;
        head = head-&gt;next;
        toMove-&gt;next = temp-&gt;next;
        temp-&gt;next = toMove;
    }

    return head;
}


int main()
{
    srand(time(NULL));

    node  *head = NULL, *tail = NULL;

    for (int i=0; i&lt;21; i++)
    {
        enqueue (&amp;head, &amp;tail, rand()%26+'A');
    }

    print(head);

    head = segregateVowels (head, tail);

    print(head);
}