(顺序有序表)插入元素

#include<stdio.h>
typedef int A;
const int LIST_INIT_SIZE=100;
const int LISTINCRMENT=10;
typedef struct
{
	A *elem;
	int Length;
	int Listsize;
	int incrementsize;
}SqList;
void InitList(SqList &L,int maxsize=LIST_INIT_SIZE,int incresize=LISTINCRMENT)
{
	L.elem=new A[maxsize];
	L.Length=0;
	L.Listsize=maxsize;
	L.incrementsize=incresize;
}
void OrdInsert(SqList &L,A x)
{
	int i;
	i=L.Length-1;
	while(i>=0&&x<L.elem[i])    //该是从小到大的输入而while(i>=0&&x>L.elem[i])则是从大到小的输入
	{
		L.elem[i+1]=L.elem[i];
		i--;
	}
	L.elem[i+1]=x;
	L.Length++;
}
int main()
{
	int n,i;
	SqList L;
	InitList(L);
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		scanf("%d",&L.elem[i]);
		L.Length++;
	}
	for(i=0;i<n;i++)
	{
		printf("%d ",L.elem[i]);
	}
	printf("\n");
	OrdInsert(L,4);
	for(i=0;i<n+1;i++)
		printf("%d ",L.elem[i]);
	return 0;
}

posted @ 2014-07-27 10:05  NYNU_ACM  阅读(504)  评论(0编辑  收藏  举报