您的位置:首页 > 理论基础 > 数据结构算法

数据结构实习二1

2015-10-10 21:09 337 查看
要求:1.在顺序表类SeqList中增加成员函数void Reverse(),实现顺序表的逆置。

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

之,且函数返回true。否则函数返回false。

#include <iostream>
using namespace std;
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;
protected:
int n;
};

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 Reverse();
bool DeleteX(const T &x);
private:
int n;
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 << endl;
return false;
}
x = elements[i];
return true;
}

template <class T>
int SeqList<T>::Search(T x) const
{
for(int i = 0; i < n; ++i)
if(elements[i] == x) return i;
return -1;
}

template <class T>
bool SeqList<T>::Insert(int i, T x)
{
if(i < -1 || i > n - 1) {
cout << "Out of Bounds" << endl << endl;
return false;
}
if(n == maxLength) {
cout << "OverFlow" << endl << 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 << "UnderFlow" << endl << endl;
return false;
}
if(i < 0 || i > n - 1) {
cout << "Out of Bounds" << endl << 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 << 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 << endl;
}

template <class T>
void Union(SeqList<T> &A, SeqList<T> &B)
{
T x;
for(int i = 0; i < B.Length(); ++i) {
B.Find(i, x);
if(A.Search(x) == -1)
A.Insert(A.Length() - 1, x);
}
}

template <class T>
void SeqList<T>::Reverse()
{
T temp;
for(int i=0;i<n/2;i++){
temp = elements[i];
elements[i] = elements[n-i-1];
elements[n-i-1] = temp;
}
}
template <class T>
bool SeqList<T>::DeleteX(const T &x)
{
int flag = n;
for(int i = 0; i < n; ++i)
if(elements[i] == x) {
Delete(i);
i--;
}
if(flag != n) return true;
return false;
}
int main()
{
SeqList<int> LA(20), LB(20);
for(int i = 0; i < 5; ++i)
LA.Insert(i - 1, i);
cout<<"LA:"<<endl;
LA.Output(cout);
for(int i = 5; i < 10; ++i)
LB.Insert(i - 6, i);
cout<<"LB:"<<endl;
LB.Output(cout);
Union(LA, LB);
cout << "LA&LB:" << endl;
LA.Output(cout);
LB.Reverse();
cout << "逆置LB为:" << endl;
LB.Output(cout);
if(LB.DeleteX(7)) cout << "删除LB中7成功" << endl;
else cout << "LB没有等于7的元素" << endl;
cout << "LB:" << endl;
LB.Output(cout);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据结构