文档库 最新最全的文档下载
当前位置:文档库 › 编写函数,创建一个顺序表,在顺序表的指定位置插入,删除一个元素,将两个有序顺序表合并成一个新的有序

编写函数,创建一个顺序表,在顺序表的指定位置插入,删除一个元素,将两个有序顺序表合并成一个新的有序

# include "stdio.h"
# include "malloc.h"
# include "math.h"
# include "stdlib.h"
# define TRUE 1
# define FALSE 0
# define OK 1
# define ERROR 0
# define INFEASIBLE -1
# define NULL 0
typedef int Status;
typedef int ElemType;
# define LIST_INIT_SIZE 100
# define LISTINCREMENT 10
typedef struct
{
ElemType * elem;
int length;
int listsize;
} Sqlist;
Status Initlist_Sq(Sqlist &L)
{
L.elem=(ElemType *) malloc
(LIST_INIT_SIZE*sizeof(ElemType));
if (! L.elem) exit (OVERFLOW);
L.length=0;
L.listsize=LIST_INIT_SIZE;
return OK;
}
Status Insert_Sq(Sqlist &L,int i,ElemType e)
{
ElemType * newbase,*p,*q;
if(i<1||i>L.length+1) return ERROR;
if (L.length>=L.listsize)
{
newbase=(ElemType *) realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
if (!newbase) exit (OVERFLOW);
L.elem=newbase;
L.listsize+=LISTINCREMENT;
}
q=&(L.elem[i-1]);
for (p=&(L.elem[L.length-1]);p>=q;--p) *(p+1)=*p;
*q=e;
++L.length;
return OK;
}
Status Delete_Sq(Sqlist & L,int i ,ElemType & e)
{
ElemType *p,*q;
if ( (i>L.length)||(i<1) ) return ERROR;
p=&(L.elem[i-1]);
e=*p;
q=&(L.elem[L.length-1]);
++p;
for (p;p<=q;++p)
*(p-1)=*p;
--L.length;
return OK;
}

Status Print_Sq(Sqlist L)
{
ElemType *q;
int i;
q=L.elem;
printf("this is ElemType elemment\n");
for (i=1;i<=L.length;i++)
{
printf("%5d",*q);
q=q++;
}
printf("\n");
return OK;
}
Status Creat_Sq(Sqlist &L)
{ ElemType temp ;
printf("Please Input Date(9999) ending\n");
scanf("%d",&temp);
while (temp!=9999)
{Insert_Sq(L,L.length+1,temp);
printf("Please Input Date(9999) ending\n");
scanf("%d",&temp);
}
return OK;
}

int LocateElem_Sq(Sqlist L, ElemType e)
{
int i;ElemType *p;
i=1;p=L.elem;
while(i<=L.length&&!(*p++==e)) ++i;
if(i<=L.length) return i;
else return 0;
}//locateElem_sq
Status Sort_Sq(Sqlist &L)
{
int i,j; ElemType temp;
for (i=0;i{
for (j=i+1;j{
if (L.elem[i]>L.elem[j])
{
temp=L.elem[i];
L.elem[i]=L.elem[j];
L.elem[j]=temp;
}
}
}
return OK;
}void main()
{
ElemType e;
Sqlist sq1;
Initlist_Sq(sq1);
Creat_Sq(sq1);
Print_Sq(sq1);
e=100;
Insert_Sq(sq1,3,e);
Print_Sq(sq1);
printf("please locate number:");
scanf ("%d",&e);
printf("Element location:%d\n" , LocateElem_Sq(sq1,e));
Delete_Sq(sq1,4,e);
Print_Sq(sq1);
Sort_Sq(sq1);
Print_Sq(sq1);
}

相关文档
相关文档 最新文档