#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int Status;
typedef int ElemType;
typedef struct {
ElemType *elem;
int length;
int listsize;
}SqList;
//操作结果:构造一个空的线性表L
Status InitList(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 ResizeList(SqList *L,int increment){
ElemType *newbase = (ElemType*)realloc(L->elem,(L->listsize+increment)*sizeof(ElemType));
if(!newbase) exit(OVERFLOW);
L->elem = newbase;
L->listsize += increment;
return OK;
}
//初始条件:线性表L已存在
//操作结果:销毁线性表L
Status DestroyList(SqList *L){
if(0==L->listsize) return ERROR;
free(L->elem);
L->elem = NULL;
L->length = 0;
L->listsize = 0;
return OK;
}
//初始条件:线性表L已存在
//操作结果:将L重置为空表
Status clearList(SqList *L){
if(0==L->listsize) return ERROR;
L->length = 0;
return OK;
}
//初始条件:线性表L已存在
//操作结果:若L为空表,则返回TRUE,否则返回FALSE
Status ListEmpty(SqList L){
return (L.length>0) ? FALSE : TRUE;
}
//初始条件:线性表L已存在
//操作结果:返回L中数据元素个数
Status ListLength(SqList L){
return L.length;
}
//初始条件:线性表L已存在,1<=i<=ListLength(L)
//操作结果:用e返回L中第i个数据元素的值
Status GetElem(SqList L,int i,ElemType *e){
if(i<1 || i>ListLength(L)) return ERROR;
*e = L.elem[i-1];
return OK;
}
//初始条件:线性表L已存在,1<=i<=ListLength(L)
//操作结果:用e改写L中第i个数据元素的值
Status SetElem(SqList L,int i,ElemType e){
if(i<1 || i>ListLength(L)) return ERROR;
L.elem[i-1] = e;
return OK;
}
int my_compare(ElemType a,ElemType b){
return a==b;
}
//初始条件:线性表L已存在,compare()就数据元素判定函数
//操作结果:返回L中第一个给与e满足关系compare()的数据元素的位序。若这样的数据元素不存在,则返回值为0。
Status LocateElem(SqList L,ElemType e,int (*compare)(ElemType,ElemType)){
for(int i=1;i<=L.length;i++){
if(compare(L.elem[i-1],e)) return i;
}
return 0;
}
//初始条件:线性表L已存在
//操作结果:若cur_e是L的数据元素,且不是第一个,则用pre_e返回它的前驱,否则操作失败,pre_e无定义
Status PriorElem(SqList L,ElemType cur_e,ElemType *pre_e){
int i = LocateElem(L,cur_e,my_compare);
if(i>1){
*pre_e = L.elem[i-2];
return TRUE;
}else{
return FALSE;
}
}
//初始条件:线性表L已存在
//操作结果:若cur_e是L的数据元素,且不是最后一个,则用next_e返回它的后继,否则操作失败,next_e无定义
Status NextElem(SqList L,ElemType cur_e,ElemType *next_e){
int i = LocateElem(L,cur_e,my_compare);
if(i>0 && i<L.length){
*next_e = L.elem[i];
return TRUE;
}else{
return FALSE;
}
}
//初始条件:线性表L已存在,1<=i<=ListLength(L)+1
//操作结果:在L中第i个位置之前插入新的数据元素e,L的长度加1
Status ListInsert(SqList *L,int i,ElemType e){
if(i<1 || i>L->length+1) return ERROR;
//若存储空间已满,需开辟新空间
if(L->length*sizeof(ElemType)>=L->listsize) ResizeList(L,LISTINCREMENT);
ElemType *q=&(L->elem[i-1]);
for(ElemType *p=&(L->elem[L->length-1]);p>=q;p--){
*(p+1)=*p;
}
*q=e;
L->length++;
return OK;
}
//初始条件:线性表L已存在
//操作结果:删除L的第i个数据元素,并用e返回其值,L的长度减1
Status ListDelete(SqList *L,int i,ElemType *e){
if(i<1 || i>L->length) return ERROR;
ElemType *p =&(L->elem[i-1]);
*e = *p;
ElemType *q=&(L->elem[L->length-1]);
while(p<q){
*p=*(p+1);
p++;
}
L->length--;
return OK;
}
//初始条件:线性表L已存在
//操作结果:依次对L的每个数据元素调用函数visit()。一旦visit()失败,则操作失败。
Status ListTraverse(SqList L,void visit(ElemType)){
for(int i=0;i<L.length;i++) visit(L.elem[i]);
return OK;
}
void my_print(ElemType e){
printf("%d,",e);
}
void Union(SqList *La,SqList Lb){
int La_len = ListLength(*La);
int Lb_len = ListLength(Lb);
ElemType e;
for(int i=1;i<=Lb_len;i++){
GetElem(Lb,i,&e);
if(!LocateElem(*La,e,my_compare))
ListInsert(La,++La_len,e);
}
}
//将链表按升序进行排序
void sortAscList(SqList L){
ElemType e,f,min;
int min_pos;
int len = L.length;
for(int i=1;i<=len;i++){
GetElem(L,i,&e);
min=e;
for(int j=i+1;j<=len;j++){
GetElem(L,j,&f);
if(min>f){
min=f;
min_pos=j;
}
}
if(min!=e){
SetElem(L,i,min);
SetElem(L,min_pos,e);
}
}
}
//已知线性表La和Lb中的数据元素按值非递减排列
//归并La和Lb得到新的线性表Lc,Lc的数据元素也按值非递减排列
void MergeList(SqList La,SqList Lb,SqList *Lc){
InitList(Lc);
int alen=La.length;
int blen=Lb.length;
int i=1,j=1,k=0;
ElemType ai,bj;
while(i<=alen && j<=blen){
GetElem(La,i,&ai);
GetElem(Lb,j,&bj);
if(ai<bj){
ListInsert(Lc,++k,ai);
i++;
}else{
ListInsert(Lc,++k,bj);
j++;
}
}
while(i<=alen){
GetElem(La,i,&ai);
ListInsert(Lc,++k,ai);
i++;
}
while(j<=blen){
GetElem(Lb,j,&bj);
ListInsert(Lc,++k,bj);
j++;
}
}
int main()
{
SqList L;
ElemType e;
printf("构造空List\n");
InitList(&L);
if(ListEmpty(L)) printf("List为空\n");
else printf("List不为空\n");
printf("向List填充数据元素\n");
for(int i=1;i<11;i++){
ListInsert(&L,L.length+1,i*10);
//printf("%d,",i);
}
if(ListEmpty(L)) printf("List为空\n");
else printf("List不为空\n");
printf("打印List:");
ListTraverse(L,my_print);
printf("共%d个\n",ListLength(L));
GetElem(L,6,&e);
printf("第%d个元素为%d\n",6,e);
ListDelete(&L,3,&e);
printf("删除第%d个元素为%d\n",3,e);
printf("打印List:");
ListTraverse(L,my_print);
printf("共%d个\n",ListLength(L));
ListInsert(&L,3,33);
printf("在第%d位插入元素为%d\n",3,33);
printf("打印List:");
ListTraverse(L,my_print);
printf("共%d个\n",ListLength(L));
e=60;
int m=LocateElem(L,e,my_compare);
if(m>0) printf("查找到值为%d的数据,在第%d个\n",e,m);
else printf("查找不到值为%d的数据\n",e);
ElemType next_e;
GetElem(L,1,&e);
printf("顺序打印List:%d,",e);
while(NextElem(L,e,&next_e)){
printf("%d,",next_e);
e=next_e;
}
printf("\n");
ElemType pre_e;
GetElem(L,ListLength(L),&e);
printf("逆序打印List:%d,",e);
while(PriorElem(L,e,&pre_e)){
printf("%d,",pre_e);
e=pre_e;
}
printf("\n");
clearList(&L);
printf("清空List\n");
if(ListEmpty(L)) printf("List为空\n");
else printf("List不为空\n");
printf("销毁List\n");
DestroyList(&L);
SqList La;
InitList(&La);
for(int i=1;i<11;i++){
ListInsert(&La,La.length+1,i*10);
}
printf("打印La:");
ListTraverse(La,my_print);
printf("共%d个\n",ListLength(La));
SqList Lb;
InitList(&Lb);
for(int i=1;i<11;i++){
ListInsert(&Lb,Lb.length+1,i);
}
printf("打印Lb:");
ListTraverse(Lb,my_print);
printf("共%d个\n",ListLength(Lb));
printf("合并La、Lb\n");
Union(&La,Lb);
printf("打印La:");
ListTraverse(La,my_print);
printf("共%d个\n",ListLength(La));
srand(time(NULL));
SqList Le;
InitList(&Le);
for(int i=1;i<11;i++){
ListInsert(&Le,Le.length+1,rand()%11);
}
printf("打印Le:");
ListTraverse(Le,my_print);
printf("共%d个\n",ListLength(Le));
SqList Lf;
InitList(&Lf);
for(int i=1;i<11;i++){
ListInsert(&Lf,Lf.length+1,rand()%11*10);
}
printf("打印Lf:");
ListTraverse(Lf,my_print);
printf("共%d个\n",ListLength(Lf));
printf("排序Le\n");
sortAscList(Le);
printf("打印Le:");
ListTraverse(Le,my_print);
printf("共%d个\n",ListLength(Le));
printf("排序Lf\n");
sortAscList(Lf);
printf("打印Lf:");
ListTraverse(Lf,my_print);
printf("共%d个\n",ListLength(Lf));
SqList Lg;
printf("归并Le,Lf到Lg:\n");
MergeList(Le,Lf,&Lg);
printf("打印Lg:");
ListTraverse(Lg,my_print);
printf("共%d个\n",ListLength(Lg));
return 0;
}
线性表的顺序表示和实现:sqlist完整代码
猜你喜欢
转载自blog.csdn.net/lzdelphi/article/details/104336236
今日推荐
周排行