📜  从链表中删除元音的程序

📅  最后修改于: 2021-09-06 06:17:42             🧑  作者: Mango

给定一个单链表,任务是从给定的链表中删除元音。

例子:

方法:
我们可以在给定链表中找到元音的三种情况:

  • 在链表的开头:要从链表的开头删除元音,请将头节点移动到链表中出现的第一个辅音。
    例如:
For Linked List:
a -> e -> i -> a -> c -> r -> d -> NULL
After moving the head Node from Node a to Node c,  We have 
c -> r -> d -> NULL
  • 在链表的中间:为了从链表的中间删除元音,想法是在元音节点之前保留在链表中找到的最后一个辅音的标记,并将该节点的下一个链接更改为下一个在元音 Nodes之后的链表中找到的辅音 Node 。
    例如:
For Linked List:
c -> r -> d -> a -> e -> i -> a -> c -> r -> z -> NULL
last consonant before vowels {a, e, i, a} is d
and next consonant after vowels {a, e, i, a} is r
After linking next pointer of Node d to Node r, We have 
c -> r -> d -> r -> z -> NULL
  • 在链表的末尾:为了从链表的末尾删除元音,想法是在元音节点之前保留在链表中找到的最后一个辅音的标记,并将该节点的下一个链接更改为 NULL。
    例如:
For Linked List:
c -> r -> d -> a -> e -> i -> a -> NULL
last consonant before vowels {a, e, i, a} is d
After changing the next link of Node  to NULL, We have 
c -> r -> d -> NULL

下面是上述方法的实现:

C++
// C++ program to remove vowels
// Nodes in a linked list
#include 
using namespace std;
 
// A linked list node
struct Node {
    char data;
    struct Node* next;
};
 
// Head Node
struct Node* head;
 
// Function to add new node to the
// List
Node* newNode(char key)
{
    Node* temp = new Node;
    temp->data = key;
    temp->next = NULL;
    return temp;
}
 
// Utility function to print the
// linked list
void printlist(Node* head)
{
    if (!head) {
        cout << "Empty List\n";
        return;
    }
    while (head != NULL) {
        cout << head->data << " ";
        if (head->next)
            cout << "-> ";
        head = head->next;
    }
    cout << endl;
}
 
// Utility function for checking vowel
bool isVowel(char x)
{
    return (x == 'a' || x == 'e' || x == 'i'
            || x == 'o' || x == 'u' || x == 'A'
            || x == 'E' || x == 'I' || x == 'O'
            || x == 'U');
}
 
// Function to remove the vowels Node
void removeVowels()
{
    // Node pointing to head Node
    struct Node* ptr = head;
 
    // Case 1 : Remove the trailing
    // vowels
    while (ptr != NULL) {
 
        // If current Node is a vowel
        // node then move the pointer
        // to next node
        if (isVowel(ptr->data))
            ptr = ptr->next;
 
        // Else break if a consonant
        // node is found
        else
            break;
    }
 
    // This prev node used to link
    // prev consonant to next
    // consonant after vowels
    struct Node* prev = ptr;
 
    // Head points to the first
    // consonant of the linked list
    head = ptr;
 
    ptr = ptr->next;
 
    // Case 2: If vowels found in
    // between of the linked list
    while (ptr != NULL) {
 
        // If current node is vowel
        if (isVowel(ptr->data)) {
 
            // Move ptr to the next
            // node
            ptr = ptr->next;
 
            // Check for vowels
            // occurring continuously
            while (ptr != NULL) {
 
                // If ptr is a vowel
                // move to next pointer
                if (isVowel(ptr->data)) {
                    ptr = ptr->next;
                }
                // Else break if
                // consonant found
                else
                    break;
            }
 
            // Case 3: If we have ending
            // vowels then link the prev
            // consonant to NULL
            if (ptr == NULL) {
                prev->next = NULL;
                break;
            }
 
            // Case 2: change the next
            // link of prev to current
            // consonant pointing to
            // ptr
            else {
                prev->next = ptr;
            }
        }
 
        // Move prev and ptr to next
        // for next iteration
        prev = prev->next;
        ptr = ptr->next;
    }
}
 
// Driver code
int main()
{
    // Initialise the Linked List
    head = newNode('a');
    head->next = newNode('b');
    head->next->next = newNode('c');
    head->next->next->next = newNode('e');
    head->next->next->next->next = newNode('f');
    head->next->next->next->next->next = newNode('g');
    head->next->next->next->next->next->next = newNode('i');
    head->next->next->next->next->next->next->next = newNode('o');
 
    // Print the given Linked List
    printf("Linked list before :\n");
    printlist(head);
 
    removeVowels();
 
    // Print the Linked List after
    // removing vowels
    printf("Linked list after :\n");
    printlist(head);
 
    return 0;
}


Java
// Java program to remove vowels
// Nodes in a linked list
import java.io.*;
 
// A linked list node
class Node
{
  char data;
  Node next;
 
  // Function to add new node to the
  // List
  Node(char item)
  {
    data = item;
    next = null;
  }
}
class GFG
{
 
  // Head Node
  public static Node head;
 
  // Utility function to print the
  // linked list
  static void printlist(Node head)
  {
    if(head == null)
    {
      System.out.println("Empty List");
 
    }
    while(head != null)
    {
      System.out.print(head.data + " ");
      if(head.next != null)
      {
        System.out.print("-> ");  
      }
      head = head.next;
    }
    System.out.println();
  }
 
  // Utility function for checking vowel
  static boolean isVowel(char x)
  {
    return (x == 'a' || x == 'e' || x == 'i' ||
            x == 'o' || x == 'u' || x == 'A' ||
            x == 'E' || x == 'I' || x == 'O' || x == 'U');
 
  }
 
  // Function to remove the vowels Node
  static void removeVowels()
  {
 
    // Node pointing to head Node
    Node ptr = head;
 
    // Case 1 : Remove the trailing
    // vowels
    while(ptr != null)
    {
 
      // If current Node is a vowel
      // node then move the pointer
      // to next node
      if(isVowel(ptr.data))
      {
        ptr = ptr.next;
      }
 
      // Else break if a consonant
      // node is found
      else
      {
        break;
      }
    }
 
    // This prev node used to link
    // prev consonant to next
    // consonant after vowels
    Node prev = ptr;
 
    // Head points to the first
    // consonant of the linked list
    head = ptr;
    ptr = ptr.next;
 
    // Case 2: If vowels found in
    // between of the linked list
    while(ptr != null)
    {
 
      // If current node is vowel
      if(isVowel(ptr.data))
      {
 
        // Move ptr to the next
        // node
        ptr = ptr.next;
 
        // Check for vowels
        // occurring continuously
        while(ptr != null)
        {
 
          // If ptr is a vowel
          // move to next pointer
          if(isVowel(ptr.data))
          {
            ptr = ptr.next;
          }
 
          // Else break if
          // consonant found
          else
          {
            break;
          }
        }
 
        // Case 3: If we have ending
        // vowels then link the prev
        // consonant to NULL
        if(ptr == null)
        {
          prev.next = null;
          break;
        }
 
        // Case 2: change the next
        // link of prev to current
        // consonant pointing to
        // ptr
        else
        {
          prev.next = ptr;
        }
      }
 
      // Move prev and ptr to next
      // for next iteration
      prev = prev.next;
      ptr = ptr.next;
    }
  }
 
  // Driver code
  public static void main (String[] args)
  {
 
    // Initialise the Linked List
    GFG tree = new GFG();
    tree.head = new Node('a');
    tree.head.next = new Node('b');
    tree.head.next.next = new Node('c');
    tree.head.next.next.next = new Node('e');
    tree.head.next.next.next.next = new Node('f');
    tree.head.next.next.next.next.next = new Node('g');
    tree.head.next.next.next.next.next.next = new Node('i');
    tree.head.next.next.next.next.next.next.next = new Node('o');
 
    // Print the given Linked List
    System.out.println("Linked list before :");
    printlist(head);
    removeVowels();
 
    // Print the Linked List after
    // removing vowels
    System.out.println("Linked list after :");
    printlist(head);
  }
}
 
// This code is contributed by avanitrachhadiya2155


Python3
# Python3 program to remove vowels
# Nodes in a linked list
 
# A linked list node
class Node:
    def __init__(self, x):
        self.data = x
        self.next = None
 
# Head Node
head = None
 
# Utility function to print the
# linked list
def printlist(head):
    if (not head):
        print("Empty List")
        return
 
    while (head != None):
        print(head.data, end = " ")
        if (head.next):
            print(end="-> ")
        head = head.next
    print()
 
# Utility function for checking vowel
def isVowel(x):
    return (x == 'a' or x == 'e' or x == 'i'
            or x == 'o' or x == 'u' or x == 'A'
            or x == 'E' or x == 'I' or x == 'O'
            or x == 'U')
 
# Function to remove the vowels Node
def removeVowels():
    global head
     
    # Node pointing to head Node
    ptr = head
 
    # Case 1 : Remove the trailing
    # vowels
    while (ptr != None):
 
        # If current Node is a vowel
        # node then move the pointer
        # to next node
        if (isVowel(ptr.data)):
            ptr = ptr.next
 
        # Else break if a consonant
        # node is found
        else:
            break
 
    # This prev node used to link
    # prev consonant to next
    # consonant after vowels
    prev = ptr
 
    # Head points to the first
    # consonant of the linked list
    head = ptr
 
    ptr = ptr.next
 
    # Case 2: If vowels found in
    # between of the linked list
    while (ptr != None):
 
        # If current node is vowel
        if (isVowel(ptr.data)):
 
            # Move ptr to the next
            # node
            ptr = ptr.next
 
            # Check for vowels
            # occurring continuously
            while (ptr != None):
 
                # If ptr is a vowel
                # move to next pointer
                if (isVowel(ptr.data)):
                    ptr = ptr.next
                 
                # Else break if
                # consonant found
                else:
                    break
 
            # Case 3: If we have ending
            # vowels then link the prev
            # consonant to NULL
            if (ptr == None):
                prev.next = None
                break
 
            # Case 2: change the next
            # link of prev to current
            # consonant pointing to
            # ptr
            else:
                prev.next = ptr
 
        # Move prev and ptr to next
        # for next iteration
        prev = prev.next
        ptr = ptr.next
 
# Driver code
if __name__ == '__main__':
     
    # Initialise the Linked List
    head = Node('a')
    head.next = Node('b')
    head.next.next = Node('c')
    head.next.next.next = Node('e')
    head.next.next.next.next = Node('f')
    head.next.next.next.next.next = Node('g')
    head.next.next.next.next.next.next = Node('i')
    head.next.next.next.next.next.next.next = Node('o')
 
    # Print the given Linked List
    print("Linked list before :")
    printlist(head)
 
    removeVowels()
 
    # Print the Linked List after
    # removing vowels
    print("Linked list after :")
    printlist(head)
 
    # This code is contributed by mohit kumar 29


C#
// C# program to remove vowels
// Nodes in a linked list
using System;
 
// A linked list node
class Node
{
    public char data;
    public Node next;
     
    // Function to add new node to the
    // List
    public Node(char item)
    {
        data = item;
        next = null;
    }
}
 
class GFG{
     
// Head Node
static Node head;
 
// Utility function to print the
// linked list
static void printlist(Node head)
{
    if (head == null)
    {
        Console.WriteLine("Empty List");
    }
    while (head != null)
    {
        Console.Write(head.data + " ");
         
        if (head.next != null)
        {
            Console.Write("-> ");  
        }
        head = head.next;
    }
    Console.WriteLine();
}
 
// Utility function for checking vowel
static bool isVowel(char x)
{
    return (x == 'a' || x == 'e' ||
            x == 'i' || x == 'o' ||
            x == 'u' || x == 'A' ||
            x == 'E' || x == 'I' ||
            x == 'O' || x == 'U');
}
 
// Function to remove the vowels Node
static void removeVowels()
{
     
    // Node pointing to head Node
    Node ptr = head;
     
    // Case 1 : Remove the trailing
    // vowels
    while (ptr != null)
    {
         
        // If current Node is a vowel
        // node then move the pointer
        // to next node
        if (isVowel(ptr.data))
        {
            ptr = ptr.next;
        }
         
        // Else break if a consonant
        // node is found
        else
        {
            break;
        }
    }
     
    // This prev node used to link
    // prev consonant to next
    // consonant after vowels
    Node prev = ptr;
     
    // Head points to the first
    // consonant of the linked list
    head = ptr;
    ptr = ptr.next;
     
    // Case 2: If vowels found in
    // between of the linked list
    while (ptr != null)
    {
         
        // If current node is vowel
        if (isVowel(ptr.data))
        {
             
            // Move ptr to the next
            // node
            ptr = ptr.next;
             
            // Check for vowels
            // occurring continuously
            while (ptr != null)
            {
                 
                // If ptr is a vowel
                // move to next pointer
                if (isVowel(ptr.data))
                {
                    ptr = ptr.next;
                }
                 
                // Else break if
                // consonant found
                else
                {
                    break;
                }
            }
             
            // Case 3: If we have ending
            // vowels then link the prev
            // consonant to NULL
            if (ptr == null)
            {
                prev.next = null;
                break;
            }
             
            // Case 2: change the next
            // link of prev to current
            // consonant pointing to
            // ptr
            else
            {
                prev.next = ptr;
            }
        }
         
        // Move prev and ptr to next
        // for next iteration
        prev = prev.next;
        ptr = ptr.next;
    }
}
 
// Driver code
static public void Main()
{
     
    // Initialise the Linked List
    GFG.head = new Node('a');
    GFG.head.next = new Node('b');
    GFG.head.next.next = new Node('c');
    GFG.head.next.next.next = new Node('e');
    GFG.head.next.next.next.next = new Node('f');
    GFG.head.next.next.next.next.next = new Node('g');
    GFG.head.next.next.next.next.next.next = new Node('i');
    GFG.head.next.next.next.next.next.next.next = new Node('o');
     
    // Print the given Linked List
    Console.WriteLine("Linked list before :");
    printlist(head);
    removeVowels();
     
    // Print the Linked List after
    // removing vowels
    Console.WriteLine("Linked list after :");
    printlist(head);
}
}
 
// This code is contributed by rag2127


Javascript


输出:
Linked list before :
a -> b -> c -> e -> f -> g -> i -> o 
Linked list after :
b -> c -> f -> g

时间复杂度: O(N),其中 N 是链表中的节点数。

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live