标签:Lb 线性表 La int elem length SqList return
#include<iostream>
#include<stdio.h>
#include <malloc.h>
using namespace std;
#define MaxSize 50
typedef char ElemType;
typedef struct
{
ElemType elem[MaxSize]; //存放顺序表中的元素
int length; //存放顺序表的长度
} SqList;
void InitList(SqList *&L) //初始化线性表,构造一个空的线性表,并将长度设置为0
{
L=(SqList *)malloc(sizeof(SqList));
L->length=0;
}
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->elem[i-1];
return 1;
}
int LocateElem(SqList *L, ElemType e)//按元素查找,找到与该元素值相同的元素并返回其序号
{
int i=0;
while (i<L->length && L->elem[i]!=e) i++;
if (i>=L->length)
return 0;
else
return i+1;
}
int ListInsert(SqList *&L,int i,ElemType e)//插入数据元素,在第i个位置上插入新元素,使后面的元素依次后移,并是长度加1;
{
int j;
if (i<1 || i>L->length+1)
return 0;
i--; /*将顺序表位序转化为elem下标*/
for (j=L->length;j>i;j--) /*将elem[i]及后面元素后移一个位置*/
L->elem[j]=L->elem[j-1];
L->elem[i]=e;
L->length++; /*顺序表长度增1*/
return 1;
}
int ListEmpty(SqList *L) //判断线性表是否为空 即看其长度是否为0
{
return(L->length==0);
}
void DispList(SqList *L) //输出线性表
{
int i;
if (ListEmpty(L)) return;
for (i=0;i<L->length;i++)
printf("%c",L->elem[i]);
printf("\n");
}
void Union(SqList *La,SqList *Lb){
ElemType e;
int La_len;
int Lb_len;
La_len=ListLength(La);
Lb_len=ListLength(Lb);
for(int i=1;i<=Lb_len;i++){
GetElem(Lb,i,e);
if(!LocateElem(La,e)) ListInsert(La,++La_len,e);
}
}
void main()
{
SqList *La;
SqList *Lb;
ElemType e;
int La_len;
int Lb_len;
printf("(1)初始化顺序表L\n");
InitList(La);
InitList(Lb);
printf("(2)依次采用尾插法插入元素\n");
ListInsert(La,1,'a');
ListInsert(La,2,'b');
ListInsert(La,3,'c');
ListInsert(La,4,'d');
ListInsert(La,5,'e');
printf("(2)依次采用尾插法插入元素\n");
ListInsert(Lb,1,'a');
ListInsert(Lb,2,'b');
ListInsert(Lb,3,'c');
ListInsert(Lb,4,'d');
ListInsert(Lb,5,'5');
printf("(3)输出表L:");
DispList(La);
DispList(Lb);
Union(La,Lb);
/*La_len=ListLength(La);
Lb_len=ListLength(Lb);
for(int i=1;i<=Lb_len;i++){
GetElem(Lb,i,e);
if(!LocateElem(La,e)) ListInsert(La,++La_len,e);
}*/
printf("合并后:");
DispList(La);
}
代码参考:https://blog.csdn.net/ciganxian/article/details/27549653
标签:Lb,线性表,La,int,elem,length,SqList,return 来源: https://blog.csdn.net/weixin_43424154/article/details/88319873
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。