您的位置:首页 > 理论基础 > 数据结构算法

数据结构实现顺序线性表的一些小应用

2009-12-05 21:22 831 查看
头文件 “2_1.h”

# include"cf1.h"

void union1(SqList &La,SqList Lb){   //操作一
	//将所有在线性表Lb中但不在La中数据元素插入到La中
	int La_len=ListLength(La),Lb_len=ListLength(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 MergeList(SqList La,SqList Lb,SqList &Lc){  //操作二
	//已知线性表La和Lb中的数据元素按值非递减排列
	//归并La和Lb得到新的线性表Lc,Lc的数据元素也按值非递减排列
	InitList(Lc);
	int i=1,j=1,k=0,ai,bj,La_len=ListLength(La),Lb_len=ListLength(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);
	}
}




头文件 “cf1.h”

# include<iostream.h>
# include<malloc.h>
# include<stdlib.h>

# define LIST_INIT_SIZE 100  //线性表存储空间的初始分配量
# define LISTINCREAMENT  100  //线性表存储空间的分配增量

typedef struct{
	int *elem;  //存储空间基址,是数组的头指针
	int length;  //当前长度
	int listsize;  //当前分配的存储容量(以sizeof(int))为单位
}SqList;  

void InitList(SqList &L){
	//构造一个空的线性表L
	L.elem=(int*)malloc(LIST_INIT_SIZE*sizeof(int));
	if(!L.elem) exit(0);  //存储分配失败
	L.length=0;  //空表的长度为0
	L.listsize=LIST_INIT_SIZE;  //初始存储容量
}

void DestroyList(SqList &L){
	//初始条件:线性表L已存在
	//操作结果:销毁线性表
	free(L.elem);
	L.length=0;
	L.listsize=0;
	L.elem=NULL;
}

void ClearList(SqList &L){  //操作三
	//初始条件:线性表L已存在
	//操作结果:将L重置为空表
	free(L.elem);
	L.elem=(int*)malloc(LIST_INIT_SIZE*sizeof(int));
	if(!L.elem) exit(0);  //存储空间分配失败
	L.length=0;   //空表长度为0
	L.listsize=LIST_INIT_SIZE; //当前存储空间分配的容量
}

int ListEmpty(SqList L){  //操作二
	//初始条件:线性表L存在
	//操作结果:若L为空表,则返回1,否则返回0
	if(L.length)
		return 0;
	return 1;
}

int ListLength(SqList L){
	//初始条件:线性表L存在
	//操作结果:返回L中数据元素的个数
	return L.length;
}

void ListInsert(SqList &L,int i,int e){  //操作一
	//初始条件:线性表L已存在,1<=i<=ListLength(L)+1
	//操作结果:在L中第i个位置之前插入新的数据元素e,L的长度加1
	if(i<1||i>L.length+1)
		cout<<"i的值不合法"<<endl;
	else{
	    	if(L.length>=L.listsize){ //当前存储空间已满,增加分配容量
			int *newbase=(int*)realloc(L.elem,(L.listsize+LISTINCREAMENT)*sizeof(int));
			if(!newbase) exit(0);  //存储空间分配失败
			L.elem=newbase; //新基址
			L.listsize+=LISTINCREAMENT;  //增加存储容量
			}
		int *q=&(L.elem[i-1]);   //q为插入位置
		for(int *p=&(L.elem[L.length-1]);p>=q;p--)
			*(p+1)=*p;    //插入位置及之后的元素右移
		*q=e;//插入e
		++L.length;  //表长增1
	}
}

void ListDelete(SqList &L,int i,int &e){  //操作八
	//初始条件:线性表L已存在且非空,1<=i<=ListLength(L)
	//操作结果:删除L的第i个数据元素,并用e返回其值,L的长度减1
	if(i<1||i>L.length)
		cout<<"i值不合法"<<endl;
	else{
		int *p=&(L.elem[i-1]);  //p为被删除元素的位置
		e=*p;  //被删除元素的值赋给e
		int *q=L.elem+L.length-1;  //表尾元素的位置
		for(++p;p<=q;++p)  *(p-1)=*p;  //被删除元素之后的元素左移
		--L.length; //表长减1
	}
}

void GetElem(SqList L,int i,int &e){  //操作四
	//初始条件:线性表L已存在,1<=i<=ListLength(L)
	//操作结果:用e返回L中第i个数据元素的值
	if(i<1||i>ListLength(L))
		cout<<"有误"<<endl;
	else
		e=L.elem[i-1];
}

int compare(int e1,int e2){
	//比较,如果相等,返回1,否则返回0
	if(e1==e2)
		return 1;
	return 0;
}

int LocateElem(SqList L,int e,int(*compare)(int,int)){ //操作五
	//初始条件:线性表L已存在;1<=i<=ListLength(L);
	//操作结果:返回L中第一个与e满足关系compare()的数据元素的位序,若这样的数据元素不存在,则返回0
	int i=1;int *p=L.elem;
	while(i<=L.length&&!compare(*p++,e)) ++i;
	if(i<=L.length) return i;
	return 0;
}

void PriorElem(SqList L,int cur_e,int &pre_e){  //操作六
	//初始条件:线性表L已存在
	//操作结果:若cur_e是L的数据元素,且不是第一个,则用pre_e返回它的前驱;否则操作失败,pre_e无定义
	int i=LocateElem(L,cur_e,compare);
	if(i==0)
		cout<<"不存在该数据元素"<<endl;
	else if(i==1)
		cout<<"是第一个元素,没有前驱结点"<<endl;
	else{
		pre_e=L.elem[i-2];
	}
}

void NextElem(SqList L,int cur_e,int &next_e){  //操作七
	//初始条件:线性表L已存在
	//操作结果:若cur_e是L的数据元素,且不是最后一个,则用next_e返回它的后继,否则操作失败,next_e无定义
	int i=LocateElem(L,cur_e,compare);
	if(i==0)
		cout<<"不存在该元素"<<endl;
	else if(i==ListLength(L))
		cout<<"是最后一个元素,无后继"<<endl;
	else
		next_e=L.elem[i];
}

void visit(int e){
	cout<<e<<" ";
}

void ListTraverse(SqList L,void(*visit)(int)){
	//初始条件:线性表L已存在
	//操作结果:依次对L的每个元素调用函数visit().一旦visit()失败,则操作失败
	for(int i=0;i<ListLength(L);i++)
		visit(L.elem[i]);
}


主文件 “test.cpp”

# include"2_1.h"

void main(){
	cout<<"#######测试操作一########"<<endl;
	SqList L1,L2;
	InitList(L1);InitList(L2);
	ListInsert(L1,1,1);ListInsert(L1,2,3);ListInsert(L1,3,5);
	ListInsert(L2,1,3);ListInsert(L2,2,4);ListInsert(L2,3,5);
	ListTraverse(L1,visit);
	cout<<endl;
	ListTraverse(L2,visit);
	union1(L1,L2);
	cout<<endl;
	ListTraverse(L1,visit);	
	DestroyList(L1);DestroyList(L2);
	cout<<endl<<endl;
	cout<<"#######测试操作二########"<<endl;
	SqList La,Lb,Lc;
	InitList(La);InitList(Lb);
	ListInsert(La,1,1);ListInsert(La,2,3);ListInsert(La,3,5);
	ListInsert(Lb,1,3);ListInsert(Lb,2,4);ListInsert(Lb,3,5);
	ListTraverse(La,visit);
	cout<<endl;
	ListTraverse(Lb,visit);
	cout<<endl;
	MergeList(La,Lb,Lc);
	ListTraverse(Lc,visit);
	cout<<endl;
}




输出结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: