南郵資料結構實驗1.1 順序表的操作

think_ycx發表於2014-10-02

內容和提示:


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 <iostream>
using namespace std;
const int SIZE=20 ;

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

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(i==maxLength)
	{
		cout<<"OverFlow"<<endl;return false;
	}
	for(int j=n-1;j>i;j--)
		elements[j+1]=elements[j];
	elements[i+1]=x;
	n++;return true;
}

template <class T>
bool SeqList<T>::Delete(int i)
{
	if(!n)
	{
		cout<<"OverFlow"<<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;
	return true;
}

template <class T>
void SeqList<T>::Output(ostream &out)const
{
	for(int i=0;i<n;i++) out<<elements[i]<<"  ";
	out<<endl;
}

template<class T>                    //順序表逆置
void SeqList<T>::Reserve() const
{
	T x;
	for(int i=0;i<n/2;i++)
	{
		x=elements[i];
		elements[i]=elements[n-1-i];
		elements[n-1-i]=x;
	}

}

template<class T>                   //刪除相同的元素
bool SeqList<T>::DeleteX(const T &x)
{
	int count=0;
	for(int i=0;i<n;i++)
	{
	if (Search(x) < 0)	return false;
	else
	{
		while (Search(x)>=0)
		Delete(Search(x));
	}

	return true;
	}
}

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

void main()
{

	SeqList <int> LA(SIZE);
	int x,n,a;
		cout<<"Please input the length:"<<endl;
		cin>>n;
		cout<<"Please input the SeqList:"<<endl;
		for(int i=0;i<n;i++)
		{	
			cin>>x;
			LA.Insert(i-1,x);
		}
		LA.Output(cout);

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

		LA.Reserve();
		cout<<"After reserved:"<<endl;
		LA.Output(cout);
}


相關文章