Linked List
Linked List
A linked list is a collection of nodes, where each node consists of two parts:
info: the actual element to be stored in the list. It is also called data field.
link: one or two links that points to next and previous node in the list. It is also called next or
pointer field.
Operations on linked list
• Creation: This operation is used to create a linked list
• Insertion: This operation is used to insert a new node in a linked list in a specified
position. A new node may be inserted
1. Traversing:
Traversing a singly linked list means visiting each node in the list exactly once. Here's how it
works:
This process continues until the current pointer becomes NULL, signifying the end of the list.
2. Searching:
Searching for a specific value in a singly linked list involves iterating through the list and
comparing the data in each node with the target value.
If the loop completes without finding a match, the target value doesn't exist in the list, and you
can return an appropriate indicator (e.g., -1 or NULL).
3. Insertion:
Inserting a new node into a singly linked list can be done at different positions:
4. Deletion:
Deleting a node from a singly linked list involves handling different scenarios:
1. Node Structure:
Data: This can store any type of information, like integers, characters, or even complex
objects.
Next: This is a pointer (or reference) that holds the memory address of the next node in
the list. The last node's next pointer typically points to NULL (or equivalent), signifying
the end of the list.
2. Creating a Node:
You'll need a function or method to create a new node. This function allocates memory for the
node, initializes the data field with the desired value, and sets the next pointer to NULL initially.
There are various ways to build the list depending on your needs. Here are two common
approaches:
Once you have a linked list, you can iterate through it by starting at the head node and following
the next pointers of each node until you reach the end (indicated by NULL).
There are variations like adding nodes at a specific position or removing nodes.
You can implement the linked list using various programming languages with slight
syntax variations but following the same core concepts.
Traversing a doubly linked list can be done in both forward and backward directions.
Forward Traversal:
o Start with a pointer to the head node.
o While the current pointer is not NULL:
Access the data stored in the current node.
Move the current pointer to the next node using its next field.
Backward Traversal:
o Start with a pointer to the tail node (if you maintain it) or traverse to the last node.
o While the current pointer is not NULL:
Access the data stored in the current node.
Move the current pointer to the previous node using its prev field.
2. Searching:
Similar to singly linked lists, you can search for a value by iterating through the list and
comparing data in each node:
3. Insertion:
With doubly linked lists, insertion can be done more efficiently at any position because you can
navigate in both directions. Here are common scenarios:
4. Deletion: