//从链表中插入5个数,并按小到大排序后输出
代码:
#include <cstdio>
#include <algorithm>
using namespace std;
struct Node {
int Element;
struct Node* Next;//指向下一个结点
};
bool comp(Node lhs, Node rhs) {
return lhs.Element < rhs.Element;
}
int main() {
struct Node L[5];
for (int i = 0; i < 5; ++i) {
scanf_s("%d", &L[i].Element);
}
sort(L, L + 5, comp);//排序
for (int i = 0; i < 5; ++i) {
if(i==4)
printf("%d\n", L[i].Element);
else
printf("%d ", L[i].Element);
}
}
运行结果: