Unit 2
Unit 2
Linked Lists: Singly linked lists: representation and operations, doubly linked lists and
circular linked lists, Comparing arrays and linked lists, Applications of linked lists.
1) Linked List:
Linked list is a linear collection of data elements that store in a
sequence called nodes. For each node will have 2-fields. Such as
Data field
Address field
Address Field: it is used to store the address of next node in list (i.e
link)
The starting node in linked list is called Head node which is used as start of linked list
Operations of linked list
Insert
Delete
Search
Display
Algorithm
Change the address pointer of last node from NULL to the new
node
Make the address pointer of new node as NULL to show the end
of Linked List
Deletion: This operation is used to delete an element from linked list.
Deleting a node in a Linked List is an important operation and can be
done by removing the first node in list.
Algorithm
Check if the list is empty: If the head is NULL, the list is empty,
and there’s nothing to delete.
Update the head node: Set the head to the second node (head = head-
>addr.
Delete the original head node: The original head node is now free
Algorithm
We will initialize a temporary pointer to the head node of the
singly linked list.
After that, we will check if that pointer is null or not null, if it
is null, then return.
While the travel pointer is not null, we will access and print the
data of the current node, then we move the pointer to next address
node.
Search: This operation is used to find the given user element exist in
the list or not.
Algorithm
-------------------------------------------------------------------------
-------
The main advantage of a doubly linked list is that it allows for efficient
traversal of the list in both directions. This is because each node in the
list contains a pointer to the previous node and a pointer to the next
node.
This allows for quick and easy insertion and deletion of nodes from the
list, as well as efficient traversal of the list in both directions.
The double linked list is represented using nodes that have three fields:
Data field
A pointer address to the next node (next_addr)
A pointer address to the previous node (prev_addr)
Its Data structure is defined by using struct user defined data type
Each node in a Doubly Linked List contains the data it holds, a pointer to
the next node in the list, and a pointer to the previous node in the list.
By linking these nodes together through the next and prev pointers, we can
traverse the list in both directions (forward and backward), which is a
key feature of a Doubly Linked List.
Note: In double linked list Start node is called as Head and last Node is
called as tail
Insertion
Deletion
Search
Traversal
Algorithm
Deletion: This operation is used to delete the node from linked list.
Removing of node can done only when there is DLL. Each node in DLL
contains pointers to both its previous and next nodes, deletion requires
careful pointer adjustments to ensure no broken links occur.
Algorithm
Check if the list is empty; if it is, return as there is nothing to
delete.
Store the current head node in a temporary variable.
Move the head pointer to the next node.
If the new head exists, update its prev pointer to NULL.
Delete the old head node to free memory.
Algorithm
Example: DLL is
Note: With DLL Data structure it is easy to reverse the list without
complexity.
-------------------------------------------------------------------------
-------
This means that you can keep traversing the list without ever
reaching a NULL value.
Example
Operations on the Circular Linked list
We can do some operations on the circular linked list similar to
the singly and doubly linked list which are:
1. Insertion
2. Deletion
3. Search
4. Display/Traversal
Insertion
Deletion
Traversal
This operation is used to travel the CLL from head node to the
again head .Because here the list is maintained in circularly.
-------------------------------------------------------------------
------