📌  相关文章
📜  数据结构示例-创建n个节点的循环链表并计算节点数

📅  最后修改于: 2020-10-15 06:01:10             🧑  作者: Mango

问:程序创建一个n个节点的循环链表,并计算节点数。

说明

在此程序中,我们必须找出循环链接列表中存在的节点数。我们首先创建循环链接列表,然后遍历列表并将变量“ count”增加1。

算法

  • 定义一个Node类,该类代表列表中的一个节点。它具有两个属性数据,下一个将指向下一个节点。
  • 定义另一个用于创建循环链接列表的类,它具有两个节点:head和tail。它有两种方法:add()和display()。
  • add()会将节点添加到列表中:
    1. 首先检查size是否为null或head为null。然后它将节点插入为头。
    2. 头部和尾部都将指向新添加的节点。
    3. 如果head不为空,则新节点将是新尾,并且新尾将指向头,因为它是一个循环链接列表。
  • countNodes()将计算列表中存在的节点数。
    1. 定义新节点电流,该电流将指向头节点。
    2. 遍历列表以使当前节点指向列表中的下一个节点,直到当前节点再次指向节点,从而对节点进行计数。

示例:

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();

输出:

Count of nodes present in circular linked list: 6

C

#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

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();
        }
}

输出:

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{
            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

PHP:




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