数据结构实习二1

要求: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;  
}  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值