Linked List

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

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

• ✔At the beginning of the linked list

• ✔ At the end of the linked list

• ✔At the specified position in a linked list


• Deletion: The deletion operation is used to delete a node from the linked list. A node
may be deleted from

• ✔The beginning of the linked list

• ✔the end of the linked list

• ✔ the specified position in the linked list.


• Traversing: The list traversing is a process of going through all the nodes of the linked
list from on end to the other end. The traversing may be either forward or backward.
• Searching or find: This operation is used to find an element in a linked list. In the desired
element is found then we say operation is successful otherwise unsuccessful.
• Concatenation: It is the process of appending second list to the end of the first list.
Types of Linked List
• basically we can put linked list into the following four types:
• Singly linked list
• doubly linked list
• circular linked list
• circular doubly linked list
Singly linked list
A singly linked list is a dynamic data structure which may grow or shrink, and growing and
shrinking depends on the operation made. In this type of linked list each node contains
two fields one is info field which is used to store the data items and another is link field
that is used to point the next node in the list. The last node has a NULL pointer.
The following example is a singly linked list that contains three elements 5, 3, 8.

1. Traversing:

Traversing a singly linked list means visiting each node in the list exactly once. Here's how it
works:

 Start with a pointer to the head node of the list.


 While the current pointer is not NULL:
o Access the data stored in the current node.
o Update the current pointer to point to the next node using its next field.

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.

 Start a pointer at the head node.


 While the current pointer is not NULL:
o Check if the data in the current node matches the target value.
 If yes, return the current node or its index (depending on your
implementation).
o Move the current pointer to the next node using its next field.

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:

 Insertion at the Beginning:


o Create a new node with the desired data.
o Make the next pointer of the new node point to the current head node.
o Update the head pointer to point to the newly created node.
 Insertion at the End:
o Traverse the list to find the last node (the one with next as NULL).
o Create a new node with the desired data and set its next to NULL.
o Update the next pointer of the last node to point to the new node.
 Insertion at a Specific Position:
o Traverse the list to reach the node before the desired insertion position.
o Create a new node with the desired data.
o Update the next pointer of the new node to point to the node currently at the
desired position.
o Update the next pointer of the previous node to point to the newly created node.

4. Deletion:

Deleting a node from a singly linked list involves handling different scenarios:

 Deletion from the Beginning:


o If the list is empty (head is NULL), there's nothing to delete.
o Otherwise, store a reference to the node to be deleted (current head).
o Update the head pointer to point to the second node (current head's next).
o Free the memory allocated to the deleted node.
 Deletion from the End:
o Traverse the list to find the second-last node (the one whose next points to the
last node).
o Set the next pointer of the second-last node to NULL.
o Free the memory allocated to the last node.
 Deletion of a Specific Node:
o Traverse the list to find the node before the one to be deleted.
o If the node to be deleted is not found, return an error (e.g., node not present).
o Store a reference to the node to be deleted (the node after the previous node).
o Update the next pointer of the previous node to point to the node after the one to
be deleted (effectively skipping the deleted node).
o Free the memory allocated to the deleted node.

1. Node Structure:

A singly linked list node typically consists of two parts:

 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.

3. Building the List:

There are various ways to build the list depending on your needs. Here are two common
approaches:

 Adding Nodes at the Beginning:


o Create a new node.
o If the list is empty (head pointer is NULL), set the head pointer to this new node.
o Otherwise, update the next pointer of the current head node to point to the new
node.
o Make the new node the new head of the list.
 Adding Nodes at the End:
o Traverse the list until you reach the last node (the one with next as NULL).
o Create a new node and set its next to NULL.
o Update the next pointer of the last node to point to the newly created node.

4. Traversing the List:

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).

Here are some additional points to consider:

 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.

2.1 Doubly Linked List: traversing, inserting, creating and


deleting in doubly linked list
Doubly linked lists offer more flexibility compared to singly linked lists because nodes have
connections in both the forward and backward directions. Here's a breakdown of key operations
on doubly linked lists:
1. Traversing:

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:

 Start a pointer at the head node (or tail if searching backward).


 While the current pointer is not NULL:
o Check if the data in the current node matches the target value.
 If yes, return the current node or its index.
o Move the current pointer to the next node (or previous node for backward search)
using the appropriate pointer.

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:

 Insertion at the Beginning:


o Create a new node with the desired data.
o Update the next pointer of the new node to point to the current head node.
o If the current head is not NULL, update its prev pointer to point to the new node.
o Make the new node the new head of the list and update the head pointer to point
to it.
 Insertion at the End:
o Create a new node with the desired data.
o If the list is empty (head is NULL), set both head and tail pointers to the new node.
o Otherwise, traverse to the last node (tail).
o Update the next pointer of the last node to point to the new node.
o Update the prev pointer of the new node to point to the last node.
o Update the tail pointer to point to the newly created node.
 Insertion at a Specific Position:
o Traverse the list to find the node before the desired insertion position.
o Create a new node with the desired data.
o Update the next pointer of the new node to point to the node currently at the
desired position.
o Update the prev pointer of the node at the desired position to point to the new
node.
o If inserting at the beginning (i.e., before the head node), update the head pointer to
point to the new node.
o Update the prev pointer of the new node to point to the previous node
(considering the case where you might be inserting before the head).

4. Deletion:

Deletion also benefits from two-way traversal in doubly linked lists:

 Deletion from the Beginning:


o If the list is empty (head is NULL), there's nothing to delete.
o Otherwise, store a reference to the node to be deleted (current head).
o Update the head pointer to point to the second node (current head's next).
o If the list becomes empty after deletion (i.e., head becomes NULL), set the tail
pointer to NULL as well.
o Update the prev pointer of the new head node (if not null) to point to NULL (since
it becomes the new head).
o Free the memory allocated to the deleted node.
 Deletion from the End:
o If the list is empty (head is NULL), there's nothing to delete.
o Traverse to the second-last node (the one whose next points to the last node).
o Store a reference to the last node (tail).
o Update the next pointer of the second-last node to NULL.
o If the list has only one node (head and tail are the same), update the head pointer
to NULL as well.
o Free the memory allocated to the deleted tail node.

You might also like