题目是:
设顺序表L中数组元素递增有序,写一算法,将X插入到顺序表的适当位置使表仍然有序,我写的程序是下面的,但是得不到想要的结果:
#define M 100
typedef int ItemType;
typedef struct {
int len;
ItemType items[M];
} Vector;
int createCircle(int population, Vector* vector)
{
int i;
if(population>M) return 0;
for (i=0; i<population; i++)
{
vector->items[i]=i+1;
}
vector->len=population;
return 1;
}
void insertOrderList(Vector* vector ,ItemType x )
{
int i;
if (vector->len>=100)
printf("overflow");
for (i=vector->len;i>1&&vector->items[i-1]>x;i--)
{
vector->items[i]=vector->items[i-1] ;
}
vector->items[i] =x;
vector->len++;
}
main()
{
int n,m,i;
Vector vector;
printf("Please input the length =");
scanf("%d",&n);
createCircle(n,&vector);
printf("Please type the number what to insert =");
scanf("%d",m);
insertOrderList(&vector,m);
for(i=0;i<=n;i++)
printf("%d ",vector.items[i]);
scanf("%d");
}
我的思路是,输入表长15,然后创建顺序表 1.2.3.4.。。。15 ,然后再输入插入数3,将3插入到表中,再输出新表,为什么输出结果不对??