南邮数据结构实验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 <iostream>
using namespace std;
int const LEN = 50;
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>
{
private:
    int maxLength;         //线性表的最大长度
    T *elements;           //动态一维数组的指针
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);
};
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;  //对i进行越界检查
        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];
    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;
    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>::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 tmp = n, i;    //用于判断是否有删除数据
    n = 0;
    int *hash = new int[tmp];
    for(i = 0; i < tmp; i++)
    {
        hash[i] = 0;
        if(elements[i] == x)
            hash[i]++;
    }
    for(i = 0; i < tmp; i++)
        if(!hash[i])
            elements[n++] = elements[i];
    delete[]hash;
    if(n == tmp)   //判断是否有删除的数据
        return false;
    else
        return true;
}
int main()
{
    int del_data, len ,num;
    SeqList<int> A(LEN);
    cout << "Input the length of the seqlist: ";
    cin >> len;
    cout << "\nInput each element: ";
    for(int i = 0; i < len; i++)
    {
        cin >> num;
        A.Insert(i - 1, num);
    }
    cout << "\nInitial seqlist: ";
    A.Output(cout);
    A.Reverse();
    cout << "\nResevered seqlist: ";
    A.Output(cout);
    cout << "\nInput the element to be deleted: ";
    cin >> del_data;
    if(A.DeleteX(del_data) == true)
    {
        cout << "\nSeqlist after being deleted: ";
        A.Output(cout);
    }
    else
        cout << "\nNot found" << endl;
    return 0;
}


你可能感兴趣的:(数据结构,实验)