The WP Debugging plugin must have a wp-config.php file that is writable by the filesystem.

Efficient algorithm to find the size of Doubly Linked List

Program to find the size of Doubly Linked List

In this blog, we will study the problem in which we will find the size of a doubly linked list. A doubly linked list is a type of linked list in which we can traverse in both ends i.e. forward and backward as there is both pointer i.e. pointer of the previous node as well as the pointer of the next node.

Problem Statement

In this problem, we are given a doubly linked list, and we need to find the size of a doubly linked list.

Note: The linked list does not contain loop.

Problem Statement Understanding

Let’s first understand what a doubly linked list is.

In simple words, a doubly linked list can be defined as a linked data structure made up of nodes, which are sequentially linked records. Each node has three fields one data field and two link fields which are basically the pointers to the node before it and the node after it in the sequence. We can traverse through the doubly linked list in both ways as each of its node contains the address of previous as well as next node.

Now, let’s understand the problem statement with help of examples.

Suppose if we are given a doubly linked list:

and we have to output its size.

We can see that the linked list contains total of 4 nodes so our output will be 4, as the size of linked list is equal to the number of nodes in the linked list.

If the linked list is:

the size of the linked list is 6 equal to the number of the nodes.

Explanation The size of the linked list is equal to the number of nodes in the linked list.

Approach to find the size of a doubly linked list

The size of the linked list is equal to the count of the number of the nodes in the linked list.

To find the count of the nodes in the linked list:

  • Initialize a counter variable to 0 and a temp pointer to the head of the linked list
  • Using the temp pointer traverse the linked list until temp!=NULL and increment the counter variable by 1 for each node.

Finally when the temp==NULL the counter variable will have the size of the linked list.

Algorithm to find the size of a doubly linked list

  • Initialize a variable ListSize to 0.
  • Initialize a node pointer, temp = head.
  • Do following while temp is not NULL
    a) temp = temp -> next
    b) ListSize++;
  • Return ListSize.

Dry Run to find the size of a doubly linked list


Implementation to find the size of a doubly linked list

#include 
using namespace std;

struct llNode
{
    int data;
    struct llNode *next;
    struct llNode *prev;
};


void push(struct llNode** head_ref, int data)
{
    struct llNode* new_node = new llNode;
    new_node->data = data;
    new_node->next = (*head_ref);
    new_node->prev = NULL;
    if ((*head_ref) != NULL)
    (*head_ref)->prev = new_node ;
    (*head_ref) = new_node;
}


int SizeOfList(struct llNode *temp)
{
    int ListSize = 0;
    while(temp != NULL)
    {
        ListSize++;
        temp = temp->next;
    }
    return ListSize;
}

int main()
{
    struct llNode* head = NULL;
    push(&head, 9);
    push(&head, 7);
    push(&head, 5);
    push(&head, 3);
    cout <<"Size of the doubly linked list: "<< SizeOfList(head);
    return 0;
}

Output

Size of the doubly linked list: 5

Time Complexity of the size of a doubly linked list: O(n), where n is the number of nodes in the linked list.

In this article, we have tried to explain the most efficient algorithm for how to get the size of a doubly linked list. This problem is interesting as well as important from the interviewee’s point of view. If you want to solve more questions on Linked List, which is curated by our expert mentors at PrepBytes, you can follow this link Linked List.

Related Articles
Doubly circular linked list in data structure Application of doubly linked list
Applications of linked list Singly linked list vs doubly linked list
Advantages and disadvantages of linked list Doubly linked list all operations in C
Binary search in linked list Bubble sort linked list
Deletion in doubly linked list Delete the middle node of a linked list
Polynomial addition using linked list Find max value and min value in linked list
Insert a node at a specific position in a linked list Swap nodes in linked list
Add two numbers represented by linked lists Find starting point of loop in linked list
Merge sort linked list Delete a node from linked list at a given position
Remove duplicates from unsorted linked list Reverse a linked list in Python

FAQs

  1. What is a doubly linked list used for?
  2. A doubly linked list is used in navigation systems that require both forward and backward navigation.

  3. What is the difference between singly linked lists and doubly linked lists?
  4. A singly linked list is traversed in one direction only whereas a doubly linked list is traversed in two ways.

  5. What is a memory-efficient doubly linked list?
  6. When each node has only one pointer to traverse the list back and forth.

Leave a Reply

Your email address will not be published. Required fields are marked *