問題及代碼:
/*
*Copyright (c)2016,煙台大學計算機與控制工程學院
*All rights reserved.
*檔案名稱:111.cpp
*作 者:王曼
*完成日期:2016年9月19日
*版 本 号:v1.0
*
*問題描述:假設有兩個集合 A 和 B 分别用兩個線性表 LA 和 LB 表示,即線性表中的資料元素即為
集合中的成員。設計算法,用函數unionList(List LA, List LB, List &LC )函數實作該
算法,求一個新的集合C=A∪B,即将兩個集合的并集放線上性表LC中。
*輸入描述:每個集合
*程式輸出:合并後的集合
*/
list.h代碼:
#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED
#define MaxSize 50
#include <stdio.h>
#include <malloc.h>
typedef 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)#endif // LIST_H_INCLUDED
#endif
void unionList(SqList *LA, SqList *LB, SqList *&LC);
功能函數list.cpp代碼:
//#include "list.h"
//用數組建立線性表
void CreateList(SqList *&L, ElemType a[], int n)
{
int i;
L=(SqList *)malloc(sizeof(SqList));
for (i=0; i<n; i++)
L->data[i]=a[i];
L->length=n;
}
//初始化線性表InitList(L)
void InitList(SqList *&L) //引用型指針
{
L=(SqList *)malloc(sizeof(SqList));
//配置設定存放線性表的空間
L->length=0;
}
//銷毀線性表DestroyList(L)
void DestroyList(SqList *&L)
{
free(L);
}
//判定是否為空表ListEmpty(L)
bool ListEmpty(SqList *L)
{
return(L->length==0);
}
//求線性表的長度ListLength(L)
int ListLength(SqList *L)
{
return(L->length);
}
//輸出線性表DispList(L)
void DispList(SqList *L)
{
int i;
if (ListEmpty(L)) return;
for (i=0; i<L->length; i++)
printf("%d ",L->data[i]);
printf("\n");
}
//求某個資料元素值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;
}
//插入資料元素ListInsert(L,i,e)
bool ListInsert(SqList *&L,int i,ElemType e)
{
int j;
if (i<1 || i>L->length+1)
return false; //參數錯誤時傳回false
i--; //将順序表邏輯序号轉化為實體序号
for (j=L->length; j>i; j--) //将data[i..n]元素後移一個位置
L->data[j]=L->data[j-1];
L->data[i]=e; //插入元素e
L->length++; //順序表長度增1
return true; //成功插入傳回true
}
//删除資料元素ListDelete(L,i,e)
bool ListDelete(SqList *&L,int i,ElemType &e)
{
int j;
if (i<1 || i>L->length) //參數錯誤時傳回false
return false;
i--; //将順序表邏輯序号轉化為實體序号
e=L->data[i];
for (j=i; j<L->length-1; j++) //将data[i..n-1]元素前移
L->data[j]=L->data[j+1];
L->length--; //順序表長度減1
return true; //成功删除傳回true
}
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"
//用main寫測試代碼
int main()
{
SqList *sq_a, *sq_b, *sq_c;
ElemType a[6]= {1,2,3,4,5,6};
CreateList(sq_a, a, 6);
printf("LA: ");
DispList(sq_a);
ElemType b[6]= {7,8,9,0};
CreateList(sq_b, b, 4);
printf("LB: ");
DispList(sq_b);
unionList(sq_a, sq_b, sq_c);
printf("LC: ");
DispList(sq_c);
return 0;
}
運作結果:

鍛煉了對算法庫的運用能力