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

📅  最后修改于: 2020-10-15 05:29:52             🧑  作者: Mango

问:程序从双向链表中查找最大值和最小值节点。

说明

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

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

算法

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

示例:

Python

#Represent a node of doubly linked list
class Node:
    def __init__(self,data):
        self.data = data;
        self.previous = None;
        self.next = None;
        
class MinMax:
    #Represent the head and tail of the doubly linked list
    def __init__(self):
        self.head = None;
        self.tail = None;
        
    #addNode() will add a node to the list
    def addNode(self, data):
        #Create a new node
        newNode = Node(data);
        
        #If list is empty
        if(self.head == None):
            #Both head and tail will point to newNode
            self.head = self.tail = newNode;
            #head's previous will point to None
            self.head.previous = None;
            #tail's next will point to None, as it is the last node of the list
            self.tail.next = None;
        else:
            #newNode will be added after tail such that tail's next will point to newNode
            self.tail.next = newNode;
            #newNode's previous will point to tail
            newNode.previous = self.tail;
            #newNode will become new tail
            self.tail = newNode;
            #As it is last node, tail's next will point to None
            self.tail.next = None;
            
    #MinimumNode() will find out minimum value node in the list
    def minimumNode(self):
        #Node current will point to head
        current = self.head;
        
        #Checks if list is empty
        if(self.head == None):
            print("List is empty");
            return 0;
        else:
            #Initially, min will store the value of head's data
            min = self.head.data;
            while(current != None):
                #If value of min is greater than current's data
                #Then, replace value of min with current node's data
                if(min > current.data):
                    min = current.data;
                current = current.next;
        return min;
        
    #MaximumNode() will find out maximum value node in the list
    def maximumNode(self):
        #Node current will point to head
        current = self.head;
        
        #Checks if list is empty
        if(self.head == None):
            print("List is empty");
            return 0;
        else:
            #Initially, max will store the value of head's data
            max = self.head.data;
            #If value of max is lesser than current's data
            #Then, replace value of max with current node's data
            while(current != None):
                if(current.data > max):
                    max = current.data;
                current = current.next;
        return max;
            
dList = MinMax();
#Add nodes to the list
dList.addNode(5);
dList.addNode(7);
dList.addNode(9);
dList.addNode(1);
dList.addNode(2);
 
#Prints the minimum value node in the list
print("Minimum value node in the list: "+ str(dList.minimumNode()));
#Prints the maximum value node in the list
print("Maximum value node in the list: "+ str(dList.maximumNode()));

输出:

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

C

#include 
 
//Represent a node of the doubly linked list

struct node{
    int data;
    struct node *previous;
    struct node *next;
};    
 
//Represent the head and tail of the doubly linked list
struct node *head, *tail = NULL;
 
//addNode() will add a node to the list
void addNode(int data) {
    //Create a new node
    struct node *newNode = (struct node*)malloc(sizeof(struct node));
    newNode->data = data;
    
    //If list is empty
    if(head == NULL) {
        //Both head and tail will point to newNode
        head = tail = newNode;
        //head's previous will point to NULL
        head->previous = NULL;
        //tail's next will point to NULL, as it is the last node of the list
        tail->next = NULL;
    }
    else {
        //newNode will be added after tail such that tail's next will point to newNode
        tail->next = newNode;
        //newNode's previous will point to tail
        newNode->previous = tail;
        //newNode will become new tail
        tail = newNode;
        //As it is last node, tail's next will point to NULL
        tail->next = NULL;
    }
}
 
//MinimumNode() will find out minimum value node in the list
int minimumNode() {
    //Node current will point to head
    struct node *current = head;
    int min;
    
    //Checks if list is empty
    if(head == NULL) {
        printf("List is empty\n");
        return 0;
    }
    else {
        //Initially, min will store the value of head's data
        min = head->data;
        while(current != NULL) {
            //If value of min is greater than current's data
            //Then, replace value of min with current node's data
            if(min > current->data)
                min = current->data;
            current = current->next;
        }
    }
    return min;
}
 
//MaximumNode() will find out maximum value node in the list
int maximumNode() {
    //Node current will point to head
    struct node *current = head;
    int max;
    
    //Checks if list is empty
    if(head == NULL) {
        printf("List is empty\n");
        return 0;
    }
    else {
        //Initially, max will store the value of head's data
        max = head->data;
        //If value of max is lesser than current's data
        //Then, replace value of max with current node's data
        while(current != NULL) {
            if(current->data > max) 
                max = current->data;
            current = current->next;
        }
    }
    return max;
}
 
int main()
{
    //Add nodes to the list
    addNode(5);
    addNode(7);
    addNode(9);
    addNode(1);
    addNode(2);
    
    //Prints the minimum value node in the list
    printf("Minimum value node in the list: %d\n", minimumNode());
    //Prints the maximum value node in the list
    printf("Maximum value node in the list: %d", maximumNode());
 
    return 0;
}

输出:

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

JAVA

public class MinMax {
    
    //Represent a node of the doubly linked list

    class Node{
        int data;
        Node previous;
        Node next;
        
        public Node(int data) {
            this.data = data;
        }
    }
    
    //Represent the head and tail of the doubly linked list
    Node head, tail = null;
    
    //addNode() will add a node to the list
    public void addNode(int data) {
        //Create a new node
        Node newNode = new Node(data);
        
        //If list is empty
        if(head == null) {
            //Both head and tail will point to newNode
            head = tail = newNode;
            //head's previous will point to null
            head.previous = null;
            //tail's next will point to null, as it is the last node of the list
            tail.next = null;
        }
        else {
            //newNode will be added after tail such that tail's next will point to newNode
            tail.next = newNode;
            //newNode's previous will point to tail
            newNode.previous = tail;
            //newNode will become new tail
            tail = newNode;
            //As it is last node, tail's next will point to null
            tail.next = null;
        }
    }
    
    //MinimumNode() will find out minimum value node in the list
    public int minimumNode() {
        //Node current will point to head
        Node current = head;
        int min;
        
        //Checks if list is empty
        if(head == null) {
            System.out.println("List is empty");
            return 0;
        }
        else {
            //Initially, min will store the value of head's data
            min = head.data;
            while(current != null) {
                //If the value of min is greater than the current's data

                //Then, replace the value of min with current node's data

                if(min > current.data)
                    min = current.data;
                current = current.next;
            }
        }
        return min;
    }
    
    //MaximumNode() will find out maximum value node in the list
    public int maximumNode() {
        //Node current will point to head
        Node current = head;
        int max;
        
        //Checks if list is empty
        if(head == null) {
            System.out.println("List is empty");
            return 0;
        }
        else {
            //Initially, max will store the value of head's data
            max = head.data;
            //If value of max is lesser than current's data
            //Then, replace value of max with current node's data
            while(current != null) {
                if(current.data > max) 
                    max = current.data;
                current = current.next;
            }
        }
        return max;
    }
    
    public static void main(String[] args) {
        
        MinMax dList = new MinMax();
        //Add nodes to the list
        dList.addNode(5);
        dList.addNode(7);
        dList.addNode(9);
        dList.addNode(1);
        dList.addNode(2);
        
        //Prints the minimum value node in the list
        System.out.println("Minimum value node in the list: "+ dList.minimumNode());
        //Prints the maximum value node in the list
        System.out.println("Maximum value node in the list: "+ dList.maximumNode());
    }
}

输出:

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

C#

using System; 
namespace DoublyLinkedList 
{                     
    public class Program
    {
        //Represent a node of the doubly linked list

        public class Node{
            public T data;
            public Node previous;
            public Node next;
            
            public Node(T value) {
                data = value;
            }
        }
        
        public class MinMax where T : IComparable{
            //Represent the head and tail of the doubly linked list
            protected Node head = null;             
             protected Node tail = null;
            
            //addNode() will add a node to the list
            public void addNode(T data) {
                //Create a new node
                Node newNode = new Node(data);
 
                //If list is empty
                if(head == null) {
                    //Both head and tail will point to newNode
                    head = tail = newNode;
                    //head's previous will point to null
                    head.previous = null;
                    //tail's next will point to null, as it is the last node of the list
                    tail.next = null;
                }
                else {
                    //newNode will be added after tail such that tail's next will point to newNode
                    tail.next = newNode;
                    //newNode's previous will point to tail
                    newNode.previous = tail;
                    //newNode will become new tail
                    tail = newNode;
                    //As it is last node, tail's next will point to null
                    tail.next = null;
                }
            }
            
            //MinimumNode() will find out minimum value node in the list
            public T minimumNode() {
                //Node current will point to head
                Node current = head;
                T min;
 
                //Checks if list is empty
                if(head == null) {
                    Console.WriteLine("List is empty");
                    return default(T);
                }
                else {
                    //Initially, min will store the value of head's data
                    min = head.data;
                    while(current != null) {
                        //If value of min is greater than current's data
                        //Then, replace value of min with current node's data
                        if(min.CompareTo(current.data) > 0)
                            min = current.data;
                        current = current.next;
                    }
                }
                return min;
            }
    
            //MaximumNode() will find out maximum value node in the list
            public T maximumNode() {
                //Node current will point to head
                Node current = head;
                T max;
 
                //Checks if list is empty
                if(head == null) {
                    Console.WriteLine("List is empty");
                    return default(T);
                }
                else {
                    //Initially, max will store the value of head's data
                    max = head.data;
                    //If value of max is lesser than current's data
                    //Then, replace value of max with current node's data
                    while(current != null) {
                        if(current.data.CompareTo(max) > 0) 
                            max = current.data;
                        current = current.next;
                    }
                }
                return max;
            }
        }
        
        public static void Main()
        {
            MinMax dList = new MinMax();
            //Add nodes to the list
            dList.addNode(5);
            dList.addNode(7);
            dList.addNode(9);
            dList.addNode(1);
            dList.addNode(2);
 
            //Prints the minimum value node in the list
            Console.WriteLine("Minimum value node in the list: "+ dList.minimumNode());
            //Prints the maximum value node in the list
            Console.WriteLine("Maximum value node in the list: "+ dList.maximumNode());
        }    
    }
}           

输出:

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

PHP:




data = $data;
    }
}
class MinMax{
    //Represent the head and tail of the doubly linked list
    public $head;
    public $tail;
    function __construct(){
        $this->head = NULL;
        $this->tail = NULL;
    }
    
    //addNode() will add a node to the list
    function addNode($data){
        //Create a new node
        $newNode = new Node($data);
        
        //If list is empty
        if($this->head == NULL) {
            //Both head and tail will point to newNode
            $this->head = $this->tail = $newNode;
            //head's previous will point to NULL
            $this->head->previous = NULL;
            //tail's next will point to NULL, as it is the last node of the list
            $this->tail->next = NULL;
        }
        else {
            //newNode will be added after tail such that tail's next will point to newNode
            $this->tail->next = $newNode;
            //newNode's previous will point to tail
            $newNode->previous = $this->tail;
            //newNode will become new tail
            $this->tail = $newNode;
            //As it is last node, tail's next will point to NULL
            $this->tail->next = NULL;
        }
    }
    
    //MinimumNode() will find out minimum value node in the list
    function minimumNode() {
        //Node current will point to head
        $current = $this->head;
        
        //Checks if list is empty
        if($this->head == NULL) {
            print("List is empty 
"); return 0; } else { //Initially, min will store the value of head's data $min = $this->head->data; while($current != NULL) { //If value of min is greater than current's data //Then, replace value of min with current node's data if($min > $current->data) $min = $current->data; $current = $current->next; } } return $min; } //MaximumNode() will find out maximum value node in the list function maximumNode() { //Node current will point to head $current = $this->head; //Checks if list is empty if($this->head == NULL) { print("List is empty
"); return 0; } else { //Initially, max will store the value of head's data $max = $this->head->data; //If value of max is lesser than current's data //Then, replace value of max with current node's data while($current != NULL) { if($current->data > $max) $max = $current->data; $current = $current->next; } } return $max; } } $dList = new MinMax(); //Add nodes to the list $dList->addNode(5); $dList->addNode(7); $dList->addNode(9); $dList->addNode(1); $dList->addNode(2); //Prints the minimum value node in the list print("Minimum value node in the list: " . $dList->minimumNode()); print("
"); //Prints the maximum value node in the list print("Maximum value node in the list: " . $dList->maximumNode()); ?>

输出:

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