Implement Linked List class in C++, which contains a pointer head as data member and define
following member methods in the class;
Constructor: initialize data members of the class.
Destructor: deallocate the memory which was allocated dynamically.
isEmpty: check whether list is empty or not.
insertAtStart: insert a node at the start of linked list
insertAtEnd: insert a node at the end of linked list
insertNode: insert a node at a specified index of linked list
displayList: print all the elements of a linked list
findNode: find a node in the linked list and return true if found, false otherwise
deleteAtStart: insert a node at the start of linked list
deleteAtEnd: insert a node at the end of linked list
delateNode: delete a node from the linked list
CODE
#include<iostream>
using namespace std;
class LinkedList
{
private:
struct LinkListNodes
{
int value;
LinkListNodes *next;
};
LinkListNodes*head;
public:
~LinkedList(); // Constructor
LinkedList(); // Destructor
bool isEmpty (void); // isEmpty
void Insert_At_Start(int) ; // Insert_At_Start
void Insert_At_End (int) ; // Insert_At_End
void Insert_Node (int) ; // Insert_Node
void Display_Nodes (void); // Display_Nodes
void Find_Node (int) ; // Find_Node
void Delete_At_Start(void); // Delete_At_Start
void Delete_At_End (void); // Delete_At_End
void Delete_Node (int) ; // Delete_Node
};
LinkedList::LinkedList() // Constructor
{
head = NULL;
}
LinkedList::~LinkedList() // Destructor
{
LinkListNodes *NodePtr, *NextNode;
NodePtr = head;
while (NodePtr)
{
NextNode = NodePtr->next;
delete NodePtr;
NodePtr = NextNode;
}
}
bool LinkedList::isEmpty(void) // isEmpty
{
if (!head)
return true;
else
return false;
}
void LinkedList::Insert_At_Start(int num) // Insert_At_Start
{
LinkListNodes *NodePtr, *NewNode;
NewNode = new LinkListNodes;
NewNode->value = num;
NewNode->next = NULL;
if (!head)
{
head = NewNode;
NewNode->next = NULL;
}
else if (head)
{
head = NewNode;
NewNode->next = NodePtr;
}
}
void LinkedList::Insert_At_End(int num) // Insert_At_End
{
LinkListNodes *NodePtr, *NewNode;
NewNode = new LinkListNodes;
NewNode->value = num;
NewNode->next = NULL;
if (!head)
{
head = NewNode;
}
else
{
NodePtr = head;
while (NodePtr->next)
{
NodePtr = NodePtr->next;
}
NodePtr->next = NewNode;
}
}
void LinkedList::Insert_Node(int num) // Insert_Node
{
LinkListNodes *PreNode, *NodePtr, *NewNode;
NewNode = new LinkListNodes;
NewNode->value = num;
if (!head)
{
head = NewNode;
NewNode->next = NULL;
}
else
{
NodePtr = head;
PreNode = NULL;
while (NodePtr != NULL && NodePtr->value < num)
{
PreNode = NodePtr;
NodePtr = NodePtr->next;
}
if (PreNode == NULL)
{
head = NewNode;
NewNode->next = NodePtr;
}
else
{
PreNode->next = NewNode;
NewNode->next = NodePtr;
}
}
}
void LinkedList::Find_Node(int num) // Find_Node
{
LinkListNodes *NodePtr;
if (isEmpty())
{
cout << "Link List Is Empty " << endl;
}
else
{
while (NodePtr->next != NULL && NodePtr->value != num)
{
NodePtr = NodePtr->next;
}
if (NodePtr->value == num)
{
cout <<"Value Is = " << NodePtr->value << "Value Found Index = " << &NodePtr->value << endl;
}
else
{
cout << "Value Not Found " << endl;
}
}
}
void LinkedList::Delete_At_Start() // Delete_At_Start
{
LinkListNodes *NodePtr, *NewNode;
NewNode = new LinkListNodes;
if (isEmpty())
{
cout << "Link List Is Empty " << endl;
}
else if (head)
{
NodePtr = head->next;
delete head;
head = NodePtr;
}
}
void LinkedList::Delete_At_End() // Delete_At_End
{
LinkListNodes *NodePtr;
if (isEmpty())
{
cout << "Link List Is Empty " << endl;
}
else if (head->next == NULL)
{
delete head;
}
NodePtr = head;
while (NodePtr->next->next != NULL)
NodePtr = NodePtr->next;
delete (NodePtr->next);
NodePtr->next = NULL;
}
void LinkedList::Delete_Node(int num) // Delete_Node
{
LinkListNodes *NodePtr, *PreNode;
if (!head)
{
return;
}
if (head->value == num)
{
NodePtr = head->next;
delete head;
head = NodePtr;
}
else
{
NodePtr = head;
while (NodePtr != NULL && NodePtr->value != num)
{
PreNode = NodePtr;
NodePtr = NodePtr->next;
}
PreNode->next = NodePtr->next;
delete NodePtr;
}
}
void LinkedList::Display_Nodes(void) // Display_Nodes
{
LinkListNodes *NodePtr;
cout << "Liked List Nodes Value are: " << endl;
NodePtr = head;
while (NodePtr != NULL)
{
cout << "Value Is = " << NodePtr->value << " " << endl;
NodePtr = NodePtr->next;
}
}
int main()
{
LinkedList l;
l.Insert_At_Start(14);
l.Insert_Node(12);
l.Insert_Node(34);
l.Insert_Node(10);
l.Insert_At_End(22);
l.Display_Nodes();
cout<<"\t\t\t\tAfter Delete Nodes "<<endl;
l.Delete_At_End();
l.Delete_At_Start();
l.Display_Nodes();
}