线性表的顺序存储结构

以顺序表表示集合,编制一个能演示执行集合的并、交、差、补运算的程序

以顺序表表示集合,编制一个能演示执行集合的并、交、差、补运算的程序。

(1) 集合的元素限定为小写字母字符(‘a’…’z’),集合输入的形式为一个以“回车符”为结束标志的字符串,串中字符顺序不限,且允许出现重复字符或非法字符,程序应能自动滤去。输出的运算结果字符串中将不含重复字符和非法字符。

(2) 演示程序以用户和计算机的对话方式执行,即在计算机终端上显示“提示信息”后,由用户在键盘上输入演示程序中规定的运算命令,相应的输入数据(滤去输入中的非法字符)和运算结果显示在其后。

(3) 程序执行的命令应包括:① 构造集合1;② 构造集合2;③ 求并集;④求补集;⑤退出

代码如下:

#include<iostream>
#include<stdlib.h>
using namespace std;
#define ARRAY_SIZE 100//数组容量 
#define LIST_INIT_SIZE 100//线性表空间的初始分配量
#define LISTSIZE 10//线性表空间的增量 
typedef char ElemType;
typedef struct {
    ElemType *elem;		//储存空间基址 
    int length;		//当前长度 
    int listsize;	//储存容量 
}List;
void Menu();
void InitList(List *L);
void Insert(List* L,ElemType elem);
void SortList(List *L);
void MergeList(List *LA,List *LB,List *LC);
void RemoveRepeat(List* L);
void Intersect(List* LA,List* LB,List* LC);
void DifferenceSet(List* LA,List* LB,List* LC);
void ComplementarySet(List *L1,List *L,List *LC);
void Output(List *L);
int main() {
	int command;//菜单命令 
	ElemType La[ARRAY_SIZE],Lb[ARRAY_SIZE];
    List LA, LB, LC, Lall;
    InitList(&LA); 
	InitList(&LB);
	InitList(&LC);//储存结果 
	InitList(&Lall);//储存全集 
	for(ElemType i='a';i<='z';i++){//构建全集 
		Insert(&Lall,i);
	}
	cout<<"请输入集合A:" <<endl; 
	gets(La);
	for(int i=0;La[i]!='\0'; i++){//将输入数据添加进LA 
    	if(La[i]>='a' && La[i]<='z')
    	Insert(&LA,La[i]);
	}
	
	cout<<"请输入集合B:" <<endl; 
	gets(Lb);
	for(int i=0;Lb[i]!='\0'; i++){//将输入数据添加进LB 
    	if(Lb[i]>='a'&&Lb[i]<='z')
    	Insert(&LB,Lb[i]);
	}
	SortList(&LA);//排序 
	SortList(&LB);
	RemoveRepeat(&LA);//去重 
	RemoveRepeat(&LB);
	cout<<endl; 
	cout<<"排序去重后:" <<endl;
	cout<<"A:  "; 
	Output(&LA);
	cout<<"B:  "; 
	Output(&LB);
	while(true){
		cout << endl;
		Menu();
		cin>>command;
		List LC;
		InitList(&LC);
		switch(command){
			case 1: //并集 
				cout<<"并集:"<<endl; 
				MergeList(&LA,&LB,&LC);
				Output(&LC);
				break;
			case 2://交集 
				cout<<"交集:"<<endl;
				Intersect(&LA,&LB,&LC);
				Output(&LC);
				break;
		    case 3://差集 
		    	cout<<"差集:"<<endl;
		    	cout<<"A-B:"<<endl;
		    	DifferenceSet(&LA,&LB,&LC);
		    	Output(&LC);
		    	cout<<"B-A:"<<endl;
		    	InitList(&LC);
		    	DifferenceSet(&LB,&LA,&LC);
		    	Output(&LC);
		    	break;
			case 4://补集 
				cout<<"A的补集:"<<endl;
				ComplementarySet(&LA,&Lall,&LC);
				Output(&LC);
				InitList(&LC);
				cout<<"B的补集:"<<endl;
				ComplementarySet(&LB,&Lall,&LC);
				Output(&LC);
				break;
			case 5://退出 
				return 0;
			default:
				cout<<"输入错误!"<<endl;
				break;
		} 
	}
}

void Menu()//菜单 
{
	cout << "----------------1:  求并集---------------" << endl;
	cout << "----------------2:  求交集---------------" << endl;
	cout << "----------------3:  求差集---------------" << endl;
	cout << "----------------4:  求补集---------------" << endl;
	cout << "----------------5:  退出-----------------" << endl;
	cout << "\n请输入你的选择 :" << endl;
}
void InitList(List *L)//构造一个空的线性表L
{
    L->elem = (ElemType*)malloc(LIST_INIT_SIZE * sizeof(ElemType));
    L->length = 0;
    L->listsize = LISTSIZE;
}
void Insert(List* L,ElemType elem)//添加元素 
{
	L->length++;
	if (L->length > L->listsize){//此时输入的数据个数超出了此线性表的最大容量
		ElemType* newbase;
		newbase = (ElemType*)realloc(L->elem, (L->listsize + LISTSIZE) * sizeof(ElemType));//开辟新空间 
		L->elem = newbase;
		L->listsize += LISTSIZE;
	}
	L->elem[L->length - 1] = elem;
}
void SortList(List *L)//排序 
{
	int i,j;
	ElemType temp;
	for(i=0; i<L->length-1; i++){//冒泡排序 
		for(j=0; j<L->length-i-1; j++){
			if(L->elem[j]>L->elem[j+1]){
				temp = L->elem[j];
				L->elem[j] = L->elem[j+1];
				L->elem[j+1] = temp;
			}
		}
	}
}
void MergeList(List *LA,List *LB,List *LC)//顺序归并 
{
	ElemType *pa,*pb,*pa_end,*pb_end;
	pa = &LA->elem[0];//指向第一个节点
	pb = &LB->elem[0];
	pa_end = &LA->elem[LA->length-1];//指向最后一个节点 
	pb_end = &LB->elem[LB->length-1];
	while(pa<=pa_end && pb<=pb_end){//一旦一个顺序表循环至最后一个节点便结束 
		if(*pa < *pb){
			Insert(LC,*pa++);
		}
		else if(*pa > *pb){
			Insert(LC,*pb++);
		}
		else{
			Insert(LC,*pb);
			pa++;
			pb++;
		}
	}
	while(pa<=pa_end){//将剩余的加入LC 
		Insert(LC, *pa++);
	}
	while(pb<=pb_end){
		Insert(LC, *pb++);
	}
}
void RemoveRepeat(List* L){//去重 
	int i=0;
	ElemType *p,*q,*p_last;
	p=&(L->elem[0]);
	p_last=p+L->length-1;//储存最后一个元素的地址 
	while(p<p_last){
		p_last=&L->elem[0]+L->length-1;
		if(*p==*(p+1)){//比较相邻两个字母
			q=p+1;
			while(q<=p_last){
				*q=*(q+1);//后面的元素前移  
				q++; 
			}
			L->length--; 
		}
		else
		p++;
	}
}
void Intersect(List* LA,List* LB,List* LC){//求交集 
	ElemType *pa,*pb,*pa_end,*pb_end;
	pa = &LA->elem[0];
	pb = &LB->elem[0];
	pa_end = &LA->elem[LA->length-1];
	pb_end = &LB->elem[LB->length-1];
	while(pa<=pa_end && pb<=pb_end){
		if(*pa < *pb){
			pa++;
		}
		else if(*pa > *pb){
			pb++;
		}
		else{//将相同的添加进LC 
			Insert(LC,*pb);
			pa++;
			pb++;
		}
	}
}
void DifferenceSet(List* LA,List* LB,List* LC)//求差集 
{
	ElemType *pa,*pb,*pa_end,*pb_end;
	pa = &LA->elem[0];
	pb = &LB->elem[0];
	pa_end = &LA->elem[LA->length-1];
	pb_end = &LB->elem[LB->length-1];
	while(pa<=pa_end && pb<=pb_end){
		if(*pa < *pb){
			Insert(LC,*pa++);
		}
		else if(*pa > *pb){
			pb++;
		}
		else{
			pa++;
			pb++;
		}
	}
	while(pa<=pa_end)//将剩余的加入LC
	Insert(LC, *pa++); 
}
void ComplementarySet(List *L1,List *L,List *LC)//求补集
{ 
	DifferenceSet(L,L1,LC);
}
void Output(List *L){//输出顺序表中的内容 
	for(int i=0; i<L->length;i++)
	cout<<L->elem[i];
	if(L->length==0)//若顺序表为空 
	cout<<"null";
	cout<<endl;
}
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值