C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Q. Program to create a circular linked list of n nodes and count the number of nodes.
ExplanationIn 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
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#
|