16.1 C
Delhi
Friday, December 27, 2024

Linked List Data Structure : Its Operations & Complexity 

Must read

In the world of programming and data structures we have already covered arrays , now we have another important and versatile data structure linked list. Linked list data structures are an important part of understanding and implementing other data structures like stacks , queues , trees ( non-linear data structure ). 

In real life we have used a lot of things that remind us of practical implementation of  linked list data structures like image viewer where we use buttons to navigate to the next image and another next image.

Linked list data structure is very easy to understand and the way we are implementing advanced data structure with the help of it .  Its real life examples help beginners to understand its implementation. In this blog we will cover the introduction of linked list, types of linked lists, real life examples of using linked lists, different operations performed on it along with its complexity analysis using space and time.

What is linked list data structure ?

A linked list data structure is a linear data structure in which different nodes are connected linearly with the help of pointers. In linked list data structure , we are specifically using the concept of the node.

Node is generally a structure in which we divide a block into two parts one is data (value ) and another is a pointer which connects the address of the next node. 

As we have represented the image below , it helps you to understand how different nodes are connected with each other in a linked list and a head pointer connects the initial  node of the entire linked list and the last node of the linked list contains null so that the pointer will not points to unknown location in the memory (if the pointer points the unknown location it shows undefined behaviour ).

These are basic terminologies to understand entire concept of linked list 

Head :  Head is a pointer that points to the first node of the linked list . If the head pointer contains NULL that indicates that the linked list is empty ( there is no node in the linked list ).

Next : Next pointer points to the next node of the linked list and the  next pointer of the last node contains NULL to avoid undefined behaviour.

Node : Node itself is a structure that contains value and a pointer in a linked list .

Types of linked list data structure

In this article,  we have covered mainly three various linked list data structures :

Singly Linked List Data Structure (SLL)

Singly linked list contains the next pointer that points to the next node along with traversing in the singly linked  list done only in one direction , as there is no possibility to traverse back to find the previous element in a singly linked list data structure.

Because of the unidirectional property of singly linked list in data structure make it difficult to approach traversing property to the back.

Doubly Linked List Data Structure (DLL)

Doubly linked lists have extended properties in comparison to the singly linked list data structure because it consists of two pointers: one is the previous pointer that points to the previous node and another pointer is the next pointer that points to the next node of the linked list.

This linked list improves the traversing properties in comparison to singly linked list as it contains two pointers; it’s very easy to go bidirectional now and back to the previous node.

It provides efficient insertion and deletion of elements from both the ends and improves the complexity. We will discuss complexity analysis of linked lists in a later section.

dOUBLY LINKED LIST DATA STRUCTURE
DOUBLY LINKED LIST DATA STRUCTURE

Circular Linked List Data Structure (CLL)

Circular linked list data structure is the different version it forms a loop between the nodes in a linked list. The pointer of the last node contains the address of the first node.

The same last node connects with the initial node of a linked list and makes a circle or loop in it. The complexity of the circular linked lists are generally better in comparison to other versions of the linked lists.

CIRCULAR LINKED LIST DATA STRUCTURE

Applications of linked list data structure

The are various applications of linked list data structure :

Use of linked list data structure in sparse matrix

Linked lists efficiently represent the sparse matrix , for every non-zero value we will create a node in linked list that represents the value (  non-zero ) along with the row and column index . It is also an advantage using linked lists for sparse matrices because if we store non-zero values , it will reduce the memory usage along with that we can grow and shrink the size of it.

Packet switching in networking

Usage of linked lists is also very efficient in networking because we are using linked lists in routers that help us for data transmission , it manages the packets (data) waiting in the queue for information processing. 

Large message (good length) divided into smaller chunks and chunks (information) stored in the nodes of a linked list.

Implementation in other data structures

With the help of linked lists we can easily implement  various data structures like stacks , queues , trees etc. Complexity is an important factor for implementing data structures and linked lists play an efficient role in maintaining complexity.

Operations on linked list data structure 

There are various operations that we can perform in a linked list data structure like traversal of an element in a linked list , insertion of an element at a particular location , inserting of an element at a start location , insertion of an element at the end location , deletion of an element at a particular location , deletion of an element from start location , deletion of an element at the end location along with the sorting approach.

Here is the table given below in detail that specify entire operations structure that various linked lists performed in a data structure : 

Operations Performed Singly linked list complexities Doubly linked list complexitiesCircular linked list complexities
Traversal operation in linked list O ( n )O ( n ) O ( n )
Insertion of an element at a particular location O ( n )O ( n ) O ( n )
Insertion of an element at a start location O ( 1 )O ( 1 ) O ( 1 )
Insertion of an element at the end location O ( n )O ( n )O ( n )
Deletion of an element at a particular location O ( n )O ( n )O ( n ) 
Deletion of an element from the start location O ( 1 ) O ( 1 ) O ( 1 ) 
Deletion of an element at the end location O ( n ) O ( n )O ( n )
Sorting Approach (Sorting )O ( n ^ 2 ) O ( n ) O ( n ) 

Complexity Analysis

Analysis of complexity for linked list data structure for any operation in terms of time and space is an important concept to understand to analyze the workflow of an algorithm or while writing the code. Detailed information of entire operation according to its time complexity is given below where you can cover entire concept at a one place is given below in the listed table : 

Operations performed on linked list data structure  Time Complexity 
Searching an element in linked list O( n )
Deletion of an element from a particular position O( n ) 
Deletion of an element from the startO ( 1 ) 
Deletion of an element from the last O ( n )
Insertion of an element on a particular position O ( n ) 
Insertion of an element from the start O ( 1 )
Insertion of an element at the end O ( n )

Why choose linked lists over array data structures?

  • Linked list provides dynamic resizing over array data structure, we will use array data structure when we want to do random access. Apart from dynamic resizing insertion and deletion operations in array data structure is costly in comparison to the linked lists.  
  • Sorting an element becomes efficient when we are performing linked list over array data structure because doubly linked lists are able to access previous and next nodes easily but arrays can store elements in a linear arrangement. Memory fragmentation is a concern when we talk about linked lists over arrays.

Conclusion

We have almost covered foundational concepts of linked list data structure, working with time complexity to compare how efficient a linked list is in comparison to arrays in data structure.

Using linked lists over array data structures help us in performing better performance in terms of insertion and deletion (cost will be less ) and implementation of linked lists in advanced data structures comes with better complexity.

Join India's Largest CAT Communityspot_img
Course on Demandspot_img

Latest article