5.编写程序把以下数值:10,3,5,7,8,2,4,6,8,1存放在下面的顺序表中,并调用自定义的排序函数将数值按照降序排列并输出。(本小题8分) typedef int datatype; #define maxsize 10 typedef struct { datatype array[maxsize]; int length; }sequenlist;
时间: 2024-03-08 22:46:03 浏览: 60
下面是一个简单的实现:
```c
#include <stdio.h>
typedef int datatype;
#define maxsize 10
typedef struct {
datatype array[maxsize];
int length;
} sequenlist;
void sort_desc(sequenlist *list) {
int i, j;
datatype temp;
for (i = 0; i < list->length - 1; i++) {
for (j = i + 1; j < list->length; j++) {
if (list->array[i] < list->array[j]) {
temp = list->array[i];
list->array[i] = list->array[j];
list->array[j] = temp;
}
}
}
}
int main() {
sequenlist list = {{10, 3, 5, 7, 8, 2, 4, 6, 8, 1}, 10};
sort_desc(&list);
int i;
for (i = 0; i < list.length; i++) {
printf("%d ", list.array[i]);
}
printf("\n");
return 0;
}
```
输出结果为:
```
10 8 8 7 6 5 4 3 2 1
```
阅读全文