南邮数据结构实验1.1 顺序表的操作

内容和提示:


1.在顺序表类SeqList中增加成员函数void Reverse(),实现顺序表的逆置。

2.在顺序表类SeqList中增加成员函数bool DeleteX(const T &x),删除表中所有元素值等于x的元素。若表中存在这样的元素,则删除之,且函数返回true;否则函数返回false。

3.编写main函数,调用上述新增函数。

4.提示:创建LinearList.h,SeqList.h文件包含程序2.1和程序2.2的代码。在其中新增上述两个函数。


#include 
using namespace std;
const int SIZE=20 ;

template 
class LinearList
{
protected:
		int n;					//线性表的长度

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)=0;
		virtual bool Delete(int i)=0;
		virtual bool Update(int i,T x)=0;
		virtual void Output(ostream &out) const=0;

};

//#include"linearlist.h"

template 
class SeqList:public LinearList
{
	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;
		void Reserve()const;          //顺序表逆置
		bool DeleteX(const T &x);     //删除相同的元素
	private:
		int maxLength;
		T *elements;
};

template 
SeqList::SeqList(int mSize)
{
	maxLength=mSize;
	elements=new T[maxLength];
	n=0;
}

template 
bool SeqList::IsEmpty() const
{
	return n==0;
}

template 
int SeqList::Length() const
{	
	return n;
}

template
bool SeqList::Find(int i,T &x) const
{
	if(i<0 ||i>n-1)
	{
		cout<<"Out of Bounds"<
int SeqList::Search(T x) const
{
	for(int j=0;j
bool SeqList::Insert(int i,T x)
{
	if(i<-1 ||i>n-1)
	{	
		cout<<"Out of Bounds"<i;j--)
		elements[j+1]=elements[j];
	elements[i+1]=x;
	n++;return true;
}

template 
bool SeqList::Delete(int i)
{
	if(!n)
	{
		cout<<"OverFlow"<n-1)
	{
		cout<<"Out of Bounds"<
bool SeqList::Update(int i,T x)
{
	if(i<0||i>n-1)
	{
		cout<<"Out of Bounds"<
void SeqList::Output(ostream &out)const
{
	for(int i=0;i                    //顺序表逆置
void SeqList::Reserve() const
{
	T x;
	for(int i=0;i                   //删除相同的元素
bool SeqList::DeleteX(const T &x)
{
	int count=0;
	for(int i=0;i=0)
		Delete(Search(x));
	}

	return true;
	}
}

//#include"seqlist.h"
//#include"singlelist.h"

void main()
{

	SeqList  LA(SIZE);
	int x,n,a;
		cout<<"Please input the length:"<>n;
		cout<<"Please input the SeqList:"<>x;
			LA.Insert(i-1,x);
		}
		LA.Output(cout);

		cout<<"Please input x to be deleted:"<>a;
		LA.DeleteX(a);
		cout<<"After delete:"<


你可能感兴趣的:(NJUPT-CS)