#include <iostream>
#include <string>
using namespace std;
const int SIZE = 20;
template <class T>
class LinearList
{
public:
virtual bool IsEmpty() const = 0;
virtual int Length() const = 0;
virtual bool Find(int i, T& x) const = 0; virtual int Search(T x) const = 0;
virtual bool Insert(int i, T x) = 0;
virtual bool Delete(int i) = 0;
virtual bool Update(int i, T x) = 0;
virtual void Output(ostream& out) const = 0;
};
template <class T>
class SeqList :public LinearList<T>
{
public:
SeqList(int mSize);
~SeqList()
{
delete[] elements;
}
bool IsEmpty() const;
int Length() const;
bool Find(int i, T& x) const;
int Search(T x) const;
bool Insert(int i, T x);
bool Delete(int i);
bool Update(int i, T x);
void Output(ostream& out)const;
private:
int maxLength;
T* elements;//动态-维数组的指针
protected:
int n;
};
template <class T>
void Union(SeqList <T>& LA, SeqList <T> LB)
{
T x;
for (int i = 0; i < LB.Length(); i++)
{
LB.Find(i, x);
if (LA.Search(x) == -1)
{
LA.Insert(LA.Length() - 1, x);
}
}
}
template <class T>
SeqList<T>::SeqList(int mSize)
{
maxLength = mSize;
elements = new T[maxLength];
n = 0;
}
template <class T>
bool SeqList<T>::IsEmpty() const
{
return n == 0;
}
template <class T>
int SeqList<T>::Length() const
{
return n;
}
template<class T>
bool SeqList<T>::Find(int i, T& x) const
{
if (i<0 || i>n - 1)
{
cout << "out of Bounds" << endl;
return false;
}
x = elements[i];
return true;
}
template<class T>
int SeqList<T>::Search(T x) const
{
for (int j = 0; j < n; j++)
{
if (elements[j] == x)
return j;
}
return -1;
}
template<class T>
bool SeqList<T>::Insert(int i, T x)
{
if (i<-1 || i>n - 1)
{
cout << "out Of Bounds" << endl; return false;
}
//上溢出检查
if (n == maxLength)
{
cout << "overFlow" << endl;
return false;
}
//从后往前逐个后移元素
for (int j = n - 1; j > i; j--)
elements[j + 1] = elements[j];
//将x插入下标为i的元素后面
elements[i + 1] = x;
n++;
return true;
}
template <class T>
bool SeqList<T>::Delete(int i)
{
//下溢出检查
if (!n)
{
cout << "UnderFlow" << endl; return false;
}
if (i<0 || i>n - 1)
{
cout << "out Of Bounds" << endl; return false;
}
for (int j = i + 1; j < n; j++)
elements[j - 1] = elements[j];
//从前往后逐个前移元素
n--;
return true;
}
template <class T>
bool SeqList<T>::Update(int i, T x)
{
if (i<0 || i>n - 1)
{
cout << "out of Bounds" << endl;
return false;
}
elements[i] = x;
//将下标为i的元素值修改为x
return true;
}
template <class T>
void SeqList<T>::Output(ostream& out)const
{
for (int i = 0; i < n; i++)
out << elements[i] << ' ';
out << endl;
}
void main()
{
SeqList<int> LA(SIZE);//定义顺序表对象LA,表示集合A
SeqList<int> LB(SIZE);//定义顺序表对象LB,表示集合B
for (int i = 0; i < 5; i++)
LA.Insert(i - 1, i); //插人元素0~4,构造集合LA={0,1,2,3,4}
for (int i = 5; i < 10; i++)
LB.Insert(i - 6, i);//插人5~9,构造集合B={5,6,7,8,9}
LB.Insert(-1, 0);//LB中插入0,LB={0,5,6,7,8,9}
LB.Insert(3, 2);//LB中插入2,LB={0,5,6,7,2,8,9}
LB.Insert(LB.Length() - 1, 4);//LB中插人4, LB={0,5,6,7,2,8,9,4}
Union(LA, LB);//两集合并,结果放人LA中
LA.Output(cout);//输出最终结果LA={0,1,2,3,4,5,6,7,8,9}
system("pause");
}
线性表的顺序表示以及两个顺序表实现“并”的程序
最新推荐文章于 2022-04-15 10:32:07 发布