程序在C ++中查找双链表的大小
在本教程中,我们将讨论一个程序来查找双链表的大小。
为此,我们将提供一个双向链接列表。我们的任务是找到链表的大小,即链表中元素的数量。
示例
#include <bits/stdc++.h>
using namespace std;
//structure of linked list node
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;
}
//returning size of linked list
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
热门推荐
10 喜得孙子祝福语简短唯美
11 婚礼祝福语简短朴实大众
12 员工周年祝福语大全简短
13 家长回复节日祝福语简短
14 简短生日祝福语老同学
15 夜雨祝福语简短10字
16 蛋糕祝福语简短闺蜜
17 部门过年祝福语简短独特
18 简短精炼的结婚祝福语