Intercambiar las vocales en la representación de lista enlazada de una string

Dada la representación de lista enlazada de una string, la tarea es intercambiar cada par de vocales subsiguientes en ella para producir la lista enlazada resultante.

Ejemplos:  

Entrada: Lista = g -> e -> e -> k -> s -> NULL 
Salida: g -> e -> e -> k -> s -> NULL 
Las únicas vocales son ‘e’ y ‘e’ que 
se intercambiarán entre sí.

Entrada: Lista = a -> b -> c -> d -> e -> NULL 
Salida: e -> b -> c -> d -> a -> NULL 
intercambio (a, e) 
 

Acercarse: 

  • Use dos punteros del tipo de Node de lista enlazada para almacenar el Node con la primera vocal y para recorrer la lista enlazada respectivamente.
  • Comience a recorrer la lista enlazada hasta el final.
  • Si se encuentra una vocal y el primer puntero ya contiene un Node con una vocal en su parte de datos, entonces intercambie los contenidos del Node actual y el primer Node y establezca el primer puntero en NULL.
  • En caso de que se encuentre una vocal y el primer puntero no contenga nada, configure el primer puntero para que apunte a este Node.
  • En caso de que no se encuentre la vocal, continúe el recorrido.

A continuación se muestra la implementación del enfoque anterior:  

C++

// 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;
}

Java

// 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

Python3

# Python3 implementation of the approach
 
# Structure to hold the data and
# the address of next element
class newNode:
     
    def __init__(self, data):
         
        self.data = data
        self.next = None
 
# Utility function to add a new
# node to the linked list
def add(data):
     
    newnode = newNode(data)
    return newnode
 
# Function to add the given character
# in the string to the linked list
def addinll(head, data):
     
    # If the linked list is empty then add
    # it to the head otherwise add
    # the character to the end
    if (head == None):
        head = add(data)
    else:
        curr = head
         
        while (curr.next != None):
            curr = curr.next
             
        curr.next = add(data)
         
    return head
 
# Function to print the linked list
def print1(head):
     
    curr = head
     
    while (curr != None):
        print(curr.data,end = " -> ")
        curr = curr.next
         
    print("Null")
 
# Function that returns true if
# the character is vowel
def isvowel(data):
     
    if (data == 'a' or data == 'e' or
        data == 'i' or data == 'o' or
        data == 'u'):
        return True
    if (data == 'A' or data == 'E' or
        data == 'I' or data == 'O' or
        data == 'U'):
        return True
         
    return False
 
# Function to reverse the vowels in the
# linked list
def reversevowels(head):
     
    # The pointer named first holds the
    # address of the node containing
    # the first vowel
    first = None
 
    # The pointer curr is used
    # to point to head
    curr = head
 
    # Traverse the linked list
    while (curr != None):
 
        # If a vowel is encountered and first is
        # set to None then update the first
        # with the address of this node
        if (isvowel(curr.data) and first == None):
            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
        elif (isvowel(curr.data) and first != None):
            temp = first.data
            first.data = curr.data
            curr.data = temp
             
            # Set the first pointer to None
            first = None
             
        curr = curr.next
 
# Driver code
if __name__ == '__main__':
     
    s = "geeks"
 
    # Start with an empty linked list
    head = None
 
    # Add the characters one by
    # one to the linked list
    for i in range(len(s)):
        head = addinll(head, s[i])
 
    # Reverse the vowels
    # in the linked list
    reversevowels(head)
 
    # Print the linked list
    # with reversed vowels
    print1(head)
 
# This code is contributed by SURENDRA_GANGWAR

C#

// 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

Javascript

<script>
      // JavaScript implementation of the approach
      // Structure to hold the data and
      // the address of next element
      class node {
        constructor() {
          this.data = 0;
          this.next = null;
        }
      }
 
      // Utility function to add a new
      // node to the linked list
      function add(data) {
        var newnode = new node();
        newnode.data = data;
        newnode.next = null;
        return newnode;
      }
 
      // Function to add the given
      // character in the String to
      // the linked list
      function addinll(head, 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 {
          var curr = head;
          while (curr.next != null) curr = curr.next;
          curr.next = add(data);
        }
        return head;
      }
 
      // Function to print the linked list
      function print(head) {
        var curr = head;
        while (curr != null) {
          document.write(curr.data + " -> ");
          curr = curr.next;
        }
        document.write("NULL");
      }
 
      // Function that returns true if
      // the character is vowel
      function isvowel(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
      function reversevowels(head) {
        // The pointer named first
        // holds the address of the
        // node containing the first vowel
        var first = null;
 
        // The pointer curr is used
        // to point to head
        var 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;
        }
      }
 
      function swap(data, data2) {
        // Auto-generated method stub
        var newdata = data;
        data = data2;
        data2 = newdata;
      }
 
      // Driver code
      var s = "geeks";
 
      // Start with an empty linked list
      var head = null;
 
      // Add the characters one by
      // one to the linked list
      for (var 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 is contributed by rdtank.
    </script>
Producción: 

g -> e -> e -> k -> s -> NULL

 

Publicación traducida automáticamente

Artículo escrito por code_freak y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *