Count Total Nodes in Circular Linked List
JAVA Program to Count the Number of Nodes in a Circular Linked List
In this page, we’ll take a look at a program to Count Total Nodes in Circular Linked List, a loop is required until the very first element reappears. Circular linked lists have a head and tail. The head marks the beginning of the list pointing towards the first first element whereas the tail is the last element that also points to the first element of the list. A loop iterates through the entire list and increases the value of a counter variable for every passing node.
Steps to be followed to create a program to count the number of Nodes in a Linked List
To count the number of elements in a circular linked list –
- Intiate a count variable with zero
- Store the value of head in current variable.
- Iterate through the loop increasing the value of count by 1 and changing the value of current to the next node.
- Stop the iteration as soon as the current value equals to the head.
- Print the count variable as the total number of elements in the circular linked list.
public void countNodes() { Node temp = head; do{ cnt++; temp = temp.next; }while(temp != head); System.out.println("Number of Nodes in the list is "+cnt); }
Algorithm for writing the function to count Nodes in a Circular Linked List
- countNodes()
- Node temp = head
- do while(temp != head)
- cnt++
- temp = temp->next
Code to Count Total Nodes in Circular Linked List in JAVA
import java.util.*; public class Main { public static void main(String[] args) { Main Obj = new Main(); Obj.add(10); Obj.add(20); Obj.add(30); Obj.add(40); Obj.add(50); Obj.add(60); Obj.print(); Obj.countNodes(); } public class Node{ int element; Node next; public Node(int element) { this.element = element; } } int cnt; static Node list1, list2; public Node head = null; public Node tail = null; public void print() { Node current = head; if(head == null) { System.out.println("Empty List"); } else { System.out.println("Circular Linked List is"); do{ //Prints each node by incrementing pointer. System.out.print(" "+ current.element); current = current.next; }while(current != head); System.out.println(); } } public void print(Node head) { Node current = head; if(head == null) { System.out.println("Empty List"); } else { System.out.println("Circular Linked List is"); do{ //Prints each node by incrementing pointer. System.out.print(" "+ current.element); current = current.next; }while(current != head); System.out.println(); } } public void add(int element){ Node newNode = new Node(element); if(head == null) { head = newNode; tail = newNode; newNode.next = head; } else { tail.next = newNode; tail = newNode; tail.next = head; } } public void countNodes() { Node temp = head; do{ cnt++; temp = temp.next; }while(temp != head); System.out.println("Number of Nodes in the list is "+cnt); } }
Output
Circular Linked List is 10 20 30 40 50 60 Number of Nodes in the list is 6
import java.lang.*; public class Main { public static void main(String[] args) { Main Obj = new Main(); Obj.add(10); Obj.add(20); Obj.add(30); Obj.add(40); Obj.add(50); Obj.add(60); System.out.println("Original List"); Obj.print(); Obj.countNodes(); } public class Node{ int element; Node next; Node prev; public Node(int element) { this.element = element; } } int cnt; static Node list1, list2; public Node head = null; public Node tail = null; public void print() { Node temp = head; if(head == null) { System.out.println("null"); } else { do{ System.out.print(" "+ temp.element); temp = temp.next; }while(temp != head); System.out.println(); } } public void add(int element){ Node newNode = new Node(element); if(head == null) { head = newNode; tail = newNode; newNode.next = head; newNode.prev=head; } else { tail.next = newNode; newNode.prev=tail; tail = newNode; tail.next = head; } } public void print(Node head) { Node current = head; if(head == null) { System.out.println("Empty List"); } else { System.out.println("Circular Linked List is"); do{ //Prints each node by incrementing pointer. System.out.print(" "+ current.element); current = current.next; }while(current != head); System.out.println(); } } public void countNodes() { Node temp = head; do{ cnt++; temp = temp.next; }while(temp != head); System.out.println("Number of Nodes in the list is "+cnt); } }
Output
Original List 10 20 30 40 50 60 Number of Nodes in the list is 6
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Circular Linked List
- Introduction to Circular Linked List
Click Here - Circular Linked List Applications
Click Here - Circular Linked List in –
- Insertion in Circular Linked List –
- Insertion at the beginning–
- Insertion at the end –
- Insertion at nth position –
- Deletion in Circular Linked List –
- Deletion from beginning in Circular Linked List –
- Deletion from nth position in Circular Linked List –
- Deletion from end in Circular Linked List –
- Insertion and Deletion in Circular Linked List – C | C++ | Java
- Split a Circular Linked List in two halves –
- Count nodes in Circular Linked List –
- Sorted Insert In Circular Linked List –
- Insertion in the middle in Circular Linked List –
Circular Linked List
- Introduction to Circular Linked List
- Circular Linked List Applications
- Circular Linked List in – C | C++ | Java
- Insertion in Circular Linked List – C | C++ | Java
- Deletion in Circular Linked List – C | C++ | Java
- Insertion and Deletion in a Circular Linked List – C | C++ | Java
- Split a Circular Linked List in two halves – C | C++ | Java
- Count nodes in Circular Linked List – C | C++ | Java
- Sorted Insert In Circular Linked List – C | C++ | Java
- Insertion in the middle in Circular Linked List – C | C++ | Java
Login/Signup to comment