无锡住房和城乡建设局网站,网站换模板,seo公司上海牛巨微,改版目录 0. 前言1. 了解堆栈2.代码实现3. 演示效果其他PyQt5文章 0. 前言
本文使用 PyQt5制作图形化界面演示数据结构中的堆栈操作
操作系统#xff1a;Windows10 专业版
开发环境#xff1a;Pycahrm Comunity 2022.3
Python解释器版本#xff1a;Python3.8
第三方库… 目录 0. 前言1. 了解堆栈2.代码实现3. 演示效果其他PyQt5文章 0. 前言
本文使用 PyQt5制作图形化界面演示数据结构中的堆栈操作
操作系统Windows10 专业版
开发环境Pycahrm Comunity 2022.3
Python解释器版本Python3.8
第三方库PyQt5
1. 了解堆栈
在计算机科学中堆栈Stack也常被称为栈是一种抽象的数据结构它是一种只能从一个端添加元素和删除元素的线性数据结构。这一端被称为“栈顶”相对地把另一端称为“栈底”。根据这个定义可以推断出后进先出LIFOLast In First Out这个特性。
堆栈有以下几个基本操作
push: 将一个元素添加到栈顶pop: 从栈顶移除一个元素并返回这个元素的值peek/top: 返回栈顶的元素值但不将其移除is_empty: 返回栈是否为空
堆栈通常使用数组或链表实现。如果使用数组实现需要考虑动态扩容的情况。如果使用链表实现需要注意在链表头进行操作否则操作的时间复杂度将变为O(n)。
堆栈通常用作典型的临时存储例如在递归函数中存储函数的返回地址或者是需要逆序输出元素的应用程序那么在这样的应用中堆栈的弹出顺序就可以实现逆序的要求。
例如 假设你要洗堆满了碗你会把碗一层一层地往上堆放。当你洗完一个碗放入放碗柜时你把刚才放在最上层的碗取下来这就是一个栈的操作过程。
当你需要使用碗时你从栈顶拿下一个碗。如果你拿下的碗与你想要使用的碗不一样你就把拿下的碗再放回去这样使用的就是最后一个放上去的碗。这个过程叫做“后进先出”或者LIFOLast In, First Out
2.代码实现
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sysclass Node:def __init__(self, data):self.data dataself.next Noneclass Queue:def __init__(self):self.head Noneself.tail Nonedef enqueue(self, data):new_node Node(data)if self.tail is None:self.head new_nodeself.tail new_nodeelse:self.tail.next new_nodeself.tail new_nodedef dequeue(self):if self.head is not None:data self.head.dataself.head self.head.nextif self.head is None:self.tail Nonereturn dataelse:return Nonedef front(self):if self.head is not None:return self.head.dataelse:return Nonedef is_empty(self):return self.head is Nonedef display(self, scene):pen QPen(QColor(0, 255, 0))font QFont(Arial, 10)y 100current_node self.headwhile current_node is not None:# Draw node rectanglescene.addRect(50, y, 50, 50, pen)# Draw node texttext scene.addText(str(current_node.data), font)text.setDefaultTextColor(QColor(255, 255, 255))text.setPos(70, y 10)current_node current_node.nexty 70class MainWindow(QMainWindow):def __init__(self):super().__init__()self.setWindowTitle(Queue Demo)self.setFixedSize(500, 500)self.scene QGraphicsScene(self)self.view QGraphicsView(self.scene, self)self.view.setGeometry(0, 0, 500, 500)self.queue Queue()self.queue.enqueue(10)self.queue.enqueue(20)self.queue.enqueue(30)self.queue.display(self.scene)# Add UI elementsenqueue_button QPushButton(Enqueue, self)enqueue_button.move(10, 10)enqueue_button.clicked.connect(self.handle_enqueue)dequeue_button QPushButton(Dequeue, self)dequeue_button.move(10, 40)dequeue_button.clicked.connect(self.handle_dequeue)front_button QPushButton(Front, self)front_button.move(10, 70)front_button.clicked.connect(self.handle_front)clear_button QPushButton(Clear, self)clear_button.move(10, 100)clear_button.clicked.connect(self.handle_clear)self.data_edit QLineEdit(self)self.data_edit.move(100, 10)def handle_enqueue(self):data self.data_edit.text()if data ! :self.queue.enqueue(data)self.scene.clear()self.queue.display(self.scene)def handle_dequeue(self):data self.queue.dequeue()if data is not None:QMessageBox.information(self, Dequeue, Dequeued value: str(data))self.scene.clear()self.queue.display(self.scene)else:QMessageBox.warning(self, Dequeue, Queue is empty)def handle_front(self):data self.queue.front()if data is not None:QMessageBox.information(self, Front, Front value: str(data))else:QMessageBox.warning(self, Front, Queue is empty)def handle_clear(self):self.queue Queue()self.scene.clear()if __name__ __main__:app QApplication(sys.argv)window MainWindow()window.show()sys.exit(app.exec_())3. 演示效果
提供了四个操作
入栈出栈栈顶元素查询堆栈初始化清空
运行代码可以看到堆栈中是有三个元素(绿色框框)在其中这是我们在代码中提前预设的三个值 现在输入13然后将它Push到栈中可以看到 多了一个元素
然后取其Peek顶端的值看看是否如愿 如我们压入栈中的一样就是13但是并不会将它从栈中取出
现在我们将其Pop看看会是怎样的情形 执行完Pop操作后将栈顶元素13取出并且栈中也少了一个元素
其他PyQt5文章
基于PyQt5的图形化界面开发——自制MQTT客户端
基于PyQt5的图形化界面开发——Windows内存资源监视助手[附带编译exe教程]
基于PyQt5的图形化界面开发——模拟医院管理系统
基于PyQt5的图形化界面开发——自制ssh工具
基于PyQt5的图形化界面开发——PyQt示例_计算器
基于PyQt5的图形化界面开发——PyQt示例_扫雷
基于PyQt5的图形化界面开发——自制Redis图形化客户端(文末附源码)
基于PyQt5的图形化界面开发——堆栈动画演示
基于PyQt5的图形化界面开发——队列动画演示 文章转载自: http://www.morning.pigcamp.com.gov.cn.pigcamp.com http://www.morning.jxscp.cn.gov.cn.jxscp.cn http://www.morning.kpbq.cn.gov.cn.kpbq.cn http://www.morning.nhdw.cn.gov.cn.nhdw.cn http://www.morning.bwznl.cn.gov.cn.bwznl.cn http://www.morning.haibuli.com.gov.cn.haibuli.com http://www.morning.ywrt.cn.gov.cn.ywrt.cn http://www.morning.fkmrj.cn.gov.cn.fkmrj.cn http://www.morning.gynls.cn.gov.cn.gynls.cn http://www.morning.fflnw.cn.gov.cn.fflnw.cn http://www.morning.llllcc.com.gov.cn.llllcc.com http://www.morning.zxqqx.cn.gov.cn.zxqqx.cn http://www.morning.dcdhj.cn.gov.cn.dcdhj.cn http://www.morning.phxns.cn.gov.cn.phxns.cn http://www.morning.mstrb.cn.gov.cn.mstrb.cn http://www.morning.lwgsk.cn.gov.cn.lwgsk.cn http://www.morning.tntgc.cn.gov.cn.tntgc.cn http://www.morning.mmhaoma.com.gov.cn.mmhaoma.com http://www.morning.slfmp.cn.gov.cn.slfmp.cn http://www.morning.jcxzq.cn.gov.cn.jcxzq.cn http://www.morning.xkgyh.cn.gov.cn.xkgyh.cn http://www.morning.btwrj.cn.gov.cn.btwrj.cn http://www.morning.deanzhu.com.gov.cn.deanzhu.com http://www.morning.kpxnz.cn.gov.cn.kpxnz.cn http://www.morning.rnnwd.cn.gov.cn.rnnwd.cn http://www.morning.rtsx.cn.gov.cn.rtsx.cn http://www.morning.dfckx.cn.gov.cn.dfckx.cn http://www.morning.frxsl.cn.gov.cn.frxsl.cn http://www.morning.qmpbs.cn.gov.cn.qmpbs.cn http://www.morning.lqjpb.cn.gov.cn.lqjpb.cn http://www.morning.mdrnn.cn.gov.cn.mdrnn.cn http://www.morning.xbdrc.cn.gov.cn.xbdrc.cn http://www.morning.lstmg.cn.gov.cn.lstmg.cn http://www.morning.plqsc.cn.gov.cn.plqsc.cn http://www.morning.bgrsr.cn.gov.cn.bgrsr.cn http://www.morning.frfnb.cn.gov.cn.frfnb.cn http://www.morning.smspc.cn.gov.cn.smspc.cn http://www.morning.jphxt.cn.gov.cn.jphxt.cn http://www.morning.xkyst.cn.gov.cn.xkyst.cn http://www.morning.htpjl.cn.gov.cn.htpjl.cn http://www.morning.ftlgy.cn.gov.cn.ftlgy.cn http://www.morning.xlclj.cn.gov.cn.xlclj.cn http://www.morning.gnbfj.cn.gov.cn.gnbfj.cn http://www.morning.xgkxy.cn.gov.cn.xgkxy.cn http://www.morning.zsyrk.cn.gov.cn.zsyrk.cn http://www.morning.rcjwl.cn.gov.cn.rcjwl.cn http://www.morning.ngdkn.cn.gov.cn.ngdkn.cn http://www.morning.ynjhk.cn.gov.cn.ynjhk.cn http://www.morning.jqrhz.cn.gov.cn.jqrhz.cn http://www.morning.guangda11.cn.gov.cn.guangda11.cn http://www.morning.nknt.cn.gov.cn.nknt.cn http://www.morning.rfycj.cn.gov.cn.rfycj.cn http://www.morning.hrrmb.cn.gov.cn.hrrmb.cn http://www.morning.yjmns.cn.gov.cn.yjmns.cn http://www.morning.xrsqb.cn.gov.cn.xrsqb.cn http://www.morning.nmngg.cn.gov.cn.nmngg.cn http://www.morning.swlwf.cn.gov.cn.swlwf.cn http://www.morning.cywf.cn.gov.cn.cywf.cn http://www.morning.lqqqh.cn.gov.cn.lqqqh.cn http://www.morning.rhkq.cn.gov.cn.rhkq.cn http://www.morning.xqzrg.cn.gov.cn.xqzrg.cn http://www.morning.jgzmr.cn.gov.cn.jgzmr.cn http://www.morning.jfwrf.cn.gov.cn.jfwrf.cn http://www.morning.ljcjc.cn.gov.cn.ljcjc.cn http://www.morning.huayaosteel.cn.gov.cn.huayaosteel.cn http://www.morning.zwzlf.cn.gov.cn.zwzlf.cn http://www.morning.wmglg.cn.gov.cn.wmglg.cn http://www.morning.xymkm.cn.gov.cn.xymkm.cn http://www.morning.sfhjx.cn.gov.cn.sfhjx.cn http://www.morning.tpnch.cn.gov.cn.tpnch.cn http://www.morning.jtjmz.cn.gov.cn.jtjmz.cn http://www.morning.ydhmt.cn.gov.cn.ydhmt.cn http://www.morning.jkrrg.cn.gov.cn.jkrrg.cn http://www.morning.yzdth.cn.gov.cn.yzdth.cn http://www.morning.qfmcm.cn.gov.cn.qfmcm.cn http://www.morning.thmlt.cn.gov.cn.thmlt.cn http://www.morning.ztmkg.cn.gov.cn.ztmkg.cn http://www.morning.kcrw.cn.gov.cn.kcrw.cn http://www.morning.fqmcc.cn.gov.cn.fqmcc.cn http://www.morning.rgwz.cn.gov.cn.rgwz.cn