为什么C++链表程序只显示最大数而忽略最小数?

Why the program of linked lists in C++ only displays the maximum number and ignores minimum number?

我使用 C++ 编写了这段代码来显示链表的最大和最小数,当我 运行 代码时我可以获得最大数但我始终无法获得最小数及其值零:

#include <iostream>

using namespace std;

class Node {
public:
    int a;
    Node* next; 
};

Node* createNode(int num) {
    Node* n = new Node();
    n->a = num;
    n->next = NULL;
    return n;
}

void addValue(int a, Node** h) {

    Node* y = createNode(a), * p = *h;
    if (*h == NULL)
        *h = y;
    else {
        while (p->next != NULL) 
            p = p->next;    
        p->next = y;
    }
}

void display(Node* x) {
    while (x != NULL) {
        cout << x->a << " ";
        x = x->next;
    }
}

double sumNodes(Node** h) {
    double sum = 0;
    Node* x = *h;

    while (x != NULL) {
        sum += x->a;
        x = x->next;
    }
    return sum;
}

void minmax(Node* numH1, Node* numH2) {
    double max = 0, min = 0;

    while (numH1 != NULL) {
        if (max < numH1->a)
            max = numH1->a;
        numH1 = numH1->next;
    }
    cout << "max: " << max << endl;

    while (numH2 != NULL) {

    if (min > numH2->a)
        min = numH2->a;
    numH2 = numH2->next;
    }
    cout << "min: " << min;

}

int main() {
    int num = 0;  char choice;
    Node* head1 = NULL;
    Node* head2 = NULL;
    do {
        cout << "Enter a number : ";
        cin >> num;
        addValue(num, &head1);
        cout << "Enter [Y] to add another number : ";
        cin >> choice;
    } while (choice == 'Y');

    cout << endl;
     minmax(head1, head2);
    
    return 0;
}

我必须使用一个函数来显示链接 lsit 的最大和最小数字,不确定到底出了什么问题

您必须将 min 设置为较高的值,如果将其设置为 0 则永远不会成立

if (min > numH2->a)

也一样

 #include <limits>
 ....
 double max = 0, min = std::numeric_limits<double>::max();

或者如果您不允许使用限制 (omg wtf)

 double max = 0, min = 999999999;

实际上这里是 DBL_MAX 给你的(这是 numeric_limits 会给你的)

 double max = 0, min = 1.79769e+308;

还因为这是双(有符号)最大值,应设置为尽可能小的值

 double max = 2.22507e-308; // DBL_MIN
 double min = 1.79769e+308; // DBL_MAX