In this program, we are going to share a C++ program to find size of doubly linked list. If you are a beginner and want to start learning the C++ programming, then keep your close attention in this tutorial as I am going to share a program for C++ program to find size of doubly linked list.
Copy the below C++ program and execute it with the help of GCC compiler. At the end of this program, We have shared the output of this program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #include <bits/stdc++.h> using namespace std; struct Node { int data; struct Node *next; struct Node *prev; }; 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; } int findSize(struct Node *node) { int res = 0; while (node != NULL) { res++; node = node->next; } return res; } int main() { struct Node* head = NULL; push(&head, 4); push(&head, 3); push(&head, 2); push(&head, 1); cout << findSize(head); return 0; } |
4
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.