線性表的合併已知兩個集合A和B,現要求一個新的集合A=A∪B。例如,設A=(7,5,3,11),B=(2,6,3),合併後A=(7,5,3,11,2,6)。
阿新 • • 發佈:2018-11-21
#include<iostream>
using namespace std;
#define MAXSIZE 100
#define OK 1
#define ERROR -1
#define OVERFLOW -2
#define MAXSIZE 100 //順序表的最大長度
typedef struct SQLIST{
int *elem; //線性表的起始地址
int length;
//線性表的當前長度,即實際元素個數
}SqList;
void InitList_Sq(SqList &L);
void DestroyList(SqList &L);
void ClearList(SqList &L);
int IsEmpty(SqList L);
void add_from_end(SqList &L, int e);
void ListOutput(SqList L, SqList Q); //輸出函式
void bianli(SqList L);
int main()
{
SqList L, Q;
InitList_Sq(L); //分配空間
InitList_Sq(Q);
add_from_end(L,7); //尾部新增元素;
add_from_end(L,5); //尾部新增元素;
add_from_end(L,3); //尾部新增元素;
add_from_end(L,11); //尾部新增元素;
add_from_end(Q,2); //尾部新增元素;
add_from_end(Q,6); //尾部新增元素;
add_from_end(Q,3); //尾部新增元素;
bianli(L);
cout << endl;
bianli(Q);
cout << endl;
cout << endl;
cout << "結果為:";
ListOutput(L, Q);
cout << endl;
cout << endl;
ClearList(L);
ClearList(Q);
DestroyList(L);
DestroyList(Q);
system("pause");
return 0;
}
void InitList_Sq(SqList &L)
{ //構造一個空的順序表L
L.elem = new int[MAXSIZE]; //為順序表分配空間
if (L.elem)
{
L.length = 0;
}
}
void DestroyList(SqList &L)
{
if (L.elem) delete[]L.elem; //釋放儲存空間
}
void ClearList(SqList &L)
{
L.length = 0; //將線性表的長度置為0
}
int GetLength(SqList L) //獲取順序表的長度
{
return (L.length);
}
int IsEmpty(SqList L) //順序表是否為空
{
if (L.length == 0) return 1;
else return 0;
}
int GetElem(SqList L, int i, int &e) // i為位置序號
{
if (i<1 || i>L.length) return ERROR;
//判斷i值是否合理,若不合理,返回ERROR
e = L.elem[i - 1];
return OK;
}
void add_from_end(SqList &L, int e) //尾部新增數字
{
int num = L.length; //獲取當前長度
L.elem[num] = e; //將最後一個元素賦值給線性表最後一個
L.length++;
}
void bianli(SqList L)
{
for (int i = 0; i < L.length; i++)
{
cout << L.elem[i] << " ";
}
}
void ListOutput(SqList L,SqList Q) //輸出函式
{
int k = 0;
int i;
int n = L.length;
for (i = 0; i < Q.length; i++)
{
k = 1;
for (int j = 0; j < n; j++)
{
if (Q.elem[i] == L.elem[j])
{
break;
}
else
{
k++;
if (k == n)
{
L.elem[L.length] = Q.elem[i];
L.length++;
}
}
}
}
for (i = 0; i < L.length; i++) //列印當前L線性表的內容
{
cout << L.elem[i] << " ";
}
}
using namespace std;
#define MAXSIZE 100
#define OK 1
#define ERROR -1
#define OVERFLOW -2
#define MAXSIZE 100 //順序表的最大長度
typedef struct SQLIST{
int *elem; //線性表的起始地址
int length;
//線性表的當前長度,即實際元素個數
}SqList;
void InitList_Sq(SqList &L);
void DestroyList(SqList &L);
void ClearList(SqList &L);
int IsEmpty(SqList L);
void add_from_end(SqList &L, int e);
void ListOutput(SqList L, SqList Q); //輸出函式
void bianli(SqList L);
int main()
{
InitList_Sq(L); //分配空間
InitList_Sq(Q);
add_from_end(L,7); //尾部新增元素;
add_from_end(L,5); //尾部新增元素;
add_from_end(L,3); //尾部新增元素;
add_from_end(L,11); //尾部新增元素;
add_from_end(Q,2); //尾部新增元素;
add_from_end(Q,6); //尾部新增元素;
add_from_end(Q,3); //尾部新增元素;
cout << endl;
bianli(Q);
cout << endl;
cout << endl;
cout << "結果為:";
ListOutput(L, Q);
cout << endl;
cout << endl;
ClearList(L);
ClearList(Q);
DestroyList(L);
DestroyList(Q);
system("pause");
}
void InitList_Sq(SqList &L)
{ //構造一個空的順序表L
L.elem = new int[MAXSIZE]; //為順序表分配空間
if (L.elem)
{
L.length = 0;
}
}
void DestroyList(SqList &L)
{
if (L.elem) delete[]L.elem; //釋放儲存空間
}
void ClearList(SqList &L)
{
L.length = 0; //將線性表的長度置為0
}
int GetLength(SqList L) //獲取順序表的長度
{
return (L.length);
}
int IsEmpty(SqList L) //順序表是否為空
{
if (L.length == 0) return 1;
else return 0;
}
int GetElem(SqList L, int i, int &e) // i為位置序號
{
if (i<1 || i>L.length) return ERROR;
//判斷i值是否合理,若不合理,返回ERROR
e = L.elem[i - 1];
return OK;
}
void add_from_end(SqList &L, int e) //尾部新增數字
{
int num = L.length; //獲取當前長度
L.elem[num] = e; //將最後一個元素賦值給線性表最後一個
L.length++;
}
void bianli(SqList L)
{
for (int i = 0; i < L.length; i++)
{
cout << L.elem[i] << " ";
}
}
void ListOutput(SqList L,SqList Q) //輸出函式
{
int k = 0;
int i;
int n = L.length;
for (i = 0; i < Q.length; i++)
{
k = 1;
for (int j = 0; j < n; j++)
{
if (Q.elem[i] == L.elem[j])
{
break;
}
else
{
k++;
if (k == n)
{
L.elem[L.length] = Q.elem[i];
L.length++;
}
}
}
}
for (i = 0; i < L.length; i++) //列印當前L線性表的內容
{
cout << L.elem[i] << " ";
}
}