Basic Linked List Operations
This lesson lists the various operations that can be performed on linked lists
We'll cover the following
The primary operations which are generally a part of the LinkedList
class are listed below:
insertAtTail(data)
- inserts an element at the end of the linked listinsertAtHead(data)
- inserts an element at the start/head of the linked listdelete(data)
- deletes an element with your specified value from the linked listdeleteAtHead()
- deletes the first element of the listsearch(data)
- searches for an element in the linked listgetHead()
- returns head of the linked listisEmpty()
- returns true if the linked list is empty
If you observe the list of functions mentioned above, the last function, isEmpty()
is a helper function which will prove useful in defining all the other functions.
So let’s define it first.
isEmpty()
The basic condition for our list to be considered empty is that the head should be the only pointer in the list. This implies that the head points to NULL
.
With that in mind, let’s write down the simple implementation for isEmpty()
:
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.