📌  相关文章
📜  数据结构示例-查找单向链表中最大和最小值节点

📅  最后修改于: 2020-10-15 04:48:46             🧑  作者: Mango

程序从单个链接列表中查找最大值和最小值节点

说明

在此程序中,我们需要在给定的单链列表中找出最小值和最大值节点。

我们将维护两个变量min和max。最小值将保存最小值节点,最大值将保存最大值节点。在上面的示例中,1将是最小值节点,8将是最大值节点。下面给出了找到最大和最小节点的算法。

算法

  • 创建一个具有两个属性的类Node:data和next。下一个是指向列表中下一个节点的指针。
  • 创建另一个具有两个属性的MinMax类:head和tail。
  • addNode()将向列表添加一个新节点:
    1. 创建一个新节点。
    2. 它首先检查head是否等于null,这意味着列表为空。
    3. 如果列表为空,则头和尾都将指向新添加的节点。
    4. 如果列表不为空,则新节点将被添加到列表的末尾,使得尾部的下一个将指向新添加的节点。这个新节点将成为列表的新尾部。
  • minNode()将显示最小值节点:
    1. 定义变量min并使用head的数据对其进行初始化。
    2. 节点电流将指向头部。
    3. 通过将每个节点的数据与min进行比较来遍历列表。
    4. 如果min大于当前数据,则min将保存当前数据。
    5. 在列表的末尾,变量min将保存最小值节点。
    6. 显示最小值。
  • maxNode()将显示最大值节点:
    1. 定义变量max,并使用head的数据对其进行初始化。
    2. 节点电流将指向头部。
    3. 通过比较每个节点的数据与最大值来遍历列表。
    4. 如果max小于当前数据,则max将保存当前数据。
    5. 在列表的末尾,变量max将保存最大值节点。
    6. 显示最大值。

示例:

Python

#Represent a node of the singly linked list
class Node:
    def __init__(self,data):
        self.data = data;
        self.next = None;
        
class MinMax:
    #Represent the head and tail of the singly linked list
    def __init__(self):
        self.head = None;
        self.tail = None;
        
    #addNode() will add a new node to the list
    def addNode(self, data):
        #Create a new node
        newNode = Node(data);
        
        #Checks if the list is empty
        if(self.head == None):
            #If list is empty, both head and tail will point to new node
            self.head = newNode;
            self.tail = newNode;
        else:
            #newNode will be added after tail such that tail's next will point to newNode
            self.tail.next = newNode;
            #newNode will become new tail of the list
            self.tail = newNode;
            
    #minNode() will find out the minimum value node in the list
    def minNode(self):
        current = self.head;
        
        if(self.head == None):
            print("List is empty");
        else:
            #Initializing min with head node data
            min = self.head.data;
            
            while(current != None):
                #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;
            print("Minimum value node in the list: " + str(min));
            
    #maxNode() will find out the maximum value node in the List
    def maxNode(self):
        current = self.head;
        
        if(self.head == None):
            print("List is empty");
        else:
            #Initializing max with head node data
            max = self.head.data;
            
            while(current != None):
                #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;
            print("Maximum value node in the list: " + str(max));
 
sList = MinMax();    
        
#Adds data to the list
sList.addNode(5);
sList.addNode(8);
sList.addNode(1);
sList.addNode(6);
 
#Display the minimum value node in the list
sList.minNode();
 
#Display the maximum value node in the list
sList.maxNode();

输出:

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

C

#include 
 
//Represent a node of the singly linked list
struct node{
    int data;
    struct node *next;
};    
 
//Represent the head and tail of the singly linked list
struct node *head, *tail = NULL;
 
//addNode() will add a new node to the list
void addNode(int data) {
    //Create a new node
    struct node *newNode = (struct node*)malloc(sizeof(struct node));
    newNode->data = data;
    newNode->next = NULL;
    
    //Checks if the list is empty
    if(head == NULL) {
        //If list is empty, both head and tail will point to new node
        head = newNode;
        tail = newNode;
    }
    else {
        //newNode will be added after tail such that tail's next will point to newNode
        tail->next = newNode;
        //newNode will become new tail of the list
        tail = newNode;
    }
}
 
//minNode() will find out the minimum value node in the list
void minNode() {
    struct node *current = head;
    int min;
    
    if(head == NULL) {
        printf("List is empty \n");
    }
    else {
        //Initializing min with head node data
        min = head->data;
        
        while(current != NULL){
             //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;
        }        
        printf("Minimum value node in the list: %d\n", min);
    }
}
    
//maxNode() will find out the maximum value node in the list
void maxNode() {
    struct node *current = head;
    int max;
    
    if(head == NULL) {
        printf("List is empty \n");
    }
    else {
        //Initializing max with head node data
        max = head->data;
        
        while(current != NULL){
             //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;
        }             
        printf("Maximum value node in the list: %d\n", max);
    }
}
    
int main()
{
    //Adds data to the list
    addNode(5);
    addNode(8);
    addNode(1);
    addNode(6);
 
    //Display the minimum value node in the list
    minNode();
    
    //Display the maximum value node in the list
    maxNode();
        
    return 0;
}

输出:

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

JAVA

public class MinMax {
    
    //Represent a node of the singly linked list
    class Node{
        int data;
        Node next;
        
        public Node(int data) {
            this.data = data;
            this.next = null;
        }
    }
 
    //Represent the head and tail of the singly linked list
    public Node head = null;
    public Node tail = null;
    
    //addNode() will add a new node to the list
    public void addNode(int data) {
        //Create a new node
        Node newNode = new Node(data);
        
        //Checks if the list is empty
        if(head == null) {
            //If list is empty, both head and tail will point to new node
            head = newNode;
            tail = newNode;
        }
        else {
            //newNode will be added after tail such that tail's next will point to newNode
            tail.next = newNode;
            //newNode will become new tail of the list
            tail = newNode;
        }
    }
    
    //minNode() will find out the minimum value node in the list
    public void minNode() {
        Node current = head;
        int min;
        
        if(head == null) {
            System.out.println("List is empty");
        }
        else {
            //Initializing min with head node data
            min = head.data;
            
            while(current != null){
                 //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;
            }        
            System.out.println("Minimum value node in the list: "+ min);
        }
    }
        
    //maxNode() will find out the maximum value node in the list
    public void maxNode() {
        Node current = head;
        int max;
        
        if(head == null) {
            System.out.println("List is empty");
        }
        else {
            //Initializing max with head node data
            max = head.data;
            
            while(current != null){
                 //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;
            }             
            System.out.println("Maximum value node in the list: "+ max);
        }
    }
    
    public static void main(String[] args) {
        
        MinMax sList = new MinMax();    
        
        //Adds data to the list
        sList.addNode(5);
        sList.addNode(8);
        sList.addNode(1);
        sList.addNode(6);
 
        //Display the minimum value node in the list
        sList.minNode();
        
        //Display the maximum value node in the list
        sList.maxNode();
    }
}

输出:

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

C#

using System;
                    
public class CreateList
{
    //Represent a node of the singly linked list
    public class Node{
        public T data;
        public Node next;
        
        public Node(T value) {
            data = value;
            next = null;
        }
    }
        
    public class MinMax where T : IComparable{
        //Represent the head and tail of the singly linked list
        public Node head = null;             
         public Node tail = null;
    
        //addNode() will add a new node to the list
        public void addNode(T data) {
            //Create a new node
            Node newNode = new Node(data);
 
            //Checks if the list is empty
            if(head == null) {
                //If list is empty, both head and tail will point to new node
                head = newNode;
                tail = newNode;
            }
            else {
                //newNode will be added after tail such that tail's next will point to newNode
                tail.next = newNode;
                //newNode will become new tail of the list
                tail = newNode;
            }
        }
    
        //minNode() will find out the minimum value node in the list
        public void minNode() {
            Node current = head;
            T min;
 
            if(head == null) {
                Console.WriteLine("List is empty");
            }
            else {
                //Initializing min with head node data
                min = head.data;
 
                while(current != null){
                     //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;
                }        
                Console.WriteLine("Minimum value node in the list: "+ min);
            }
        }
        
        //maxNode() will find out the maximum value node in the list
        public void maxNode() {
            Node current = head;
            T max;
 
            if(head == null) {
                Console.WriteLine("List is empty");
            }
            else {
                //Initializing max with head node data
                max = head.data;
 
                while(current != null){
                     //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;
                }             
                Console.WriteLine("Maximum value node in the list: "+ max);
            }
        }
    }
    
    public static void Main()
    {
        MinMax sList = new MinMax();    
        
        //Adds data to the list
        sList.addNode(5);
        sList.addNode(8);
        sList.addNode(1);
        sList.addNode(6);
 
        //Display the minimum value node in the list
        sList.minNode();
        
        //Display the maximum value node in the list
        sList.maxNode();
    }
}

输出:

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

PHP:




data = $data;
        $this->next = NULL;
    }
}
class MinMax{
    //Represent the head and tail of the singly linked list
    public $head;
    public $tail;
    function __construct(){
        $this->head = NULL;
        $this->tail = NULL;
    }
    
    //addNode() will add a new node to the list
    function addNode($data) {
        //Create a new node
        $newNode = new Node($data);
        
        //Checks if the list is empty
        if($this->head == NULL) {
            //If list is empty, both head and tail will point to new node
            $this->head = $newNode;
            $this->tail = $newNode;
        }
        else {
            //newNode will be added after tail such that tail's next will point to newNode
            $this->tail->next = $newNode;
            //newNode will become new tail of the list
            $this->tail = $newNode;
        }
    }
    
    //minNode() will find out the minimum value node in the list
    function minNode() {
        $current = $this->head;
        
        if($this->head == null) {
            print("List is empty 
"); } else { //Initializing min with head node data $min = $this->head->data; while($current != null){ //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; } print("Minimum value node in the list: ". $min); } } //maxNode() will find out the maximum value node in the list function maxNode() { $current = $this->head; if($this->head == null) { print("List is empty
"); } else { //Initializing max with head node data $max = $this->head->data; while($current != null){ //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; } print("
Maximum value node in the list: ". $max); } } } $sList = new MinMax(); //Adds data to the list $sList->addNode(5); $sList->addNode(8); $sList->addNode(1); $sList->addNode(6); //Display the minimum value node in the list $sList->minNode(); //Display the maximum value node in the list $sList->maxNode(); ?>

输出:

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