判断一个非空单链表是否是递增有序的
程序员文章站
2023-11-02 15:27:04
直接附上代码,不理解请看置逆那篇,链接:https://www.cnblogs.com/biaobiao88/p/12042132.html #include #define int long long using namespace std; typedef stru ......
直接附上代码,不理解请看置逆那篇,链接:
#include<bits/stdc++.h> #define int long long using namespace std; typedef struct { int len; int *next; }node; //判断一个非空单链表是否是递增有序的 void inceart(node *list) { int flag = 0; for(int i = 0;i < list->len;i++) { if(list->next[i] > list->next[i + 1]) { flag = 1; break; } } if(flag == 1) { cout << "此非空单链表不是递增有序的" << endl; return ; } if(flag == 0) { cout << "此非空单链表是递增有序的" << endl; return ; } } signed main() { node *list; list = (node *)malloc(sizeof(node)); cout << "输入顺序表长度:"; cin >> list->len; list->next = (int *)malloc(sizeof(int)*list->len); cout << "输入顺序表元素:"; for(int i = 0;i < list->len;i++) cin >> list->next[i]; inceart(list); free(list->next);
free(list); return 0; }