您的位置:首页 > 其它

(顺序有序表)插入元素

2014-07-27 10:05 246 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐