已知顺序表中元素值递增有序。用算法实现将元素x查到表中适当的位置上,以保持顺序表的有序性。
第一种写法:
//已知顺序表中元素值递增有序。用算法实现将元素x查到表中适当位置上,以保持顺序表的有序性
#include"stdio.h"
#define DATATYPE1 int
#define MAXSIZE 100
typedef struct
{
DATATYPE1 datas[MAXSIZE];
int last;
}SEQUENLIST;
void insert_x_seqlist(SEQUENLIST *a,int x)
{
//在有序表中插入一个x,插入完仍有序
int i;
i=a->last;
while(x<a->datas[i])
{
a->datas[i+1]=a->datas[i];
i--;
}
a->datas[i+1]=x;
a->last++;
}
void print_seqlist(SEQUENLIST *a)
{
//依次打印顺序表a中的所有元素
int i;
printf("表中各元素为:");
for(i=1;i