设顺序表va中的数据元素递增有序。试写一算法,将x插入到顺序表的适当位置上,以保持该表的有序性。
算法:
Status Insert_Sqlist(SqList &va,int x)
{
int i;
if(va.length+1>va.listsize) return ERROR;
va.length++;
for(i=va.length-1; va.elem[i]>x&&i>=0; i--)
va.elem[i+1]=va.elem[i];
va.elem[i+1]=x;
return OK;
{
int i;
if(va.length+1>va.listsize) return ERROR;
va.length++;
for(i=va.length-1; va.elem[i]>x&&i>=0; i--)
va.elem[i+1]=va.elem[i];
va.elem[i+1]=x;
return OK;
}
C code:
#include<stdio.h>
#include<stdlib.h>
#define LIST_INIT_SIZE 10
#define LIST_INCREMENT 2
#define ERROR 0
#define OK 1
#define OVERFLOW -1
#define TRUE 1
typedef int Status;
struct SqList
{
int *elem;
int length;
int listsize;
};
void InitList(SqList &L)
{
L.elem=(int*)malloc(LIST_INIT_SIZE*sizeof(int));
if(!L.elem)
exit(OVERFLOW);
L.length=0;
L.listsize=LIST_INIT_SIZE;
}
void DestroyList(SqList &L)
{
free(L.elem);
L.elem=NULL;
L.length=0;
L.listsize=0;
}
Status ListInsert(SqList &L,int i,int e)
{
int *newbase,*q,*p;
if(i<1||i>L.length+1)
return ERROR;
if(L.length==L.listsize)
{
newbase=(int*)realloc(L.elem,(L.listsize+LIST_INCREMENT)*sizeof(int));
if(!newbase)
exit(OVERFLOW);
L.elem=newbase;
L.listsize+=LIST_INCREMENT;
}
q=L.elem+i-1;
for(p=L.elem+L.length-1;p>=q;--p)
*(p+1)=*p;
*q=e;
++L.length;
return OK;
}
int cmp(const void*a,const void*b)
{
return *(int*)a - *(int*)b;
}
void ListSort(SqList &L)
{
qsort(L.elem,L.length,sizeof(int),cmp);
}
Status Insert_Sqlist(SqList &va,int x)
{
int i;
if(va.length+1>va.listsize) return ERROR;
va.length++;
for(i=va.length-1; va.elem[i]>x&&i>=0; i--)
va.elem[i+1]=va.elem[i];
va.elem[i+1]=x;
return OK;
}
void ListTraverse(SqList L,void(*vist)(int&))
{
int *p=L.elem;
int i;
for(i=1; i<=L.length; i++)
vist(*p++);
printf("\n");
}
void print1(int &c)
{
printf("%d ",c);
}
int main()
{
SqList La;
int i;
int t=rand()%100; //随机生成待插入数
printf("%d\n",t);
InitList(La);
for(i=1; i<=5;i++)
ListInsert(La,i,rand()%100); //随机生成5个数
ListTraverse(La,print1);
ListSort(La);
ListTraverse(La,print1);
Insert_Sqlist(La,t);
ListTraverse(La,print1);
return 0;
#include<stdlib.h>
#define LIST_INIT_SIZE 10
#define LIST_INCREMENT 2
#define ERROR 0
#define OK 1
#define OVERFLOW -1
#define TRUE 1
typedef int Status;
struct SqList
{
int *elem;
int length;
int listsize;
};
void InitList(SqList &L)
{
L.elem=(int*)malloc(LIST_INIT_SIZE*sizeof(int));
if(!L.elem)
exit(OVERFLOW);
L.length=0;
L.listsize=LIST_INIT_SIZE;
}
void DestroyList(SqList &L)
{
free(L.elem);
L.elem=NULL;
L.length=0;
L.listsize=0;
}
Status ListInsert(SqList &L,int i,int e)
{
int *newbase,*q,*p;
if(i<1||i>L.length+1)
return ERROR;
if(L.length==L.listsize)
{
newbase=(int*)realloc(L.elem,(L.listsize+LIST_INCREMENT)*sizeof(int));
if(!newbase)
exit(OVERFLOW);
L.elem=newbase;
L.listsize+=LIST_INCREMENT;
}
q=L.elem+i-1;
for(p=L.elem+L.length-1;p>=q;--p)
*(p+1)=*p;
*q=e;
++L.length;
return OK;
}
int cmp(const void*a,const void*b)
{
return *(int*)a - *(int*)b;
}
void ListSort(SqList &L)
{
qsort(L.elem,L.length,sizeof(int),cmp);
//将线性表按增序排列
}
Status Insert_Sqlist(SqList &va,int x)
{
int i;
if(va.length+1>va.listsize) return ERROR;
va.length++;
for(i=va.length-1; va.elem[i]>x&&i>=0; i--)
va.elem[i+1]=va.elem[i];
va.elem[i+1]=x;
return OK;
}
void ListTraverse(SqList L,void(*vist)(int&))
{
int *p=L.elem;
int i;
for(i=1; i<=L.length; i++)
vist(*p++);
printf("\n");
}
void print1(int &c)
{
printf("%d ",c);
}
int main()
{
SqList La;
int i;
int t=rand()%100; //随机生成待插入数
printf("%d\n",t);
InitList(La);
for(i=1; i<=5;i++)
ListInsert(La,i,rand()%100); //随机生成5个数
ListTraverse(La,print1);
ListSort(La);
ListTraverse(La,print1);
Insert_Sqlist(La,t);
ListTraverse(La,print1);
return 0;
}