A singly linked list is a type of linked list that is unidirectional, that is, it can be traversed in only one direction from head to the last node.
Each node is traversed in a linear fashion and not randomly like arrays. The node->next and null properties are very common when performing these operations. Memory management is to be done manually in C++ while performing operations on the Singly Linked List data structure.
PSUEDO CODE:-
-----Traversal - Iterating and Moving through all the elements of the linked list.
void traversal(Node* head) {
Node* temp = head;
while(temp != NULL)
{
cout<<(temp->data);
temp = temp->next;
}
}
-----Insertion - Either at the front or the back of the list. Can also be inserted at a specific position as explained in the video.
Node* insertAtBegin(Node* head, int x)
{
Node* newNode = new Node(x)
if(head == NULL)
return newNode;
else
{
newNode->next = head;
return newNode;
}
}
-----Deletion - Removing elements from either ends or from a specific position
Node deleteNode(Node* head, Node* toBeDeleted)
{
if(head == toBeDeleted)
{
return head.next;
}
Node* temp = head;
while( temp->next != NULL )
{
if(temp->next == toBeDeleted)
{
temp->next = temp->next->next;
return head;
}
temp = temp->next;
}
return head;
}