4.使用多态机制实现通用链表

.

//使用多态实现统一的链表
//链表的释放


#include 
#include 
#include 
using namespace std;

class Object
{
public:
    Object()
    {}
    virtual ~Object()
    {}
public:
    virtual void Print()const = 0;//接口
    virtual bool Compare(Object* pb)const = 0;


};



class List;
class ListNode
{
    friend class List;
public:
    ListNode()
    {
        data = NULL;
        next = NULL;
    }
    ListNode(Object *pobj)
    {
        data = pobj;
        next = NULL;
    }
    ~ListNode()
    {
        delete data;
    }
private:
    Object *data;
    ListNode *next;
};

class List
{
public:
    List()
    {
        Head = Tail = new ListNode;//带头结点的链表
    }
    ~List()
    {
        ListNode *p = Head->next;
        while(p != NULL)
        {
            Head->next = p->next;
            delete p;
            p = Head->next;
        }
        delete Head;
        Head = Tail =NULL;
    }
public:
    void Push_back(Object *p)//尾插
    {
        ListNode *s = new ListNode(p);
        assert(s != NULL);
        Tail->next = s;//?
        Tail = s;
    }
    void PrintList()const
    {
        ListNode *p = Head->next;
        while( p != NULL)
        {
            p->data->Print();//多态
            p = p->next;
        }
        cout<<"Nul."<next;
        while(p!=NULL && p->data->Compare(s->data))
        {
            q = p;
            p = p->next;
        }
        s->next = p;
        q->next = s;
        if(p == NULL)
            Tail = s;
    }

private:
    ListNode *Head;
    ListNode *Tail;
};


class IntObject:public Object
{
public:
    IntObject(int d = 0):data(d)
    {

    }
    ~IntObject()
    {

    }
public:
    virtual void Print()const
    {
        cout<";
    }
    virtual bool Compare(Object* pb)const
    {

        IntObject *pi = dynamic_cast(pb);
        assert(pi != NULL);
        return data < pi->data;
    }

private:
    int data;
};

class StringObject:public Object
{
public:
    StringObject(const char *str)
    {
        if(str == NULL)
        {
            data = new char[1];
            data[0] = '\0';
        }
        else
        {
            data = new char[strlen(str)+1];
            strcpy(data,str);
        }
    }

    ~StringObject()
    {
        delete []data;
        data = NULL;
    }

public:
    virtual void Print() const
    {
        cout<<"\""<";
    }

    virtual bool Compare(Object* pb)const
    {

        StringObject *ps = dynamic_cast(pb);
        assert(ps != NULL);
        int result;
        result = strcmp(data,ps->data);
        if(result >=0)
            return false;
        return true;
    }

private:
    char *data;

};




int main()
{
/*
    List mylist;
    for(int i=1;i<=5;++i)
    {
        IntObject *pi = new IntObject(i);
        mylist.Push_back(pi);
    }
    mylist.PrintList();
*/
/*
    List youlist;
    char *str[] = {"xyz","abcd","ffffff","udiaa","ddddddd"};
    for(int j = 0;j<5;++j)
    {
        StringObejct *ps = new StringObejct(str[j]);
        youlist.Push_back(ps);
    }
    youlist.PrintList();
*/

    List mylist;
    IntObject *pi = new IntObject(3);
    mylist.InsertOrder(pi);
    pi = new IntObject(2);
    mylist.InsertOrder(pi);
    pi = new IntObject(5);
    mylist.InsertOrder(pi);
    pi = new IntObject(4);
    mylist.InsertOrder(pi);
    pi = new IntObject(1);
    mylist.InsertOrder(pi);
    mylist.PrintList();


/*
    List youlist;
    char *str[] = {"xyz","abcd","ffffff","udiaa","ddddddd"};
    for(int j = 0;j<5;++j)
    {
        StringObject *ps = new StringObject(str[j]);
        youlist.InsertOrder(ps);
    }
    youlist.PrintList();
*/
    return 0;
}

 

你可能感兴趣的:(未分类,C++语言)