Skip to main content

Move vowels node at the beginning of linked list in c++

C++ program for Move vowels node at the beginning of linked list. Here problem description and explanation.

// Include header file
#include <iostream>

using namespace std;
/*
  C++ program for
  Shift the vowels node at beginning of linked list
*/
// Linked list node
class LinkNode
{
	public: 
    char data;
	LinkNode *next;
	LinkNode(char data)
	{
		this->data = data;
		this->next = nullptr;
	}
};
class SingleLL
{
	public: 
    LinkNode *head;
	LinkNode *tail;
	SingleLL()
	{
		this->head = nullptr;
		this->tail = nullptr;
	}
	//Add new Node at end of linked list 
	void addNode(char data)
	{
		LinkNode *node = new LinkNode(data);
		if (this->head == nullptr)
		{
			this->head = node;
		}
		else
		{
			// Append the node at last position
			this->tail->next = node;
		}
		this->tail = node;
	}
	// Display linked list element
	void display()
	{
		if (this->head == nullptr)
		{
			cout << "\n Empty linked list" << endl;
			return;
		}
		LinkNode *temp = this->head;
		//iterating linked list elements
		while (temp != nullptr)
		{
			cout << " " << temp->data << " →";
			// Visit to next node
			temp = temp->next;
		}
		cout << " NULL\n";
	}
	// Determine that given value is an vowel or not
	bool isVowels(char value)
	{
		if (value == 'a' || value == 'e' || 
            value == 'i' || value == 'o' || 
            value == 'u' || value == 'A' || 
            value == 'E' || value == 'I' || 
            value == 'O' || value == 'U')
		{
			return true;
		}
		return false;
	}
	//  Move the vowels value at beginning position
	void shiftVowels()
	{
		if (this->head == nullptr)
		{
			cout << "\n Empty linked list\n";
			return;
		}
		// Define some auxiliary variable
		LinkNode *node = this->head;
		LinkNode *back = nullptr;
		// iterate linked list nodes
		// And shift vowels at front of linked list
		while (node != nullptr)
		{
			if (this->isVowels(node->data) == true && back != nullptr)
			{
				// Enter here when
				// Node is contain vowel and 
				// its previous (any) node is not form of vowel
				if (node == this->tail)
				{
					// When is last node
					// Set new last node
					this->tail = back;
				}
				// Connect previous node to next node
				back->next = node->next;
				// Connect vowel node to first node
				node->next = this->head;
				// Set new head
				this->head = node;
				// Visit to next node in current order
				node = back->next;
			}
			else
			{
				if (!this->isVowels(node->data))
				{
					back = node;
				}
				// Visit to next node
				node = node->next;
			}
		}
	}
};
int main()
{
	SingleLL *sll = new SingleLL();
	// Constructed linked list
	sll->addNode('E');
	sll->addNode('D');
	sll->addNode('U');
	sll->addNode('C');
	sll->addNode('A');
	sll->addNode('T');
	sll->addNode('I');
	sll->addNode('O');
	sll->addNode('N');
	cout << " Before move vowels" << endl;
	// E → D → U → C → A → T → I → O → N → NULL
	sll->display();
	sll->shiftVowels();
	cout << " After move vowels" << endl;
	// O → I → A → U → E → D → C → T → N → NULL
	sll->display();
	return 0;
}

Output

 Before move vowels
 E → D → U → C → A → T → I → O → N → NULL
 After move vowels
 O → I → A → U → E → D → C → T → N → NULL




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment