已知顺序表L中的元素递增有序排列,设计算法将元素x插入到表L中并保持表L仍递增有序,用dev c++写出源程序
时间: 2024-05-24 14:11:20 浏览: 8
#include <stdio.h>
#define MAX_SIZE 100
typedef struct {
int data[MAX_SIZE];
int length;
} SqList;
void printList(SqList L) {
int i;
for (i = 0; i < L.length; i++) {
printf("%d ", L.data[i]);
}
printf("\n");
}
void insertElem(SqList *L, int x) {
int i, j;
for (i = 0; i < L->length; i++) {
if (L->data[i] > x) {
break;
}
}
for (j = L->length - 1; j >= i; j--) {
L->data[j+1] = L->data[j];
}
L->data[i] = x;
L->length++;
}
int main() {
SqList L = {{1, 3, 5, 7, 9}, 5};
int x;
printf("原有序表:");
printList(L);
printf("请输入要插入的元素:");
scanf("%d", &x);
insertElem(&L, x);
printf("插入后的有序表:");
printList(L);
return 0;
}
相关推荐
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)