Program to find size of Doubly Linked List - GeeksforGeeks

23 May, 2021
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. 
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. 
C++
Java
Python3
C#
Javascript
// A complete working C++ program to// find size of doubly linked list.#include <bits/stdc++.h>using namespace std; // A linked list nodestruct 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 listint findSize(struct Node *node){   int res = 0;   while (node != NULL)   {       res++;       node = node->next;   }   return res;} /* Driver 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;}
// A complete working Java program to// find size of doubly linked list.import java.io.*;import java.util.*; // Represents a doubly linked list nodeclass Node{    int data;    Node next, prev;    Node(int val)    {        data = val;        next = null;        prev = null;    }} class GFG{     /* Function to add a node to front of doubly    linked list */    static Node push(Node head, int data)    {        Node new_node = new Node(data);        new_node.next = head;        new_node.prev = null;        if (head != null)            head.prev = new_node;        head = new_node;                     return head;    }     // This function returns size of doubly linked list    static int findSize(Node node)    {        int res = 0;        while (node != null)        {                res++;                node = node.next;        }         return res;    }     // Driver Code    public static void main(String args[])    {        Node head = null;        head = push(head, 4);        head = push(head, 3);        head = push(head, 2);        head = push(head, 1);        System.out.println(findSize(head));    }} // This code is contributed by rachana soma
# A complete working Python3 program to# find size of doubly linked list. # A linked list nodeclass Node:    def __init__(self):        self.data = None        self.next = None        self.prev = None # Function to add a node to front of doubly# linked listdef push( head_ref, new_data):     new_node = Node()    new_node.data = new_data    new_node.next = (head_ref)    new_node.prev = None    if ((head_ref) != None):        (head_ref).prev = new_node    (head_ref) = new_node    return head_ref # This function returns size of linked listdef findSize(node):     res = 0    while (node != None):        res = res + 1        node = node.next         return res # Driver codehead = Nonehead = push(head, 4)head = push(head, 3)head = push(head, 2)head = push(head, 1)print(findSize(head)) # This code is contributed by Arnab Kundu
// A complete working C# program to// find size of doubly linked list. using System; // Represents a doubly linked list nodepublic class Node{    public int data;    public Node next, prev;    public Node(int val)    {        data = val;        next = null;        prev = null;    }} class GFG{     /* Function to add a node to front of doubly    linked list */    static Node push(Node head, int data)    {        Node new_node = new Node(data);        new_node.next = head;        new_node.prev = null;        if (head != null)            head.prev = new_node;        head = new_node;                     return head;    }     // This function returns size of doubly linked list    static int findSize(Node node)    {        int res = 0;        while (node != null)        {                res++;                node = node.next;        }         return res;    }     // Driver Code    public static void Main(String []args)    {        Node head = null;        head = push(head, 4);        head = push(head, 3);        head = push(head, 2);        head = push(head, 1);        Console.WriteLine(findSize(head));    }} // This code is contributed by Arnab Kundu
<script>// A complete working javascript program to// find size of doubly linked list.// Represents a doubly linked list node class Node {        constructor(val) {            this.data = val;            this.prev = null;            this.next = null;        }    }    /*     * Function to add a node to front of doubly linked list     */    function push(head , data) {var new_node = new Node(data);        new_node.next = head;        new_node.prev = null;        if (head != null)            head.prev = new_node;        head = new_node;         return head;    }     // This function returns size of doubly linked list    function findSize(node) {        var res = 0;        while (node != null) {            res++;            node = node.next;        }         return res;    }     // Driver Code     var head = null;        head = push(head, 4);        head = push(head, 3);        head = push(head, 2);        head = push(head, 1);        document.write(findSize(head)); // This code contributed by aashish1995</script>
4
 
rachana soma
andrew1234
nidhi_biet
aashish1995
doubly linked list
Linked List
Linked List
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Linked List | Set 1 (Introduction)
Linked List | Set 2 (Inserting a node)
Stack Data Structure (Introduction and Program)
Linked List | Set 3 (Deleting a node)
LinkedList in Java
Linked List vs Array
Detect loop in a linked list
Merge two sorted linked lists
Find the middle of a given linked list
Delete a Linked List node at a given position