查找双向链表大小的程序
给定一个双向链表,任务是找到该双向链表的大小。例如,下面链表的大小是 4。
双向链表是一种链接数据结构,由一组称为节点的顺序链接记录组成。每个节点包含两个字段,称为链接,它们是对节点序列中前一个节点和下一个节点的引用。
双向链表的遍历可以是任一方向。事实上,如果需要,遍历的方向可以改变很多次。
例如,对于上述双向链表,该函数应返回 3。
算法 :
1) 将大小初始化为 0。
2)初始化一个节点指针,temp=head。
3) 在 temp 不为 NULL 时执行以下操作
……a) 温度 = 温度 -> 下一个
……b) 尺寸++;
4) 返回大小。
C++
// A complete working C++ program to
// find size of doubly linked list.
#include
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;
}
/* 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;
}
Java
// A complete working Java program to
// find size of doubly linked list.
import java.io.*;
import java.util.*;
// Represents a doubly linked list node
class 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
Python3
# A complete working Python3 program to
# find size of doubly linked list.
# A linked list node
class Node:
def __init__(self):
self.data = None
self.next = None
self.prev = None
# Function to add a node to front of doubly
# linked list
def 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 list
def findSize(node):
res = 0
while (node != None):
res = res + 1
node = node.next
return res
# Driver code
head = None
head = 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
C#
// A complete working C# program to
// find size of doubly linked list.
using System;
// Represents a doubly linked list node
public 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
Javascript
输出:
4
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。