如何给LList类添加一个成员函数,倒置线性表中元素的顺序
答案:1 悬赏:50 手机版
解决时间 2021-04-21 05:21
- 提问者网友:世勋超人
- 2021-04-20 04:30
如何给LList类添加一个成员函数,倒置线性表中元素的顺序
最佳答案
- 五星知识达人网友:平生事
- 2021-04-20 05:23
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 2
typedef struct
{
ElemType *elem;
int length;
int listsize;
}SqList;
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 DestroyList(SqList *L)
{
free((*L).elem);
(*L).elem=NULL;
(*L).length=0;
(*L).listsize=0;
return OK;
}
Status ClearList(SqList *L)
{
(*L).length=0;
return OK;
}
Status ListEmpty(SqList L)
{
if(L.length==0)
return TRUE;
else
return FALSE;
}
int ListLength(SqList L)
{
return L.length;
}
Status GetElem(SqList L,int i,ElemType *e)
{
if(i<1||i>L.length)
exit(ERROR);
*e=*(L.elem+i-1);
return OK;
}
int LocateElem(SqList L,ElemType e,Status(*compare)(ElemType,ElemType))
{
ElemType *p;
int i=1;
p=L.elem;
while(i<=L.length && !compare(*p++,e))
++i;
if(i<=L.length)
return i;
else
return 0;
}
Status PriorElem(SqList L,ElemType cur_e,ElemType *pre_e)
{
int i=2;
ElemType *p=L.elem+1;
while(i<=L.length && *p!=cur_e)
{
p++;
i++;
}
if(i>L.length)
return INFEASIBLE;
else
{
*pre_e=*--p;
return OK;
}
}
Status NextElem(SqList L,ElemType cur_e,ElemType *next_e)
{
int i=1;
ElemType *p=L.elem;
while(i
i++;
p++;
}
if(i==L.length)
return INFEASIBLE;
else
{
*next_e=*++p;
return OK;
}
}
Status ListInsert(SqList *L,int i,ElemType e)
{
ElemType *newbase,*q,*p;
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 ListDelete(SqList *L,int i,ElemType *e)
{
ElemType *p,*q;
if(i<1||i>(*L).length)
return ERROR;
p=(*L).elem+i-1;
*e=*p;
q=(*L).elem+(*L).length-1;
for(++p;p<=q;++p)
*(p-1)=*p;
(*L).length--;
return OK;
}
Status ListTraverse(SqList L,void(*vi)(ElemType*))
{
ElemType *p;
int i;
p=L.elem;
for(i=1;i<=L.length;i++)
vi(p++);
printf("\n");
return OK;
}
#include"c1.h"
typedef int ElemType;
#include"c2-1.h"
#include"bo2-1.c"
Status equal(ElemType c1,ElemType c2)
{
if(c1==c2)
return TRUE;
else
return FALSE;
}
void Union(SqList *La,SqList Lb)
{
ElemType e;
int La_len,Lb_len;
int i;
La_len=ListLength(*La);
Lb_len=ListLength(Lb);
for(i=1;i<=Lb_len;i++)
{
GetElem(Lb,i,&e);
if(!LocateElem(*La,e,equal))
ListInsert(La,++La_len,e);
}
}
void print(ElemType *c)
{
printf("%d ",*c);
}
void main()
{
SqList La,Lb;
Status i;
int j;
i=InitList(&La);
if(i==1)
for(j=1;j<=5;j++)
i=ListInsert(&La,j,j);
printf("La= ");
ListTraverse(La,print);
InitList(&Lb);
for(j=1;j<=5;j++)
i=ListInsert(&Lb,j,2*j);
printf("Lb= ");
ListTraverse(Lb,print);
Union(&La,Lb);
printf("new La= ");
ListTraverse(La,print);
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯