📅  最后修改于: 2020-10-15 06:01:10             🧑  作者: Mango
在此程序中,我们必须找出循环链接列表中存在的节点数。我们首先创建循环链接列表,然后遍历列表并将变量“ count”增加1。
#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();
输出:
Count of nodes present in circular linked list: 6
#include
#include
#include
//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;
}
输出:
Count of nodes present in circular linked list: 6
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();
}
}
输出:
Count of nodes present in circular linked list: 6
using System;
namespace CircularLinkedList
{
public class Program
{
//Represents the node of list.
public class Node{
public T data;
public Node next;
public Node(T value) {
data = value;
next = null;
}
}
public class CreateList{
int count = 0;
protected Node head = null;
protected Node tail = null;
//This function will add the new node at the end of the list.
public void add(T data){
//Create new node
Node newNode = new Node(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 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 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();
}
}
}
输出:
Count of nodes present in circular linked list: 6
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();
?>
输出:
Count of nodes present in circular linked list: 6