Skip to main content

Delete all even parity nodes from circular linked list in c++

C++ program for Delete all even parity nodes from circular linked list. Here problem description and other solutions.

// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Remove all even parity nodes from a circular single linked list
class LinkNode
{
	public: 
    int data;
	LinkNode *next;
	LinkNode(int data)
	{
		this->data = data;
		this->next = nullptr;
	}
};
class CircularLinkedList
{
	public: 
    LinkNode *head;
	LinkNode *tail;
	CircularLinkedList()
	{
		this->head = nullptr;
		this->tail = nullptr;
	}
	void insert(int value)
	{
		LinkNode *node = new LinkNode(value);
		if (this->head == nullptr)
		{
			this->head = node;
		}
		else
		{
			this->tail->next = node;
		}
		node->next = this->head;
		this->tail = node;
	}
	// Display node element of circular linked list
	void display()
	{
		if (this->head == nullptr)
		{
			cout << "\n Empty Linked List" << endl;
		}
		else
		{
			cout << "\n Linked List Element \n";
			// First node of linked list
			cout << "  " << this->head->data;
			LinkNode *temp = this->head->next;
			// Display linked list node
			while (temp != nullptr && temp != this->head)
			{
				// Display node value
				cout << "  " << temp->data;
				// Visit to next node
				temp = temp->next;
			}
		}
	}
	// Handles the request to find set bits in given integer
	int coutSetBit(int num)
	{
		int result = num;
		result = result - ((result >> 1) &1431655765);
		result = (result &858993459) + ((result >> 2) &858993459);
		result = (result + (result >> 4)) &252645135;
		result = result + (result >> 8);
		result = result + (result >> 16);
		result = result &63;
		return result;
	}
	void removeNode()
	{
		if (this->head == nullptr)
		{
			return;
		}
		LinkNode *temp = this->head;
		LinkNode *auxiliary = this->head;
		LinkNode *point = nullptr;
		while (temp != nullptr)
		{
			if (this->coutSetBit(temp->data) % 2 == 0)
			{
				auxiliary = temp;
				// Visit to next node
				temp = temp->next;
				if (auxiliary == this->head)
				{
					// Remove head node
					if (auxiliary == this->tail)
					{
						// Removing a single node
						this->tail = nullptr;
						this->head = nullptr;
						temp = nullptr;
					}
					else
					{
						this->head = temp;
						this->tail->next = temp;
					}
				}
				else if (auxiliary == this->tail)
				{
					if (point != nullptr)
					{
						// When Remove last node
						point->next = this->head;
					}
					this->tail = point;
				}
				else
				{
					if (point != nullptr)
					{
						// When Remove last node
						point->next = temp;
					}
				}
              	delete auxiliary;
				auxiliary = nullptr;
			}
			else
			{
				point = temp;
				// Visit to next node
				temp = temp->next;
				if (temp == this->head)
				{
					// Stop the process
					temp = nullptr;
				}
			}
		}
	}
};
int main()
{
	CircularLinkedList *cll = new CircularLinkedList();
	// Insert node
	cll->insert(3);
	cll->insert(12);
	cll->insert(32);
	cll->insert(14);
	cll->insert(27);
	cll->insert(19);
	cll->insert(1);
	cll->insert(18);
	cout << "\n Before Remove ";
	cll->display();
	/*        
			    Node  Binary     Even Parity
			     3     (11)         Yes
			    12     (1100)       Yes
			    32     (100000)     No
			    14     (1110)       No
			    27     (11011)      Yes
			    19     (10011)      No
			    1         (1)       No        
			    18     (10010)      Yes
			*/
	cll->removeNode();
	cout << "\n\n After Remove";
	cll->display();
	return 0;
}

Output

 Before Remove
 Linked List Element
  3  12  32  14  27  19  1  18

 After Remove
 Linked List Element
  32  14  19  1




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