线性表的顺序表示以及两个顺序表实现“并”的程序

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值