异质链表

 

程序中,用基类类型指针,可以生成一个连接不同派生类对象的动态链表,

即每个节点指针可以指向类层次中不同的派生类对象。

这种节点类型不相同的链表称为异质链表。

如:任务管理器,管理不同的进程

 

 

#include "dialog.h"
#include <QApplication>
#include <QLabel>
#include <QPushButton>

class Base
{
public:
    virtual void show() = 0;
    virtual ~Base(){}
};

class Node
{
public:
    Node *next;
    Base *data;
};

class YiZhiLinkList
{
public:
    YiZhiLinkList()
    {
        head = nullptr;
    }
    ~YiZhiLinkList()
    {
        if(head != nullptr) {
            delete head->data;
            head = head->next;
        }
    }

    Node* add(Base *base)
    {
        if (head == nullptr){
            head = new Node();
            head->data = base;
            head->next = nullptr;
        }
        else {
            Node *tmp = head;
            Node *n = new Node();
            n->data = base;
            n->next = nullptr;

            while(tmp->next != nullptr) {
                tmp = tmp->next;
            }

            tmp->next = n;

        }
        return head;
    }
    void show()
    {
        while (head != nullptr) {
            head->data->show();
            head = head->next;
        }
    }

private:
    Node *head;

};

class MyLable:public Base
{
private:
    QLabel label;
public:
    MyLable(){

    }
    ~MyLable(){

    }
    void show()
    {
        label.setText("This is label");
        label.show();
    }
};

class MyButton:public Base
{
private:
    QPushButton button;
public:
    MyButton(){

    }
    ~MyButton(){

    }
    void show()
    {
        button.setText("This is button");
        button.show();
    }
};

class MyDialog:public Base
{
private:
    Dialog w;
public:
    MyDialog(){

    }
    ~MyDialog(){

    }
    void show()
    {
        w.show();
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    Base *l = new MyLable();
    Base *b = new MyButton();
    Base *d = new MyDialog();

    YiZhiLinkList Yi;
    Yi.add(l);
    Yi.add(b);
    Yi.add(d);

    Yi.show();

    return a.exec();
}

 

转载于:https://www.cnblogs.com/xiangtingshen/p/11617574.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中实现异质链表的常用方法是使用多态性。具体而言,我们可以定义一个基类 `Node`,然后派生出不同的子类,每个子类表示不同类型的节点。例如: ```cpp class Node { public: virtual ~Node() {} }; class IntNode : public Node { public: IntNode(int val) : value(val) {} int value; }; class StringNode : public Node { public: StringNode(const std::string& str) : value(str) {} std::string value; }; ``` 然后,我们可以定义一个链表节点 `LinkedListNode`,它包含一个指向基类 `Node` 的指针。这样,我们就可以将不同类型的节点放入链表中。 ```cpp class LinkedListNode { public: LinkedListNode(Node* n) : node(n), next(nullptr) {} Node* node; LinkedListNode* next; }; ``` 对于链表的构造,我们可以定义一个 `LinkedList` 类,并提供相应的操作: ```cpp class LinkedList { public: ~LinkedList() { LinkedListNode* cur = head; while (cur) { LinkedListNode* next = cur->next; delete cur->node; // 释放节点占用的内存 delete cur; cur = next; } } void addNode(Node* n) { LinkedListNode* newNode = new LinkedListNode(n); if (!head) { head = newNode; } else { LinkedListNode* cur = head; while (cur->next) { cur = cur->next; } cur->next = newNode; } } private: LinkedListNode* head = nullptr; }; ``` 这样,我们就可以像下面这样构造异质链表: ```cpp LinkedList list; list.addNode(new IntNode(42)); list.addNode(new StringNode("hello")); ``` 注意,由于基类 `Node` 包含虚析构函数,因此我们可以通过 `delete` 删除派生类对象,从而正确地释放内存。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值