2017-05-23 01:43

线性表插入排序问题求助

`````` #include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 5
#define Error 0
#define OK 1
//用typedef定义类型
typedef int Status;//就是int 类型,代表标准函数返回。
typedef int ElemType;//
typedef struct {
ElemType date[MAXSIZE];
int length;//线性表当前的长度
}SqList;
//创建一个线性表
Status createList(SqList *L) {
int i,n;
printf("请输入元素的个数:\n");
scanf_s("%d", &n);
printf("请输入元素:\n");
for (i = 0; i < n; i++) {
scanf_s("%d", &L->date[i]);
}
L->length = 10;
return OK;
}
Status ListInsert(SqList *L, int i, ElemType e) {
int k;
if (L->length == MAXSIZE) {
return Error;
}
if (i<1 || i>L->length + 1) {
return Error;
}
if (i <= L->length) {
for (k = L->length-1; k >=i-1; k--) {
L->date[k + 1] = L->date[k];
}
}
L->date[i-1] = e;
L->length++;
return OK;
}
Status ListTraverse(SqList L) {//遍历线性表
for (int i = 0; i < L.length; i++)
printf("%d ", L.date[i]);
return OK;
}
int main() {
SqList L;
int input_number;
ElemType input_value;
createList(&L);
printf("请输入插入的位置:");
scanf_s("%d", &input_number);
printf("\n请输入插入的值:");
scanf_s("%d", &input_value);
ListInsert(&L, input_number, input_value);
ListTraverse(L);
system("pause");
}
``````