📌  相关文章
📜  数据结构示例-查找循环列表中的最大和最小值节点

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

程序从循环链表中查找最大值和最小值节点

说明

在此程序中,我们将创建一个循环链接列表,然后遍历该列表以找出最小和最大节点。

9-> 5-> 2-> 7-> 3

我们将维护两个变量min和max。最小值将保存最小值节点,最大值将保存最大值节点。在上面的示例中,2将是最小值节点,而9将是最大值节点。

算法

  • 定义一个Node类,该类代表列表中的一个节点。它具有两个属性数据,下一个将指向下一个节点。
  • 定义另一个用于创建循环链接列表的类,它具有两个节点:head和tail。
  • minNode()将print出最小值节点:
    1. 定义变量min并使用head的数据进行初始化。
    2. 电流将指向头部。
    3. 通过将每个节点的数据与min进行比较来遍历列表。
    4. 如果min>当前数据,则min将保存当前数据。
    5. 在列表的末尾,变量min将保存最小值节点。
    6. 打印最小值。
  • maxNode()将打印出最大值节点:
    1. 定义变量max并使用head的数据进行初始化。
    2. 电流将指向头部。
    3. 通过比较每个节点的数据与最大值来遍历列表。
    4. 如果max> current的数据,则max将保存当前的数据。
    5. 在列表的末尾,变量max将保存最大值节点。
    6. 打印最大值。

示例:

Python

#Represents the node of 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.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;
            
    #Finds out the minimum value node in the list
    def minNode(self):
        current = self.head;
        #Initializing min to initial node data
        minimum = self.head.data;
        if(self.head == None):
            print("List is empty");
        else:
            while(True):
                #If current node's data is smaller than min
                #Then replace value of min with current node's data
                if(minimum > current.data):
                    minimum = current.data;
                current= current.next;
                if(current == self.head):
                    break;
        print("Minimum value node in the list: "+ str(minimum));
 
    #Finds out the maximum value node in the list
    def maxNode(self):
        current = self.head;
        #Initializing max to initial node data
        maximum = self.head.data;
        if(self.head == None):
            print("List is empty");
        else:
            while(True):
                #If current node's data is greater than max
                #Then replace value of max with current node's data
                if(maximum < current.data):
                    maximum = current.data;
                current= current.next;
                if(current == self.head):
                    break;
        print("Maximum value node in the list: "+ str(maximum));
 
class CircularLinkedList:
    cl = CreateList();
    #Adds data to the list
    cl.add(5);
    cl.add(20);
    cl.add(10);
    cl.add(1);
    #Prints the minimum value node in the list
    cl.minNode();
    #Prints the maximum value node in the list
    cl.maxNode();

输出:

Minimum value node in the list: 1
Maximum value node in the list: 20

C

#include 
#include 
#include 
 
//Represents the node of list.
struct node{
    int data;
    struct node *next;
};
 
//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;
    }
}
 
//Finds out the minimum value node in the list
void minNode() {
    struct node *current = head;
    //Initializing min to initial node data
    int min = head->data;
    if(head == NULL) {
        printf("\nList is empty");
    }
    else {
         do{
             //If current node's data is smaller than min
             //Then replace value of min with current node's data
             if(min > current->data) {
                 min = current->data;
             }
             current= current->next;
        }while(current != head);
        
        printf("Minimum value node in the list: %d", min);
    }
}
        
//Finds out the maximum value node in the list
void maxNode() {
    struct node *current = head;
    //Initializing max to initial node data
    int max = head->data;
    if(head == NULL) {
        printf("\nList is empty");
    }
    else {
         do{
             //If current node's data is greater than max
             //Then replace value of max with current node's data
             if(max < current->data) {
                 max = current->data;
             }
             current= current->next;
            }while(current != head);
         
        printf("\nMaximum value node in the list: %d", max);
    }
}
    
int main()
{
    //Adds data to the list
    add(5);
    add(20);
    add(10);
    add(1);
    //Prints the minimum value node in the list
    minNode();
    //Prints the maximum value node in the list
    maxNode();
   
   return 0;
}

输出:

Minimum value node in the list: 1
Maximum value node in the list: 20

JAVA

public class MinMax {
    //Represents the node of list.
    public class Node{
        int data;
        Node next;
        public Node(int data) {
            this.data = data;
        }
    }
    
    //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 points to head.
            tail.next = head;
        }
    }
    
    //Finds out the minimum value node in the list
    public void minNode() {
        Node current = head;
        //Initializing min to initial node data
        int min = head.data;
        if(head == null) {
            System.out.println("List is empty");
        }
        else {
             do{
                 //If current node's data is smaller than min
                 //Then replace value of min with current node's data
                 if(min > current.data) {
                     min = current.data;
                 }
                 current= current.next;
            }while(current != head);
        
            System.out.println("Minimum value node in the list: "+ min);
        }
    }
        
    //Finds out the maximum value node in the list
    public void maxNode() {
        Node current = head;
        //Initializing max to initial node data
        int max = head.data;
        if(head == null) {
            System.out.println("List is empty");
        }
        else {
             do{
                 //If current node's data is greater than max
                 //Then replace value of max with current node's data
                 if(max < current.data) {
                     max = current.data;
                 }
                 current= current.next;
                }while(current != head);
             
            System.out.println("Maximum value node in the list: "+ max);
        }
    }
                
    public static void main(String[] args) {
        MinMax cl = new MinMax();
        //Adds data to the list
        cl.add(5);
        cl.add(20);
        cl.add(10);
        cl.add(1);
        //Prints the minimum value node in the list
        cl.minNode();
        //Prints the maximum value node in the list
        cl.maxNode();
    }
}

输出:

Minimum value node in the list: 1
Maximum value node in the list: 20

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 where T : IComparable {
            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;
                }
            }
            
            
            //Finds out the minimum value node in the list
            public void minNode() {
                Node current = head;
                //Initializing min to initial node data
                T min = head.data;
                if(head == null) {
                    Console.WriteLine("List is empty");
                }
                else {
                     do{
                         //If current node's data is smaller than min
                         //Then replace value of min with current node's data
                         if(min.CompareTo(current.data) > 0) {
                             min = current.data;
                         }
                         current= current.next;
                    }while(current != head);
                
                    Console.WriteLine("Minimum value node in the list: "+ min);
                }
            }
            
            //Finds out the maximum value node in the list
            public void maxNode() {
                Node current = head;
                //Initializing max to initial node data
                T max = head.data;
                if(head == null) {
                    Console.WriteLine("List is empty");
                }
                else {
                     do{
                         //If current node's data is greater than max
                         //Then replace value of max with current node's data
                         if(max.CompareTo(current.data) < 0) {
                            
                             max = current.data;
                         }
                         current= current.next;
                        }while(current != head);
                     
                    Console.WriteLine("Maximum value node in the list: "+ max);
                }
            }
        }
        
        public static void Main()
        {
            
        CreateList cl = new CreateList();
        //Adds data to the list
        cl.add(5);
        cl.add(20);
        cl.add(10);
        cl.add(1);
        //Prints the minimum value node in the list
        cl.minNode();
        //Prints the maximum value node in the list
        cl.maxNode();
        }    
    }
}

输出:

Minimum value node in the list: 1
Maximum value node in the list: 20

PHP:




data = $data;
        $this->next = NULL;
    }
}
class CreateList{
    //Declaring head and tail pointer as null.
    private $head;
    private $tail;
    function __construct(){
        $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;
        }
    }
    
    //Finds out the minimum value node in the list
    function minNode() {
        $current = $this->head;
        //Initializing min to initial node data
        $min = $this->head->data;
        if($this->head == NULL) {
            echo "List is empty";
        }
        else {
             do{
                 //If current node's data is smaller than min
                 //Then replace value of min with current node's data
                 if($min > $current->data) {
                     $min = $current->data;
                 }
                 $current= $current->next;
            }while($current != $this->head);
        
            echo "Minimum value node in the list: $min 
"; } } //Finds out the maximum value node in the list function maxNode() { $current = $this->head; //Initializing max to initial node data $max = $this->head->data; if($this->head == NULL) { echo "List is empty"; } else { do{ //If current node's data is greater than max //Then replace value of max with current node's data if($max < $current->data) { $max = $current->data; } $current= $current->next; }while($current != $this->head); echo "Maximum value node in the list: $max
"; } } } $cl = new CreateList(); //Adds data to the list $cl->add(5); $cl->add(20); $cl->add(10); $cl->add(1); //Prints the minimum value node in the list $cl->minNode(); //Prints the maximum value node in the list $cl->maxNode(); ?>

输出:

Minimum value node in the list: 1
Maximum value node in the list: 20