#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 20
typedef int ElemType;
typedef
struct
{ ElemType
a[MAXSIZE];
int
length;
}SqList;
SqList
a,b,c;
void creat_list(SqList *L);
void out_list(SqList *L);
void insert_sq(SqList *L,ElemType e);
main()
{ int i,k,loc; ElemType e;
do { printf("\n\n\n");
printf("\n
1. 建立非递减有序线性表 " );
printf("\n
2. 插入元素e");
printf("\n
3. 结束程序运行");
printf("\n======================================");
printf("\n
请输入您的选择(1,2,3)");
scanf("%d",&k);
switch(k)
{ case 1:{ creat_list(&a);
out_list(&a);
} break;
case 2:{ printf("\n e=?"); scanf("%d",&e);
insert_sq(&a,e);
out_list(&a);
} break;
}
}while(k!=3);
printf("\n
再见!");
printf("\n
打回车键,返回。");
}
void creat_list(SqList *L)
{ int i;
printf("\n n=?"); scanf("%d",&L->length);
for(i=0;i<L->length;i++){ printf("\n data %d=?",i);
scanf("%d",&(L->a[i]));
}
}
void out_list(SqList *L)
{ int i;
printf("\n");
for(i=0;i<=L->length-1;i++) printf("%10d",L->a[i]);
printf("\n\n
打回车键,继续。");
}
void insert_sq(SqList *L,ElemType e)
{ int j,i=0,k;
for(k=0;k<L->length;k++){
if(L->a[k]<=e&&L->a[k+1]>=e){i=k+1;break;}
}
if (L->length==MAXSIZE) printf("\n overflow !");
else if(i<0)
printf("\n erroe i !");
else { for(j=L->length-1; j>i-1; j--) L->a[j+1]=L->a[j];
L->a[i]=e;
L->length++;
}
}