TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Program to Create a Circular Linked List of N Nodes and Count the Number of Nodes

Program to Create a Circular Linked List of N Nodes and Count the Number of Nodes on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph etc.

<< Back to PROGRAM

Q. Program to create a circular linked list of n nodes and count the number of nodes.

Explanation

In this program, we have to find out the number of nodes present in the circular linked list. We first create the circular linked list, then traverse through the list and increment variable 'count' by 1.

Algorithm

  1. Define a Node class which represents a node in the list. It has two properties data and next which will point to the next node.
  2. Define another class for creating the circular linked list and it has two nodes: head and tail. It has two methods: add() and display() .
  3. add() will add the node to the list:
    1. It first checks whether size is null or head is null; then it will insert the node as the head.
    2. Both head and tail will point to a newly added node.
    3. If the head is not null, the new node will be the new tail, and new tail will point to the head as it is a circular linked list.
  4. countNodes() will count the number of nodes present in the list.
    1. Define new node current which will point to the head node.
    2. Traverse through the list to count the nodes by making the current node to point to next node in the list till current points to head again.

Solution

Python

#Represents the node of the list.
class Node:
    def __init__(self,data):
        self.data = data;
        self.next = None;
 
class CreateList:
    #Declaring head and tail pointer as null.
    def __init__(self):
        self.count = 0;
        self.head = Node(None);
        self.tail = Node(None);
        self.head.next = self.tail;
        self.tail.next = self.head;
    
    #This function will add the new node at the end of the list.
    def add(self,data):
        newNode = Node(data);
        #Checks if the list is empty.
        if self.head.data is None:
            #If list is empty, both head and tail would point to new node.
            self.head = newNode;
            self.tail = newNode;
            newNode.next = self.head;
        else:
            #tail will point to new node.
            self.tail.next = newNode;
            #New node will become new tail.
            self.tail = newNode;
            #Since, it is circular linked list tail will point to head.
            self.tail.next = self.head;
            
    #This function will count the nodes of circular linked list
    def countNodes(self):
        current = self.head;
        self.count=self.count+1;
        while(current.next != self.head):
            self.count=self.count+1;
            current = current.next;
        print("Count of nodes present in circular linked list: "),
        print(self.count);
    
 
class CircularLinkedList:
    cl = CreateList();
    #Adds data to the list
    cl.add(1);
    cl.add(2);
    cl.add(4);
    cl.add(1);
    cl.add(2);
    cl.add(3);
    #Displays all the nodes present in the list
    cl.countNodes();

Output:

Count of nodes present in circular linked list: 6

C

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
//Represents the node of list.
struct node{
    int data;
    struct node *next;
};
 
int count = 0;
//Declaring head and tail pointer as null.
struct node *head = NULL;
struct node *tail = NULL;
 
//This function will add the new node at the end of the list.
void add(int data){
    //Create new node
    struct node *newNode = (struct node*)malloc(sizeof(struct node));
    newNode->data = data;
    //Checks if the list is empty.
    if(head == NULL){
        //If list is empty, both head and tail would point to new node.
        head = newNode;
        tail = newNode;
        newNode->next = head;
    }else {
        //tail will point to new node.
        tail->next = newNode;
        //New node will become new tail.
        tail = newNode;
        //Since, it is circular linked list tail will point to head.
        tail->next = head;
    }
}
 
//This function will count the nodes of circular linked list
void countNodes() {
    struct node *current = head;
    do{
        count++;
        current = current->next;
    }while(current != head);
    printf("Count of nodes present in circular linked list: %d",count);
}
    
int main()
{
    //Adds data to the list
    add(1);
    add(2);
    add(4);
    add(1);
    add(2);
    add(3);
   //Counts the number of nodes present in the list
   countNodes();
   
   return 0;
}

Output:

Count of nodes present in circular linked list: 6

JAVA

public class CountNodes {
        //Represents the node of list.
        public class Node{
            int data;
            Node next;
            public Node(int data) {
                this.data = data;
            }
        }
        
        public int count;
        //Declaring head and tail pointer as null.
        public Node head = null;
        public Node tail = null;
        
        //This function will add the new node at the end of the list.
        public void add(int data){
            //Create new node
            Node newNode = new Node(data);
            //Checks if the list is empty.
            if(head == null) {
                 //If list is empty, both head and tail would point to new node.
                head = newNode;
                tail = newNode;
                newNode.next = head;
            }
            else {
                //tail will point to new node.
                tail.next = newNode;
                //New node will become new tail.
                tail = newNode;
                //Since, it is circular linked list tail will point to head.
                tail.next = head;
            }
        }
        
        //This function will count the nodes of circular linked list
        public void countNodes() {
            Node current = head;
            do{
                //Increment the count variable by 1 for each node
                count++;
                current = current.next;
            }while(current != head);
            System.out.println("Count of nodes present in circular linked list: "+count);
        }
        
        public static void main(String[] args) {
            CountNodes cl = new CountNodes();
            cl.add(1);
            cl.add(2);
            cl.add(4);
            cl.add(1);
            cl.add(2);
            cl.add(3);
            //Counts the number of nodes present in the list
            cl.countNodes();
        }
}

Output:

Count of nodes present in circular linked list: 6

C#

using System; 
namespace CircularLinkedList 
{                     
    public class Program
    {
        //Represents the node of list.
        public class Node<T>{
            public T data;
            public Node<T> next;
            public Node(T value) {
                data = value;
                next = null;
            }
        }
        
        public class CreateList<T>{
            int count = 0;
            protected Node<T> head = null;             
             protected Node<T> tail = null;
            
            //This function will add the new node at the end of the list.
            public void add(T data){
                //Create new node
                Node<T> newNode = new Node<T>(data);
                //Checks if the list is empty.
                if(head == null){
                    head = newNode;
                    tail = newNode;
     newNode.next = head;
                }else{
                    //tail will point to new node.
                    tail.next = newNode;
                    //New node will become new tail.
                    tail = newNode;
                    //Since, it is circular linked list tail will point to head.
                    tail.next = head;
                }
    
            }
            //This function will count the nodes of circular linked list
            public void countNodes() {
                Node<T> current = head;
                do{
                    //Counts nodes by pointing to next node.
                    count++;
                    current = current.next;
                }while(current != head);
                Console.WriteLine("Count of nodes present in circular linked list: "+count);
            }
    }
        
        public static void Main()
        {
            
        CreateList<int> cl = new CreateList<int>();
        //Adds data to the list
        cl.add(1);
        cl.add(2);
        cl.add(4);
        cl.add(1);
        cl.add(2);
        cl.add(3);
        //Counts the number of nodes present in the list
        cl.countNodes();
        }    
    }
}

Output:

Count of nodes present in circular linked list: 6

PHP

<!DOCTYPE html>
<html>
<body>
<?php
//Represents the node of list.
class Node{
    public $data;
    public $next;
    function __construct($data){
        $this->data = $data;
        $this->next = NULL;
    }
}
class CreateList{
    //Declaring head and tail pointer as null.
    private $head;
    private $tail;
    private $count;
    function __construct(){
        $this->count = 0;
        $this->head = NULL;
        $this->tail = NULL;
    }
    //This function will add the new node at the end of the list.
    function add($data){
        //Create new node
        $newNode = new Node($data);
        //Checks if the list is empty.
        if($this->head == NULL){
            //If list is empty, both head and tail would point to new node.
            $this->head = $newNode;
            $this->tail = $newNode;
            $newNode->next = $this->head;
        }
        else{
            //tail will point to new node.
            $this->tail->next = $newNode;
            //New node will become new tail.
            $this->tail = $newNode;
            //Since, it is circular linked list tail will point to head.
            $this->tail->next = $this->head;
        }
    }
        //This function will count the nodes of circular linked list
        function countNodes() {
            $current = $this->head;
            do{
                //Counts nodes by pointing to next node.
                $this->count++;
                $current = $current->next;
            }while($current != $this->head);
            echo "Count of nodes present in circular linked list: $this->count";
        }
        
}
$cl = new CreateList();
//Adds data to the list
$cl->add(1);
$cl->add(2);
$cl->add(4);
$cl->add(1);
$cl->add(2);
$cl->add(3);
//Counts the number of nodes present in the list
$cl->countNodes();
?>
</body>
</html>

Output:

Count of nodes present in circular linked list: 6

Next Topic#




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf