Creation of Linked List

#ifndef NUMBERLIST_H
#define NUMBERLIST_H
class numberList
{
	private:
		//Declaration a structure of the Nodes
		struct Node
		{
			int value;
			Node* next;
		};	
		Node *head; // List head Pointer
	public:
		//Constructor
		numberList();

		//~Destructor
		~numberList();
		
		//Linked List Operations
		
		// Insertion Operations for the linked list
		void insertAtHead(int);
		void insertAtEnd(int);
		void insertInMiddle(int);
		void insertInOrder(int);

		// Print out data in the list
		void printList();
	
			
		// Deletion Operations for the linked list
		void deleteAtHead();
		void deleteAtEnd();
		void deleteIthNode(int);
		
};
#endif

Deleting Nodes from the List

Linked lists provide us the great feature of deleting a Node. The process of deletion is also easy to implement. The basic structure is to declare a temporary pointer which points the Node to be deleted. There are also three cases in which a Node can be deleted:

Deletion at the Start

In this case, the first Node of the linked list is deleted. The first Node is called head. To delete a Node at the beginning of the List, there are 3-steps:

Reference
void deleteAtHead()
{
	if(head == nullptr)
	{
		cout << "The list is empty." << endl;
		return;
     	}

	Node* temp = head;
	head = head->next;
	delete temp;
}

Deletion at the End

To delete a Node at the end of the List, there are 3-steps:

Reference
void deleteAtEnd()
{
	//(1) The list is empty
	if(head == nullptr)
	{
		cout << "The list is empty." << endl;
		return;
	}

     	//(2) There is only one Node in the list
     	if(head->next == nullptr)
	{
		delete head;
		head = nullptr;
		return;
	}

	//(3) Normal Situation
	Node *cur = head;
	Node *pre = nullptr;
	while(cur->next != nullptr)
	{
		pre = cur;
		cur = cur->next;	
	}
	pre->next = nullptr;
	delete cur;
}

Deletion at Particular Position

In linked list, we can delete a specific Node. To delete a specific Node from the linked list, there are 3-steps:

Reference

Reference
void deleteIthNode(int i)
{
	//cur will point to the item to delete
	Node* cur = head; 
	//prev will point to the item before cur
	Node* prev; 
	int j = 1; //j will keep count of which node
       		   //in the list we are currently on
	if(head == nullptr)
		cout << "Not enough items in List." << endl;
	else
	{
		//Traverse the list to find the ith item
		//The following is a while loop that will traverse
		//the list until either j == i or the end of the list
		//is encountered
    		 while(j != i && cur->next != nullptr)
		{
			prev = cur;
			cur = cur->next;
			j++;
		}
		//If there are i items in the list,
		//delete the ith item.
		if(j == i)
		{
			prev->next = cur->next;
			delete cur;
		}
		else
			cout << "Not enough items in list." << endl;
	}		
}