3 顺序表ADT模板简单应用算法设计:线性表的合并
作者: 冯向阳时间限制: 1S章节: DS:线性表
截止日期: 2022-06-30 23:55:00
问题描述 :
目的:使用自行设计的顺序表ADT或STL中的vector模板,设计并实现顺序表应用场合的一些简单算法设计。
应用4:试设计一个算法,将所有在线性表LB中但不在LA中的数据元素插入到LA中,算法执行之后,线性表LB不再存在。
参考函数原型:
(2)顺序表ADT版本
template<class ElemType>
void Union_Sq( SqList<ElemType> &LA, SqList<ElemType> &LB );
(2)vector版本
template<class ElemType>
void Union_Sq( vector<ElemType> &LA, vector<ElemType> &LB );
输入说明 :
第一行:顺序表的数据元素类型标记(0:int;1:double;2:char;3:string;其余值:输出err)
第二行:待处理顺序表LA的数据元素(数据元素之间以空格分隔)
第三行:待处理顺序表LB的数据元素(数据元素之间以空格分隔)
输出说明 :
第一行:顺序表LA的遍历结果(数据元素之间以“,”分隔)
第二行:顺序表LB的遍历结果(数据元素之间以“,”分隔)
空行
第三行:合并后顺序表LA的遍历结果(数据元素之间以“,”分隔)
--------------------
0
13 5 27 9 32 123 76 98 54 87
13 5 13 9 32 51 76 5 54 8
------------------------------------------
13,5,27,9,32,123,76,98,54,87
13,5,13,9,32,51,76,5,54,8
13,5,27,9,32,123,76,98,54,87,51,8
--------------------------------
#include<iostream>
#include <iomanip>
using namespace std;
template<class T>
struct student
{
T array[20];
int length = 0;
};
void zero2(student<string>& k)
{
for(int i=0;i<20;i++)
k.array[i]="";
}
template<class T>
void complex(student<T>& a, student<T>& b, student<T>& c)
{
for (int u = 0; u < a.length; u++)
{
c.array[u] = a.array[u];
c.length++;
}//对新的顺序表赋值
for (int k = 0; k < b.length; k++)
{
for (int y = 0; y < a.length; y++)
{
if (b.array[k] == a.array[y])
{
goto FLAG;
}
}
c.array[c.length]=b.array[k] ;
c.length++;
FLAG:
cout << "";
}
}
template<class T>
void display(student<T>& m)
{
for (int e = 0; e < m.length; e++)
{
cout << m.array[e];
if (e != m.length - 1)
cout << ",";
}
cout << endl;
}
template<class T>
void display_double12(student<T>& m)
{
for (int e = 0; e < m.length; e++)
{
cout <<m.array[e];
if (e != m.length - 1)
cout << ",";
}
cout << endl;
}
template<class T>
void display_double3(student<T>& m)
{
for (int e = 0; e < m.length; e++)
{
cout <<m.array[e];
if (e != m.length - 1)
cout << ",";
}
}
template<class T>
void creat(student<T>& a)
{
int n = 0;
T i ;
while (cin >> i)
{
a.array[n] = i;
a.length++;
n++;
if (cin.get() == '\n')
break;
}
}
int main()
{
int judge = 0;
cin >> judge;
if (judge == 0)
{
student <int>c;
student <int>a;
student <int>b;
int x = 0;
int i = 0;
creat(a);
display(a);
creat(b);
display(b);
complex(a, b, c);
cout << endl;
display(c);
}
else if (judge == 1)
{
student <double>c;
student <double>a;
student <double>b;
int x = 0;
int i = 0;
creat(a);
display_double12(a);
creat(b);
display_double12(b);
complex(a, b, c);
cout << endl;
display_double3(c);
}
else if (judge == 2)
{
student <char>c;
student <char>a;
student <char>b;
int x = 0;
int i = 0;
creat(a);
display(a);
creat(b);
display(b);
complex(a, b, c);
cout << endl;
display(c);
}
else if (judge == 3)
{
student <string>c;
student <string>a;
student <string>b;
int x = 0;
int i = 0;
creat(a);
display(a);
creat(b);
display(b);
complex(a, b, c);
cout << endl;
display(c);
}
else
{
cout << "err" <<endl;
}
return 0;
}