数据结构(严蔚敏)算法2.1-2.7实现

#include 
#include 
using namespace std;
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int ElemType;
typedef int Status;
typedef struct 
{
	ElemType *elem;
	ElemType lenth;
	ElemType listsize;
}List;
//工具函数
Status DestroyList(List& L);
Status ClearList(List& L);
Status ListEmpty(List L);
int ListLenth(List L);
void GetElem(List L,int i,int&e);
Status compare(ElemType e,ElemType a);
void ShowList(List L);
Status InputList(List& L,int lenth);
//算法2.1-2.7
void unions(List &La,List Lb);
void MergeList1(List La,List Lb,List &Lc);
Status InitList(List& L);
Status ListInsert(List &L,int i,ElemType e);
Status ListDelete(List& L,int i,ElemType &e);
int LocateElem(List& L,ElemType e,Status(*compare)(ElemType,ElemType));
void MergeList2(List La,List Lb,List &Lc);
int main()
{
	List a,b;

	cout<<'\n'<<"开始测试"<<"##算法2.3##"<<endl;
	if(InitList(a))cout<<"表a初始化成功"<<endl;
	if(InitList(b))cout<<"表b初始化成功"<<endl;
	cout<<"测试完毕"<<"##算法2.3##"<<endl;

	if(InputList(a,4))cout<<"数据导入成功!";//3,5,8,11
	if(InputList(b,7))cout<<"数据导入成功!";//2,6,8,9,11,15,20
	cout<<endl;
	cout<<"表格a存储的数据为:"<<endl;ShowList(a);
	cout<<"表格b存储的数据为:"<<endl;ShowList(b);

	cout<<'\n'<<"开始测试"<<"##算法2.6##"<<endl;
	if(LocateElem(a,8,compare))cout<<"元素‘8’位序排在第"<<LocateElem(a,8,compare)<<"位"<<endl;
	cout<<"测试完毕"<<"##算法2.6##"<<endl;

	cout<<'\n'<<"开始测试"<<"##算法2.7##"<<endl;
	List c;InitList(c);
	MergeList2(a,b,c);
	ShowList(c);
	cout<<"测试完毕"<<"##算法2.7##"<<endl;

	cout<<'\n'<<"开始测试"<<"##算法2.2##"<<endl;
	List d;InitList(d);
	MergeList1(a,b,d);
	ShowList(d);
	cout<<"测试完毕"<<"##算法2.2##"<<endl;

	cout<<'\n'<<"开始测试"<<"##算法2.4##"<<endl;
	cout<<"开始表格c存储的数据为:"<<endl;ShowList(c);
	ListInsert(c,1,1);
	ListInsert(c,4,4);
	cout<<"结束表格c存储的数据为:"<<endl;ShowList(c);
	cout<<"测试完毕"<<"##算法2.4##"<<endl;

	cout<<'\n'<<"开始测试"<<"##算法2.5##"<<endl;
	cout<<"开始表格c存储的数据为:"<<endl;ShowList(c);
	int i=0,j=0;
	ListDelete(c,1,i);
	ListDelete(c,4,j);
	cout<<"结束表格c存储的数据为:"<<endl;ShowList(c);
	cout<<"删除的数据为"<<i<<"和"<<j<<endl;
	cout<<"测试完毕"<<"##算法2.5##"<<endl;

	cout<<'\n'<<"开始测试"<<"##算法2.1##"<<endl;
	cout<<"开始表格a存储的数据为:"<<endl;ShowList(a);
	unions(a,b);	
	cout<<"结束表格a存储的数据为:"<<endl;ShowList(a);
	cout<<"测试完毕"<<"##算法2.1##"<<endl;
	return 0;
}
//工具函数
Status DestroyList(List& L)
{
	free(L.elem);
	if(!L.elem)return 1;
}
Status ClearList(List& L)
{
	L.lenth=0;
	return 1;
}
Status ListEmpty(List L)
{
	if(L.lenth==0)return true;
	return false;
}
int ListLenth(List L)
{
	return L.lenth;
}
void GetElem(List L,int i,int&e)
{
	if(i>=1&&i<=ListLenth(L))
		e=L.elem[i-1];
}
Status compare(ElemType e,ElemType a)
{
	if(e==a)return true;
	else return false;
}
void ShowList(List L)
{
	for(int i=0;i<L.lenth;i++)
	{
		cout<<L.elem[i]<<"  ";
	}
	cout<<endl;
}
void SortList(List& L)
{
	int i=0,j;
	for(i;i<L.lenth-1;i++)
	{
		for(j=i+1;j<L.lenth;j++)
		{
			if(L.elem[i]<L.elem[j])
			{
				int temp=L.elem[i];
				L.elem[i]=L.elem[j];
				L.elem[j]=temp;
			}
		}
	}
}
Status InputList(List& L,int lenth)
{
	cout<<"输入元素:"<<endl;
	if(lenth<=L.listsize)
	{
		for(int i=0;i<lenth;i++)
		{
			cin>>L.elem[i];
			L.lenth++;
		}
		return 1;
	}
	else return 0;
}
//算法2.1-2.7
void unions(List &La,List Lb)//算法2.1
{
	//将所有在线性表Lb中但不在La中的数据元素插入到La中
	int La_len=ListLenth(La),Lb_len=ListLenth(Lb),e;//求线性表的长度
	for(int i=1;i<=Lb_len;i++)
	{
		GetElem(Lb,i,e);//取Lb中第i个数据元素赋给e
		if(!(LocateElem(La,e,compare)))ListInsert(La,++La_len,e);//La中不存在和e相同的数据元素,则插入之。
	}
}
void MergeList1(List La,List Lb,List &Lc)//算法2.2
{
	//已知线性表La和Lb的元素按值非递减排列
	//归并La和Lb得到新的顺序表Lc,Lc的元素也按值非递减排列
	InitList(Lc);
	int i=1,j=1,k=0,ai=0,bj=0;
	int La_len=ListLenth(La);int Lb_len=ListLenth(Lb);
	while((i<=La_len)&&(j<=Lb_len))//La和Lb均非空
	{
		GetElem(La,i,ai);GetElem(Lb,j,bj);
		if(ai<=bj)
		{
			ListInsert(Lc,++k,ai);++i;
		}
		else
		{
			ListInsert(Lc,++k,bj);++j;
		}
	}
	while(i<=La_len)
	{
		GetElem(La,i++,ai);ListInsert(Lc,++k,ai);
	}
	while(j<=Lb_len)
	{
		GetElem(Lb,j++,bj);ListInsert(Lc,++k,bj);
	}
}
Status InitList(List& L)//算法2.3
{
	//构造一个空的线性表L。
	L.elem=(ElemType*)malloc(sizeof(ElemType)*LIST_INIT_SIZE);
	if(!L.elem)exit(OVERFLOW);//存储分配失败
	L.lenth=0;//空表长度为0
	L.listsize=LIST_INIT_SIZE;//初始存储容量
	return 1;
}
Status ListInsert(List &L,int i,ElemType e)//算法2.4
{
	//在顺序线性表L中第i个元素之前插入新的元素e,i的合法值为1<=i<=Listlenth(L)+1
	if(i<1||i>L.lenth+1)return 0;//i值不合法
	int*q=NULL;
	if(L.lenth>=L.listsize)//当前存储空间已满,增加分配
	{
		ElemType* 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]);//q为插入位置
		for(ElemType* p=&(L.elem[L.lenth-1]);p>=q;--p)//插入位置及之后的元素右移
			*(p+1)=*p;
	*q=e;//插入e
	++L.lenth;
	return 1;
}
Status ListDelete(List& L,int i,ElemType &e)//算法2.5
{
	//在顺序线性表L中删除第i个元素,并用e返回其值
	//i的合法值为1<=i<=Listlenth(L)
	if((i<1)||(i>L.lenth))return 0;//i值不合法
	ElemType* p=&(L.elem[i-1]);//p为被删除元素的位置
	e=*p;//被删除元素的值赋给e
	ElemType*q=L.elem+L.lenth-1;//表尾元素的位置
	for(++p;p<=q;++p)//被删除元素之后的元素左移
		*(p-1)=*p;
	--L.lenth;//表长减一
	return 1;

}
int LocateElem(List& L,ElemType e,Status(*compare)(ElemType,ElemType))//算法2.6
{
	//在顺序表线性表L中查找第一个值与e满足compare()的元素的位序
	//若找到,则返回其在L中的位序,否则返回0
	int i=1;//i的初值为第一个元素的位序
	int *p=L.elem;//p的初值为第一个元素的存储位置
	while(i<=L.listsize&&!((*compare)(*p++,e)))++i;
	if(i<=L.listsize) return i;
	else return 0;
}
void MergeList2(List La,List Lb,List &Lc)//算法2.7
{
	//已知顺序线性表La和Lb的元素按值非递减排列
	//归并La和Lb得到新的顺序表Lc,Lc的元素也按值非递减排列
	ElemType* pa=La.elem,*pb=Lb.elem;
	Lc.listsize=Lc.lenth=La.lenth+Lb.lenth;
	ElemType* pc=Lc.elem=(ElemType*)malloc(Lc.listsize*sizeof(ElemType));
	if(!Lc.elem)exit(OVERFLOW);//存储分配失败
	int*pa_last=La.elem+La.lenth-1;
	int*pb_last=Lb.elem+Lb.lenth-1;
	while(pa<=pa_last&&pb<=pb_last)//归并
	{
		if(*pa<=*pb) *pc++=*pa++;
		else*pc++=*pb++;
	}
	while(pa<=pa_last)*pc++=*pa++;//插入La的剩余元素
	while(pb<=pb_last)*pc++=*pb++;//插入Lb的剩余元素
}

结果:

数据结构(严蔚敏)算法2.1-2.7实现_第1张图片

你可能感兴趣的:(数据结构(严蔚敏)算法2.1-2.7实现)