南邮数据结构实验1 单链表操作

内容和提示:


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

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

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

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


#include 
using namespace std;
template 
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 SingleList;
template 
class Node
{
private:
    T element;
    Node *link;
    friend class SingleList;
};

template 
class SingleList : public LinearList
{
private:
    Node* first;
    //int n;
public:
    SingleList(){first = NULL; n = 0;}
    ~SingleList();
    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 Clear();
    void Output(ostream& out) const;
    void Reverse();
    bool DeleteX(const T& x);
};

template 
SingleList :: ~SingleList()
{
    Node *p;
    while(first)
    {
        p = first -> link;
        delete first;
        first = p;
    }
}

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

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

template 
bool SingleList :: Find(int i, T &x)const
{
    if(i < 0 || i > n - 1)
    {
        cout << "Out Of Bound" << endl;
        return false;
    }
    Node *p = first;
    for(int j = 0; j < i; j++)
        p = p -> link;
    x = p -> element;
    return true;
}

template 
int SingleList :: Search(T x)const
{
    int j;
    Node *p = first;
    for(j = 0; p && p -> element != x; j++)
        p = p -> link;
    if(p)
        return j;
    return -1;
}

template 
bool SingleList :: Insert(int i, T x)
{
    if(i < -1 || i > n - 1)
    {
        cout << "Out Of Bounds";
        return false;
    }
    Node *q = new Node;
    q -> element = x;
    Node *p = first;
    for(int j = 0; j < i; j++)
        p = p -> link;
    if(i > -1)
    {
        q -> link = p -> link;
        p -> link = q;
    }
    else
    {
        q -> link = first;
        first = q;
    }
    n++;
    return true;
}

template 
bool SingleList :: Delete(int i)
{
    if(!n)
    {
        cout << "UnderFlow" << endl;
        return false;
    }
    if(i < 0 || i > n - 1)
    {
        cout << "Out Of Bound" << endl;
        return false;
    }
    Node *p = first, *q = first;
    for(int j = 0; j < i - 1; j++)
        q = q -> link;
    if(i == 0)
        first = first -> link;
    else
    {
        p = q -> link;
        q -> link = p -> link;
    }
    delete p;
    n--;
    return true;
}

template 
bool SingleList :: Update(int i, T x)
{
    if(i < 0 || i > n - 1)
    {
        cout << "Out Of Bound" << endl;
        return false;
    }
    Node *p = first;
    for(int j = 0; j < i; j++)
        p = p -> link;
    p -> element = x;
    return true;
}

template 
void SingleList :: Output(ostream &out) const
{
    Node *p = first;
    while(p)
    {
        out << p -> element << " ";
        p = p -> link;
    }
    out << endl;
}

template 
void SingleList :: Reverse()
{
    Node *pre = first, *cur = first -> link, *next;
    while(cur)
    {
        next = cur -> link;
        cur -> link = pre;
        pre = cur;
        cur = next;
    }
    first -> link = NULL;
    first = pre;
}

template 
bool SingleList :: DeleteX(const T &x)
{
    int re = 0;
    bool ok = false;
    Node *p = first;
    while(p)
    {
        if(p -> element == x)
        {
            re++;
            Delete(Search(x));
            ok = true;
            p  = first;
        }
        p = p -> link;
    }
    p = first;
    if(p -> element == x)
        Delete(Search(x));
    return ok;
}

int main()
{
    int del_data, len ,num;
    SingleList A;
    cout << "Input the length of the list: ";
    cin >> len;
    cout << "\nInput each element: ";
    for(int i = 0; i < len; i++)
    {
        cin >> num;
        A.Insert(i - 1, num);
    }
    cout << "\nInitial list: ";  
    A.Output(cout);
    A.Reverse();
    cout << "\nResevered list: ";  
    A.Output(cout);
    cout << "\nInput the element to be deleted: ";  
    cin >> del_data;
    if(A.DeleteX(del_data) == true)
    {
        cout << "\nList after being deleted: ";  
        A.Output(cout);
    }
    else
        cout << "\nNot found" << endl;  
    return 0;
}


你可能感兴趣的:(南邮课程实验)