python-实现链式栈
7 """
8 用一个类来实现一个节点
9 """
10 class Node(object):
11 def __init__(self,data):
12 self.data = data
13 self.next = None
14 class linkstack(object):
15 def __init__(self):
16 self.top = None
17 def isEmpty(self):
18 return self.top == None
19 def clear(self):
20 self.top = None
21 def length(self):
22 i = 0
23 tempnode =self.top
24 while tempnode is not None:
25 tempnode = tempnode.next
26 i += 1
27 return i
28 def push(self,item):
29 node = Node(item)
30 node.next = self.top
31 self.top = node
32 def pop(self):
33 self.top = self.top.next
34 def gettop(self):
35 return self.top.data
36 def display(self):
37 if self.top == None:
38 print("None")
39 tempnode = self.top
40 while tempnode is not None:
41 print(tempnode.data,end = " ")
42 tempnode = tempnode.next
43 print()
44
45 if __name__ == "__main__":
46 linkstack1 = linkstack()
47 linkstack1.push(1)
48 linkstack1.push(2)
49 linkstack1.push(3)
50 linkstack1.push(4)
51 linkstack1.push(5)
52 linkstack1.push(6)
53 linkstack1.display()
54 print(linkstack1.gettop())
55 print(linkstack1.length())
56 linkstack1.pop()
57 linkstack1.display()
58 linkstack1.clear()
59 linkstack1.display()
60 print(linkstack1.isEmpty())
运行结果
6 5 4 3 2 1
6
6
5 4 3 2 1
None
True
代码逻辑较为简单所以代码没有注释,如果有什么地方不明白,欢迎留言!
python-实现链式栈的更多相关文章
- 基于python实现链式栈
""" 链式栈 linkstack.py 思路分析: 1.源于链表结构 2.封装栈的操作方法(入栈,出栈,栈空,栈顶) 3.链表的开头作为栈顶(不用每次遍历,效率高,怎样 ...
- python中文语料分词处理,按字或者词cut_sentence
cut_sentence.py import string import jieba import jieba.posseg as psg import logging #关闭jieba日制 jieb ...
- Python中的多进程与多线程(一)
一.背景 最近在Azkaban的测试工作中,需要在测试环境下模拟线上的调度场景进行稳定性测试.故而重操python旧业,通过python编写脚本来构造类似线上的调度场景.在脚本编写过程中,碰到这样一个 ...
- Python高手之路【六】python基础之字符串格式化
Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...
- Python 小而美的函数
python提供了一些有趣且实用的函数,如any all zip,这些函数能够大幅简化我们得代码,可以更优雅的处理可迭代的对象,同时使用的时候也得注意一些情况 any any(iterable) ...
- JavaScript之父Brendan Eich,Clojure 创建者Rich Hickey,Python创建者Van Rossum等编程大牛对程序员的职业建议
软件开发是现时很火的职业.据美国劳动局发布的一项统计数据显示,从2014年至2024年,美国就业市场对开发人员的需求量将增长17%,而这个增长率比起所有职业的平均需求量高出了7%.很多人年轻人会选择编 ...
- 可爱的豆子——使用Beans思想让Python代码更易维护
title: 可爱的豆子--使用Beans思想让Python代码更易维护 toc: false comments: true date: 2016-06-19 21:43:33 tags: [Pyth ...
- 使用Python保存屏幕截图(不使用PIL)
起因 在极客学院讲授<使用Python编写远程控制程序>的课程中,涉及到查看被控制电脑屏幕截图的功能. 如果使用PIL,这个需求只需要三行代码: from PIL import Image ...
- Python编码记录
字节流和字符串 当使用Python定义一个字符串时,实际会存储一个字节串: "abc"--[97][98][99] python2.x默认会把所有的字符串当做ASCII码来对待,但 ...
- Apache执行Python脚本
由于经常需要到服务器上执行些命令,有些命令懒得敲,就准备写点脚本直接浏览器调用就好了,比如这样: 因为线上有现成的Apache,就直接放它里面了,当然访问安全要设置,我似乎别的随笔里写了安全问题,这里 ...
随机推荐
- 2017CCCC决赛 L1-3. 阅览室
L1-3 阅览室(20 分) 天梯图书阅览室请你编写一个简单的图书借阅统计程序.当读者借书时,管理员输入书号并按下S键,程序开始计时:当读者还书时,管理员输入书号并按下E键,程序结束计时.书号为不超过 ...
- spring-cloud-netflix-hystrix-dashboard
Hystrix-dashboard是一款针对Hystrix进行实时监控的工具,通过Hystrix Dashboard我们可以在直观地看到各Hystrix Command的请求响应时间, 请求成功率等数 ...
- 解决debian (Friendly ARM 嵌入式板)的sudo等一部分命令无法TAB补全
TAB对于比较长的命令在使用时是十分方便的,最近就遇到TAB 键无法补全sudo后跟的命令的情况因此去网上取经.在一篇博客中找到解决问题的方法,觉得大牛们写的太精炼然后自己做如下总结方便自已以后解决类 ...
- JxBrowser: 6.6.1 Crack
JxBrowser: 6.6.1. 1. RELEASE NOTES Download:HomePage JxBrowser is a cross-platform library that prov ...
- vue v-on-clickaway
vue v-on-clickaway Custom directive 自定义指令 https://stackoverflow.com/questions/36170425/detect-click- ...
- GreenSock & SVG Animation
GreenSock & SVG Animation refs https://greensock.com/ https://greensock.com/learning/ GSAP https ...
- TypeScript Learning Paths
TypeScript Learning Paths TypeScript Expert refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以 ...
- anatomy app
anatomy app https://appolicious.com/the-best-iphone-apps-for-anatomy-students/ ios anatomy app Compl ...
- html tag filter in js
html tag filter in js const html = `可当天预订,必须21时15分之前下单,要求必须<font color=green><b>60</b ...
- uniapp设置不同的主题(Theme)
App.vue: <style lang="stylus"> @css { html { --primary: blue; --bg-image: url(https: ...