博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
异质链表
阅读量:4646 次
发布时间:2019-06-09

本文共 1943 字,大约阅读时间需要 6 分钟。

 

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

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

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

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

 

 

#include "dialog.h"#include 
#include
#include
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

你可能感兴趣的文章
springmvc3.2+spring+hibernate4全注解方式整合(一)
查看>>
Elgg网站迁移指南
查看>>
素数筛法优化
查看>>
installshield 注册dll
查看>>
Sublime Text 3 及Package Control 安装(附上一个3103可用的Key)
查看>>
LTE QCI分类 QoS
查看>>
Get MAC address using POSIX APIs
查看>>
bzoj2120
查看>>
基于uFUN开发板的心率计(一)DMA方式获取传感器数据
查看>>
【dp】船
查看>>
oracle, group by, having, where
查看>>
⑥python模块初识、pyc和PyCodeObject
查看>>
object-c中管理文件和目录:NSFileManager使用方法
查看>>
Kibana:分析及可视化日志文件
查看>>
nodejs pm2使用
查看>>
cocos2d-x 3.10 PageView BUG
查看>>
装饰器的基本使用:用户登录
查看>>
CSS选择器总结
查看>>
mysql中sql语句
查看>>
head/tail实现
查看>>