判断链表是否按升序排序
#include<iostream>
using namespace std;
struct Node {
double a;
Node* next;
Node(int x) :a(x), next(NULL) {}
};
bool isInOrder(Node* first) {
Node* p = first;
if (p == NULL) return false;
while (p != NULL) {
if (p->a > p->next->a) return false;
p = p->next;
}
return true;
}
int main() {
int n;
double m;
cout << "please input the number of data:";
cin >> n;
cout << "please input the data:";
Node* first, * p,* f;
f = new Node(0);
first = f;
for (int i = 0; i < n; i++) {
cin >> m;
p = new Node(m);
f->next = p;
f = p;
}
first = first->next;
if (isInOrder(first) == true) cout << "This listnode is in right order./n";
else cout << "This listnode is not in right order./n";
}