#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()
{
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] << " ";
}
}