判断链表是否按升序排序

WLDLNHK WLDLNHK 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#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";
}
...全文

相关推荐

评论 5
收藏 已收藏
分享
返回顶部
WLDLNHK
C++ 语言 65,214 成员 250,514 内容