24小时热门版块排行榜    

CyRhmU.jpeg
查看: 199  |  回复: 0

雪山冰锋

金虫 (职业作家)

[求助] 时间紧迫,在线等,明天要啊

#include<stdio.h>
#include<malloc.h>
#define MaxSize 100
typedef char ElemType;
typedef struct
{
  ElemType data[MaxSize];
  int length;
}SqList;

void InitList(SqList *&L);
int ListInsert(SqList *&L,int i,ElemType e);
int ListEmpty(SqList *L);
void DispList(SqList *L);
int ListLength(SqList *L);
int GetElem(SqList *L,int i,ElemType &e);
int LocateElem(SqList *L,ElemType e);
void unionList(SqList *LA,SqList *LB);

void main()
{
SqList *LA,*LB;
printf("初始化顺序表LA\n";
InitList(LA);
printf("依次采用尾插法插入a,b,c,d,e元素\n";
ListInsert(LA,1,'a');
ListInsert(LA,2,'b');
ListInsert(LA,3,'c');
ListInsert(LA,4,'d');
ListInsert(LA,5,'e');
printf(" 输出线性表LA:";
DispList(LA);
printf("初始化顺序表LB\n";
InitList(LB);
printf("依次采用尾插法插入a,v,e,h,k元素\n";
ListInsert(LB,1,'a');
ListInsert(LB,2,'v');
ListInsert(LB,3,'e');
ListInsert(LB,4,'h');
ListInsert(LB,5,'k');
printf(" 输出线性表LB:";
DispList(LB);
unionList(LA,LB);
printf(" 输出集合LA:";
DispList(LA);
}

void InitList(SqList *&L)
{
          L=(SqList *)malloc(sizeof(SqList));
        L->length=0;
}
int ListInsert(SqList *&L,int i,ElemType e)
{
        int j;
        if(i<1||i>L->length+1)
                return 0;
        i--;
        for(j=L->length;j>i;j--)
                L->data[j]=L->data[j-1];
        L->data=e;
        L->length++;
        return 1;
}

int ListEmpty(SqList *L)
{
        return(L->length==0);
}

void DispList(SqList *L)
{
        int i;
        if(ListEmpty(L))return;
        for(i=0;i<L->length;i++)
                printf("%c",L->data);
        printf("\n";
}

int ListLength(SqList *L)
{
        return(L->length);
}

int GetElem(SqList *L,int i,ElemType &e)
{
        if(i<1 || i>L->length)
                return 0;
        e=L->data[i-1];
        return 1;
}

int LocateElem(SqList *L,ElemType e)
{
        int i=0;
        while(i<L->length && L->data!=e)i++;
        if(i>=L->length)
                return 0;
        else
                return i+1;
}

void unionList(SqList *LA,SqList *LB)
{
ElemType e;
LA->length = ListLength(LA);
LB->length = ListLength(LB);
for(int i = 1;i<= LB->length;i++){
  GetElem(LB,i,e);
  if(!LocateElem(LA,e)){
          ListInsert(LA, ++LA->length,e);
  }
}
}

时间紧迫,在线等,明天要啊
_{~$6%B8N)DNQ}]8KK]3Q)Y.jpg
回复此楼
南朝四百八十寺,多少楼台烟雨中
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 雪山冰锋 的主题更新
信息提示
请填处理意见