
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Program to find size of Doubly Linked List in C++
In this problem, we are given a doubly linked list. Our task is to create a program to find size of Doubly Linked List in C++.
Doubly Linked List is a special type of Linked list in which navigation is possible in both ways, either forward and backward easily as compared to Single Linked List. The following are the important terms to understand the concept of doubly linked lists.
Link − Each link of a linked list can store a data called an element.
Next − Each link of a linked list contains a link to the next link called Next.
Prev − Each link of a linked list contains a link to the previous link called Prev.
LinkedList − A Linked List contains the connection link to the first link called First and to the last link called Last.
Representation of a Doubly Linked List −
Problem Description − We will be given a doubly linked list of the above type. And we will find the size (length) of it.
Let’s take an example to understand the problem,
Input
the above linked list A <-> B <-> C.
Output
3
Solution Approach
To find the size of the doubly linked list, we need to traverse the doubly linked list and keep track of the length with the length valable.
Algorithm
Initialise − length = 0, *temp = head
- Step 1 − Traverse the list i.e. do till the temp != NULL.
- Step 1.1 − Increase length, length++
- Step 1.2 − Update pointer, temp = temp -> next.
- Step 2 − print length.
Program to illustrate the working of our solution
Example
#include <iostream> using namespace std; struct doublyLL { char val; struct doublyLL *next; struct doublyLL *prev; }; void insertNode(struct doublyLL** head_ref, int value){ struct doublyLL* new_node = new doublyLL; new_node->val = value; new_node->next = (*head_ref); new_node->prev = NULL; if ((*head_ref) != NULL) (*head_ref)->prev = new_node ; (*head_ref) = new_node; } int calcDLLSize(struct doublyLL *temp) { int length = 0; while (temp != NULL){ temp = temp->next; length++; } return length; } int main(){ struct doublyLL* head = NULL; insertNode(&head, 'A'); insertNode(&head, 'H'); insertNode(&head, 'E'); insertNode(&head, 'K'); insertNode(&head, 'M'); insertNode(&head, 'S'); cout<<"The size of Doubly Linked List is "<<calcDLLSize(head); return 0; }
Output
The size of Doubly Linked List is 6
- Related Articles
- C++ Program to Implement Doubly Linked List
- C++ Program to Implement Circular Doubly Linked List
- C++ Program to Implement Sorted Doubly Linked List
- Python Program to Find the Largest Element in a Doubly Linked List
- C++ Program to Implement Sorted Circularly Doubly Linked List
- Reverse a Doubly-Linked List in Groups of a Given Size using C++
- Python program to search an element in a doubly linked list
- Python program to create and display a doubly linked list
- Python program to rotate doubly linked list by N nodes
- Golang program to count the number of nodes in a doubly linked list.
- Find the largest node in Doubly linked list in C++
- Difference between Singly linked list and Doubly linked list in Java
- Golang Program to create a doubly linked list and traverse forward.
- Python program to convert a given binary tree to doubly linked list
- The Doubly Linked List class in Javascript
