Delete a node from linked list specific position in cpp

C++ program for Delete a node from linked list specific position. Here problem description and explanation.

// Include header file
#include <iostream>
using namespace std;
// C++ Program for
// Delete node at given index of linked list

// Linked list node
class LinkNode
{
	public: int data;
	LinkNode *next;
	LinkNode(int 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 the end of linked list
	void addNode(int value)
	{
		// Create a new node
		LinkNode *node = new LinkNode(value);
		if (this->head == nullptr)
		{
			this->head = node;
		}
		else
		{
			this->tail->next = node;
		}
		this->tail = node;
	}
	// Display linked list element
	void display()
	{
		if (this->head == nullptr)
		{
			return;
		}
		LinkNode *temp = this->head;
		// iterating linked list elements
		while (temp != nullptr)
		{
			cout << temp->data << " → ";
			// Visit to next node
			temp = temp->next;
		}
		cout << "NULL" << endl;
	}
	// Delete node at given position
	void deleteAtIndex(int index)
	{
		LinkNode *temp = this->head;
		if (this->head == nullptr)
		{
			cout << "Empty linked list" << endl;
			return;
		}
		else if (index < 0)
		{
			cout << "Invalid positions" << endl;
			return;
		}
		else if (index == 1)
		{
			// Means deleting a first node
			if (this->tail == this->head)
			{
				// Only one node
				this->tail = nullptr;
			}
			// Make second node as head
			this->head = this->head->next;
		}
		else
		{
			int count = index - 1;
			// iterating linked list elements and find deleted node
			while (temp != nullptr && count > 1)
			{
				// Visit to next node
				temp = temp->next;
				count--;
			}
			if (count == 1 && temp->next != nullptr)
			{
				// Get deleted node
				LinkNode *n = temp->next;
				if (n == this->tail)
				{
					// When delete last node
					this->tail = temp;
				}
				// Unlink the deleting node
				temp->next = n->next;
			}
			else
			{
				cout << " Index " << index 
                     << " are not present" << endl;
				return;
			}
		}
	}
};
int main()
{
	SingleLL *sll = new SingleLL();
	// Test with your index
	// Note index start with 1
	int index = 5;
	// Linked list
	// 10 → 20 → 30 → 40 → 50 → 60 → 70 → 80 → NULL
	sll->addNode(10);
	sll->addNode(20);
	sll->addNode(30);
	sll->addNode(40);
	sll->addNode(50);
	sll->addNode(60);
	sll->addNode(70);
	sll->addNode(80);
	cout << " Before Delete Linked List" << endl;
	sll->display();
	// Delete node at 5-th position
	sll->deleteAtIndex(index);
	// Display linked list
	cout << " After Delete Linked List" << endl;
	sll->display();
	return 0;
}

Output

 Before Delete Linked List
10 → 20 → 30 → 40 → 50 → 60 → 70 → 80 → NULL
 After Delete Linked List
10 → 20 → 30 → 40 → 60 → 70 → 80 → NULL


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







© 2021, kalkicode.com, All rights reserved