Given a doubly linked list, the task is to find the size of that doubly linked list. For example, size of below linked list is 4.

A doubly linked list is a linked data structure that consists of a set of sequentially linked records called nodes. Each node contains two fields, called links, that are references to the previous and to the next node in the sequence of nodes.

Traversal of a doubly linked list can be in either direction. In fact, the direction of traversal can change many times, if desired.

For example the function should return 3 for the above doubly linked list.

## Algorithm :

1) Initialize size to 0.

2) Initialize a node pointer, temp = head.

3) Do following while temp is not NULL

……a) temp = temp -> next

……b) size++;

4) Return size.

`// A complete working C++ program to ` `// find size of doubly linked list. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// A linked list node ` `struct` `Node ` `{ ` ` ` `int` `data; ` ` ` `struct` `Node *next; ` ` ` `struct` `Node *prev; ` `}; ` ` ` `/* Function to add a node to front of doubly ` ` ` `linked list */` `void` `push(` `struct` `Node** head_ref, ` `int` `new_data) ` `{ ` ` ` `struct` `Node* new_node = ` `new` `Node; ` ` ` `new_node->data = new_data; ` ` ` `new_node->next = (*head_ref); ` ` ` `new_node->prev = NULL; ` ` ` `if` `((*head_ref) != NULL) ` ` ` `(*head_ref)->prev = new_node ; ` ` ` `(*head_ref) = new_node; ` `} ` ` ` `// This function returns size of linked list ` `int` `findSize(` `struct` `Node *node) ` `{ ` ` ` `int` `res = 0; ` ` ` `while` `(node != NULL) ` ` ` `{ ` ` ` `res++; ` ` ` `node = node->next; ` ` ` `} ` ` ` `return` `res; ` `} ` ` ` `/* Drier program to test above functions*/` `int` `main() ` `{ ` ` ` `struct` `Node* head = NULL; ` ` ` `push(&head, 4); ` ` ` `push(&head, 3); ` ` ` `push(&head, 2); ` ` ` `push(&head, 1); ` ` ` `cout << findSize(head); ` ` ` `return` `0; ` `} ` |

**Output:**

4

## Recommended Posts:

- Sorted insert in a doubly linked list with head and tail pointers
- Large number arithmetic using doubly linked list
- Rotate Doubly linked list by N nodes
- Priority Queue using doubly linked list
- Reverse a Doubly Linked List | Set 4 (Swapping Data)
- Reverse a stack without using extra space in O(n)
- Sort a k sorted doubly linked list
- Remove duplicates from an unsorted doubly linked list
- Remove duplicates from a sorted doubly linked list
- Merge k sorted linked lists | Set 2 (Using Min Heap)
- Doubly Circular Linked List | Set 1 (Introduction and Insertion)
- Convert a given Binary Tree to Doubly Linked List | Set 4
- Search an element in a Linked List (Iterative and Recursive)
- Circular Linked List | Set 1 (Introduction and Applications)
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.