- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang program to count the number of nodes in a doubly linked list.
Examples
Approach
Step 1 − Define a method that accepts the head of a doubly linked list.
Step 2 − Initialize temp:=head, count:=0
Step 3 − Iterate temp until it becomes nil.
Step 4 − Increase count by 1.
Step 5 − At the end, print count.
Example
package main import "fmt" type Node struct { prev *Node value int next *Node } func CreateNewNode(value int) *Node{ var node Node node.next = nil node.value = value node.prev = nil return &node } func TraverseDoublyLL(head * Node){ // Forward Traversal fmt.Printf("Doubly Linked List: ") count := 0 temp := head for temp!= nil{ fmt.Printf("%d ", temp.value) temp = temp.next count++ } fmt.Printf("\nTotal number of nodes: %d", count) } func main(){ // 10 <−> 20 <−> 30 <−> 40 head := CreateNewNode(10) node_2 := CreateNewNode(20) node_3 := CreateNewNode(30) node_4 := CreateNewNode(40) head.next = node_2 node_2.prev = head node_2.next = node_3 node_3.prev = node_2 node_3.next = node_4 node_4.prev = node_3 TraverseDoublyLL(head) }
Output
Doubly Linked List: 10 20 30 40 Total number of nodes: 4
- Related Articles
- Golang Program to count the number of nodes in a linked list.
- Python program to create a doubly linked list of n nodes and count the number of nodes
- Python program to rotate doubly linked list by N nodes
- Golang Program to create a doubly linked list and traverse forward.
- Python program to create a Circular Linked List of N nodes and count the number of nodes
- Delete all the even nodes from a Doubly Linked List in C++
- Python program to create a doubly linked list of n nodes and display it in reverse order
- Find count of common nodes in two Doubly Linked Lists in C++
- Delete all Prime Nodes from a Doubly Linked List in C++
- C++ Program to Implement Doubly Linked List
- Program to find size of Doubly Linked List in C++
- Count nodes in Circular linked list in C++
- 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

Advertisements