Swap the vowels in the linked list representation of a string

Given the linked list representation of a string, the task is to swap every subsequent pair of vowels in it to produce the resultant linked list.
Examples:  

Input: List = g -> e -> e -> k -> s -> NULL 
Output: g -> e -> e -> k -> s -> NULL 
The only vowels are ‘e’ and ‘e’ which 
will be swapped with each other.
Input: List = a -> b -> c -> d -> e -> NULL 
Output: e -> b -> c -> d -> a -> NULL 
swap(a, e) 
 

Approach: 

Below is the implementation of the above approach:  

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <iostream>
using namespace std;
 
// Structure to hold the data and
// the address of next element
struct node {
    char data;
    node* next;
};
 
// Utility function to add a new
// node to the linked list
node* add(char data)
{
    node* newnode = new node;
    newnode->data = data;
    newnode->next = NULL;
    return newnode;
}
 
// Function to add the given character
// in the string to the linked list
node* addinll(node* head, char data)
{
 
    // If the linked list is empty then add
    // it to the head otherwise add
    // the character to the end
    if (head == NULL)
        head = add(data);
    else {
        node* curr = head;
        while (curr->next != NULL)
            curr = curr->next;
        curr->next = add(data);
    }
    return head;
}
 
// Function to print the linked list
void print(node* head)
{
    node* curr = head;
    while (curr != NULL) {
        cout << curr->data << " -> ";
        curr = curr->next;
    }
    cout << "NULL";
}
 
// Function that returns true if
// the character is vowel
bool isvowel(char data)
{
    if (data == 'a' || data == 'e'
        || data == 'i' || data == 'o'
        || data == 'u')
        return true;
    if (data == 'A' || data == 'E'
        || data == 'I' || data == 'O'
        || data == 'U')
        return true;
    return false;
}
 
// Function to reverse the vowels in the linked list
void reversevowels(node* head)
{
 
    // The pointer named first holds the
    // address of the node containing
    // the first vowel
    node* first = NULL;
 
    // The pointer curr is used
    // to point to head
    node* curr = head;
 
    // Traverse the linked list
    while (curr != NULL) {
 
        // If a vowel is encountered and first is
        // set to NULL then update the first
        // with the address of this node
        if (isvowel(curr->data) and first == NULL)
            first = curr;
 
        // If a vowel is encountered and the first
        // already contains the address of a vowel
        // then swap the current and the first's data
        else if (isvowel(curr->data) and first != NULL) {
            swap(first->data, curr->data);
 
            // Set the first pointer to NULL
            first = NULL;
        }
        curr = curr->next;
    }
}
 
// Driver code
int main()
{
    string s = "geeks";
 
    // Start with an empty linked list
    node* head = NULL;
 
    // Add the characters one by
    // one to the linked list
    for (int i = 0; i < s.size(); i++)
        head = addinll(head, s[i]);
 
    // Reverse the vowels
    // in the linked list
    reversevowels(head);
 
    // Print the linked list
    // with reversed vowels
    print(head);
 
    return 0;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG{
 
// Structure to hold the data and
// the address of next element
static class node
{
    char data;
    node next;
};
 
// Utility function to add a new
// node to the linked list
static node add(char data)
{
    node newnode = new node();
    newnode.data = data;
    newnode.next = null;
    return newnode;
}
 
// Function to add the given character
// in the String to the linked list
static node addinll(node head, char data)
{
 
    // If the linked list is empty then add
    // it to the head otherwise add
    // the character to the end
    if (head == null)
        head = add(data);
    else
    {
        node curr = head;
        while (curr.next != null)
            curr = curr.next;
        curr.next = add(data);
    }
    return head;
}
 
// Function to print the linked list
static void print(node head)
{
    node curr = head;
    while (curr != null)
    {
        System.out.print(curr.data + " -> ");
        curr = curr.next;
    }
    System.out.print("NULL");
}
 
// Function that returns true if
// the character is vowel
static boolean isvowel(char data)
{
    if (data == 'a' || data == 'e' ||
        data == 'i' || data == 'o' ||
        data == 'u')
        return true;
    if (data == 'A' || data == 'E' ||
        data == 'I' || data == 'O' ||
        data == 'U')
        return true;
    return false;
}
 
// Function to reverse the vowels in the linked list
static void reversevowels(node head)
{
 
    // The pointer named first holds the
    // address of the node containing
    // the first vowel
    node first = null;
 
    // The pointer curr is used
    // to point to head
    node curr = head;
 
    // Traverse the linked list
    while (curr != null)
    {
 
        // If a vowel is encountered and first is
        // set to null then update the first
        // with the address of this node
        if (isvowel(curr.data) && first == null)
            first = curr;
 
        // If a vowel is encountered and the first
        // already contains the address of a vowel
        // then swap the current and the first's data
        else if (isvowel(curr.data) && first != null)
        {
            swap(first.data, curr.data);
 
            // Set the first pointer to null
            first = null;
        }
        curr = curr.next;
    }
}
 
private static void swap(char data, char data2)
{
    // Auto-generated method stub
    char newdata = data;
    data = data2;
    data2 = newdata;
     
}
 
// Driver code
public static void main(String[] args)
{
    String s = "geeks";
 
    // Start with an empty linked list
    node head = null;
 
    // Add the characters one by
    // one to the linked list
    for(int i = 0; i < s.length(); i++)
       head = addinll(head, s.charAt(i));
 
    // Reverse the vowels
    // in the linked list
    reversevowels(head);
 
    // Print the linked list
    // with reversed vowels
    print(head);
}
}
 
// This code is contributed by gauravrajput1
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
class GFG{
 
// Structure to hold the data and
// the address of next element
public class node
{
  public char data;
  public node next;
};
 
// Utility function to add a new
// node to the linked list
static node add(char data)
{
  node newnode = new node();
  newnode.data = data;
  newnode.next = null;
  return newnode;
}
 
// Function to add the given
// character in the String to
// the linked list
static node addinll(node head,
                    char data)
{
  // If the linked list is
  // empty then add it to the
  // head otherwise add the
  // character to the end
  if (head == null)
    head = add(data);
  else
  {
    node curr = head;
    while (curr.next != null)
      curr = curr.next;
    curr.next = add(data);
  }
  return head;
}
 
// Function to print the linked list
static void print(node head)
{
  node curr = head;
  while (curr != null)
  {
    Console.Write(curr.data + " -> ");
    curr = curr.next;
  }
  Console.Write("NULL");
}
 
// Function that returns true if
// the character is vowel
static bool isvowel(char data)
{
  if (data == 'a' || data == 'e' ||
      data == 'i' || data == 'o' ||
      data == 'u')
    return true;
  if (data == 'A' || data == 'E' ||
      data == 'I' || data == 'O' ||
      data == 'U')
    return true;
  return false;
}
 
// Function to reverse the vowels
// in the linked list
static void reversevowels(node head)
{
  // The pointer named first
  // holds the address of the
  // node containing the first vowel
  node first = null;
 
  // The pointer curr is used
  // to point to head
  node curr = head;
 
  // Traverse the linked list
  while (curr != null)
  {
    // If a vowel is encountered and first is
    // set to null then update the first
    // with the address of this node
    if (isvowel(curr.data) &&
        first == null)
      first = curr;
 
    // If a vowel is encountered and
    // the first already contains the
    // address of a vowel then swap the
    // current and the first's data
    else if (isvowel(curr.data) &&
             first != null)
    {
      swap(first.data, curr.data);
 
      // Set the first pointer to null
      first = null;
    }
    curr = curr.next;
  }
}
 
private static void swap(char data,
                         char data2)
{
  // Auto-generated method stub
  char newdata = data;
  data = data2;
  data2 = newdata;
     
}
 
// Driver code
public static void Main(String[] args)
{
    String s = "geeks";
 
    // Start with an empty linked list
    node head = null;
 
    // Add the characters one by
    // one to the linked list
    for(int i = 0; i < s.Length; i++)
       head = addinll(head, s[i]);
 
    // Reverse the vowels
    // in the linked list
    reversevowels(head);
 
    // Print the linked list
    // with reversed vowels
    print(head);
}
}
 
// This code contributed by gauravrajput1
chevron_right

Output: 
g -> e -> e -> k -> s -> NULL




 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.





Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : GauravRajput1

Article Tags :
Practice Tags :