第3周项目3-求集合并集
来源:互联网 发布:中软国际php培训怎么样 编辑:程序博客网 时间:2024/06/06 04:08
/* *Copyright (c) 2017, 烟台大学计算机学院 *All rights reserved. *文件名称:DSitem3-3.cpp *作 者:于子淇 *完成日期:2017年9月20日 *版 本 号:Code::Blocks 12.11 * *问题描述:假设有两个集合 A 和 B 分别用两个线性表 LA 和 LB 表示,即线性表中的数据元素即为集合中的成员。 *设计算法,用函数unionList(List LA, List LB, List &LC )函数实现该算法,求一个新的集合C=A∪B,即将两个集合的并集放在线性表LC中。 *输入描述:无 *程序输出:见程序运行结果演示 */
list.cpp
#include <stdio.h>#include <malloc.h>#include "list.h"//下面实现要测试的各个自定义函数//用数组创建线性表void CreateList(SqList *&L, ElemType a[], int n)//由a中的n个元素建立顺序表{ int i; L=(SqList *)malloc(sizeof(SqList));//分配存放线性表的空间 for (i=0; i<n; i++) L->data[i]=a[i]; L->length=n;}//输出线性表DispList(L)void DispList(SqList *L){ int i; if (ListEmpty(L))//如果线性表为空表则返回,退出DispList函数 return;//由于函数为void类型所以不需要写出返回值 for (i=0; i<L->length; i++) printf("%d ",L->data[i]); printf("\n");}//判定是否为空表ListEmpty(L)bool ListEmpty(SqList *L){ return(L->length==0);}//求线性表中某个元素的值GetElem(L,i,e)bool GetElem(SqList *L,int i,ElemType &e){ if (i<1 || i>L->length) return false; e=L->data[i-1]; return true;}//按元素值查找LocateElem(L,e)int LocateElem(SqList *L, ElemType e){ int i=0; while (i<L->length && L->data[i]!=e) i++; if (i>=L->length) return 0; else return i+1;}//求线性表的长度ListLength(L)int ListLength(SqList *L){ return(L->length);}//插入数据元素ListInsert(&L,i,e)bool ListInsert(SqList *&L,int i,ElemType e){ int j; if(i<1||i>L->length+1) return false; i--; for(j=L->length;j>i;j--) L->data[j]=L->data[j-1]; L->data[i]=e; L->length++; return true;}//初始化线性表InitList(&L)void InitList(SqList *&L){ L=(SqList *)malloc(sizeof(SqList));//分配存放线性表的空间 L->length=0;}//销毁线性表 Destroy(&L)void DestroyList(SqList *&L){ free(L);}//合并元素void unionList(SqList *LA, SqList *LB, SqList *&LC){ int lena,i; ElemType e; InitList(LC); for (i=1; i<=ListLength(LA); i++) //将LA的所有元素插入到Lc中 { GetElem(LA,i,e); ListInsert(LC,i,e); } lena=ListLength(LA); //求线性表LA的长度 for (i=1; i<=ListLength(LB); i++) { GetElem(LB,i,e); //取LB中第i个数据元素赋给e if (!LocateElem(LA,e)) //LA中不存在和e相同者,插入到LC中 ListInsert(LC,++lena,e); }}
main.cpp
#include "list.h"int main(){ SqList *sq_a, *sq_b, *sq_c; ElemType a[6]= {5,8,7,2,4,9}; CreateList(sq_a, a, 6); printf("LA: "); DispList(sq_a); ElemType b[6]= {2,3,8,6,0}; CreateList(sq_b, b, 5); printf("LB: "); DispList(sq_b); unionList(sq_a, sq_b, sq_c); printf("LC: "); DispList(sq_c); return 0;}list.h
#ifndef LIST_H_INCLUDED#define LIST_H_INCLUDED#include<malloc.h>#include<stdio.h>#define MaxSize 50typedef int ElemType;typedef struct{ ElemType data[MaxSize]; int length;} SqList;void CreateList(SqList *&L, ElemType a[], int n);//用数组创建线性表void InitList(SqList *&L);//初始化线性表InitList(L)void DestroyList(SqList *&L);//销毁线性表DestroyList(L)bool ListEmpty(SqList *L);//判定是否为空表ListEmpty(L)int ListLength(SqList *L);//求线性表的长度ListLength(L)void DispList(SqList *L);//输出线性表DispList(L)bool GetElem(SqList *L,int i,ElemType &e);//求某个数据元素值GetElem(L,i,e)int LocateElem(SqList *L, ElemType e);//按元素值查找LocateElem(L,e)bool ListInsert(SqList *&L,int i,ElemType e);//插入数据元素ListInsert(L,i,e)bool ListDelete(SqList *&L,int i,ElemType &e);//删除数据元素ListDelete(L,i,e)void unionList(SqList *LA, SqList *LB, SqList *&LC);#endif程序运行结果演示:
多文件组织演示:
基于之前构建的顺序表算法库进一步去解决更多的实际问题
阅读全文
0 0
- 第3周项目3 - 求集合并集
- 第3周项目3-求集合并集
- 【第3周 项目3 - 求集合并集】
- 第3周项目3求集合并集
- 第3周实践项目3--求集合并集
- 第3周 项目3-求集合并集
- 第3周项目3 求集合并集
- 第3周实践项目3--求集合并集
- 第3周 项目3 - 求集合并集
- 第3周 项目3-求集合并集
- 第3周项目3 求集合并集
- 第3周项目3 求集合并集
- 第3周项目3 求集合并集
- 第3周 项目3--求集合并集
- 第3周项目3-求集合并集
- 第3周项目3—求集合并集
- 第3周项目3-求集合并集
- 第3周项目3-求集合并集
- HDU 4825 Xor Sum(01字典树)
- ConstraintLayout 优秀博客
- 基础概率公式汇总
- vue2几种不同组件(页面)间传值的方式
- Docker和孤儿进程、僵尸进程
- 第3周项目3-求集合并集
- [BZOJ]3226: [Sdoi2008]校门外的区间 线段树
- 数学笔记8——最值问题
- 机器学习技法课程学习笔记1 -- Linear Support Vector Machine
- offer15
- IPC机制系列之二 IPC机制的基础概念Serializable、Parcelable以及Binder
- 网站统计中的数据收集原理及实现-埋点统计
- Python3 面向对象——继承,重写,类属性与方法
- IIS网站本机可以访问但局域网其他机器无法访问 解决方法