记录学习过程,实现顺序表的基本操作,并求集合的交集和并集
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#define OK 1
#define ERROR -1
typedef int Elemtype;
typedef int status;
typedef struct {
Elemtype* elem;
int length;
int size;
}SqList;
//初始化
status InitList(SqList* L);
//取值
Elemtype GetElem(int i, SqList* L);
//查找
Elemtype LocateElem(Elemtype e, SqList* L);
//插入
status InsertElem(int i, Elemtype e, SqList* L);
//删除
status deleteElem(int i, SqList* L);
//销毁
status DestroyList(SqList* L);
//判断是否为空
status ListEmpty(SqList* L);
//清空
status ClearList(SqList* L);
//取交集
SqList* Intersection(SqList* L, SqList* S);
//取并集
SqList* UnionList(SqList* L, SqList* S);
//遍历顺序表
status TraverseList(SqList* L);
//按顺序插入
status InsertSqList(Elemtype e, SqList* L);
#define _CRT_SECURE_NO_WARNINGS
#include"SqList.h"
//初始化
status InitList(SqList* L) {
L->elem = (Elemtype*)malloc(sizeof(Elemtype) * 30);
if (L->elem == NULL) {
return ERROR;
}
L->size = 30;
L->length = 0;
return OK;
}
//取值
Elemtype GetElem(int i, SqList* L) {
if (i<1 || i>L->length) {
return ERROR;
}
return L->elem[i - 1];
}
//查找
Elemtype LocateElem(Elemtype e, SqList* L) {
for (int i = 0; i < L->length; i++) {
if (L->elem[i] == e) return L->elem[i];
}
return 0;
}
//按顺序插入
status InsertSqList(Elemtype e, SqList* L) {
if (!L->elem) return ERROR;
if (L->length < L->size) {
L->elem[L->length] = e;
L->length++;
return OK;
}
else
{
printf("空间不足\n");
return 0;
}
}
//指定位置插入
status InsertElem(int i, Elemtype e, SqList* L) {
if (i<1 || i>L->length) return ERROR;
for (int j = L->length; j > i - 1; j--) {
L->elem[j] = L->elem[j -1];
}
L->elem[i - 1] = e;
L->length++;
return OK;
}
//删除
status deleteElem(int i, SqList* L) {
if (i<1 || i>L->length) return ERROR;
for (int j = i; i < L->length; i++) {
L->elem[j - 1] = L->elem[j];
}
L->length--;
return OK;
}
//销毁
status DestroyList(SqList* L) {
L->elem = NULL;
L->length = 0;
L->size = 0;
return OK;
}
//判断是否为空
status ListEmpty(SqList* L) {
if (L->length == 0&&L->elem==NULL) {
return 1;
}
return 0;
}
//清空
status ClearList(SqList* L) {
L->elem = NULL;
L->length = 0;
return OK;
}
//取交集
SqList* Intersection(SqList* L, SqList* S) {
SqList * R=(SqList*)malloc(sizeof(SqList));
InitList(R);
for(int i=0;i<L->length;i++){
for (int j = 0; j < S->length; j++) {
if (L->elem[i] == S->elem[j]) {
if (!LocateElem(S->elem[j], R)) {
InsertSqList(S->elem[j], R);
}
}
}
}
return R;
}
//取并
SqList* UnionList(SqList* L, SqList* S) {
SqList* R=(SqList*)malloc(sizeof(SqList));;
InitList(R);
R = L;
for (int i = 0; i < R->length; i++) {
for (int j = 0; j < S->length; j++) {
if (R->elem[i] != S->elem[j]) {
if (!LocateElem(S->elem[j], R)) {
InsertSqList(S->elem[j], R);
}
}
}
}
return R;
}
//遍历顺序表
status TraverseList(SqList* L) {
for (int i = 0; i < L->length; i++) {
int a = L->elem[i];
printf("%d ", a);
}
printf("\n");
return OK;
}
#define _CRT_SECURE_NO_WARNINGS
#include"SqList.h"
#include <iostream>
using namespace std;
int main() {
SqList L1;
SqList L2;
if (!InitList(&L1)) {
cout << "L1初始化失败" << endl;
}
if (!InitList(&L2)) {
cout << "L2初始化失败" << endl;
}
int a1[6] = { 4,7,9,12,23,34 };
int a2[6] = { 4,9,10,20,23,34 };
for (int i = 0; i < 6; i++) {
InsertSqList(a1[i], &L1);
InsertSqList(a2[i], &L2);
}
TraverseList(&L1);
TraverseList(&L2);
SqList* R1= Intersection(&L1, &L2);
cout << "交集R1" << endl;
TraverseList(R1);
SqList* R2= UnionList(&L1, &L2);
cout << "并集R2" << endl;
TraverseList(R2);
deleteElem(3, R2);
TraverseList(R2);
cout << "清空R2" << endl;
//清空
ClearList(R2);
//判断是否为空
if (ListEmpty(R2)) {
cout << "R2为空" << endl;
}
//在R1第三个位置插入999
InsertElem(3, 999, R1);
cout << "R1第三个位置插入999为:" << endl;
TraverseList(R1);
if (DestroyList(R1) && DestroyList(R2) && DestroyList(&L1) && DestroyList(&L2)) {
cout << "R1,R2,L1,L2已销毁" << endl;
}
int n1, n2;
InitList(&L1);
InitList(&L2);
cout << "请输入集合1的元素个数" << endl;
cin >> n1;
cout << "请依次输入集合1的"<<n1<<"个元素" << endl;
int l ;
for (int i = 0; i < n1; i++) {
cin >> l;
InsertSqList(l, &L1);
}
cout << "请输入集合2的元素个数" << endl;
cin >> n2;
cout << "请依次输入集合2的"<<n2<<"个元素" << endl;
int m;
for (int i = 0; i < n2; i++) {
cin >> m;
InsertSqList(m, &L2);
}
cout << "两个集合的交集为:" << endl;
SqList* L0 = Intersection(&L1, &L2);
TraverseList(L0);
cout << "两个集合的并集为:" << endl;
SqList* S0 = UnionList(&L1, &L2);
TraverseList(S0);
system("pause");
return 0;
}