判断链表是否按升序排序
#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";
}
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";
}
...全文
相关推荐
链表的奇思妙想
**分配节点**:动态分配节点是为了在运行时创建新的节点,并且分配过程中可以进行初始化,同时便于判断分配是否成功。3.
C语言 数据结构之链表实现代码
排序链表:`void sort(PNODE pHead)`,这里使用冒泡排序算法对链表中的元素进行升序排列。
建立一个二叉树,判断是不是二叉排序树!
总之,这段代码提供了一个基础的框架,用于构建二叉树并判断其是否为二叉排序树。
sort排序算法,可以排序的
**插入位置判断**:通过`p1 == p2`来判断是否需要将节点插入到链表头部。4. **循环控制**:`while (temp !
c语言基于链表的学生成绩管理系统
### 链表的排序为了对学生成绩进行比较或分析,我们可能需要对链表中的节点进行排序。使用冒泡排序算法,并通过`swap`函数交换节点信息,可实现链表的升序或降序排列。


