Inserting, deleting, and searching for an item
So far, we have seen only the operations for inserting a node and displaying all node contents. Now, we will explore the other operations available in a linked list. We will mainly focus on the following operations:
- Inserting at the first node
- Searching for a node
- Inserting before a specific node
- Inserting after a specific node
- Deleting the first node
- Deleting the last node
- Searching for and deleting one node
- Reversing a linked list
- Getting Nth position element
Inserting at the first node
When we add a node in the front, or head, we have to consider two simple possibilities. The list can be empty and as a result the new node is the head. This possibility is as simple as it can get. However, if the list already has a head, then we have to do the following:
- Create the new node.
- Make the new node as first node or head.
- Assign the previous head or first node as the next to follow for the newly created first node.
Here is the code for this:
public function insertAtFirst...