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-实现链式栈的更多相关文章

  1. 基于python实现链式栈

    """ 链式栈 linkstack.py 思路分析: 1.源于链表结构 2.封装栈的操作方法(入栈,出栈,栈空,栈顶) 3.链表的开头作为栈顶(不用每次遍历,效率高,怎样 ...

  2. python中文语料分词处理,按字或者词cut_sentence

    cut_sentence.py import string import jieba import jieba.posseg as psg import logging #关闭jieba日制 jieb ...

  3. Python中的多进程与多线程(一)

    一.背景 最近在Azkaban的测试工作中,需要在测试环境下模拟线上的调度场景进行稳定性测试.故而重操python旧业,通过python编写脚本来构造类似线上的调度场景.在脚本编写过程中,碰到这样一个 ...

  4. Python高手之路【六】python基础之字符串格式化

    Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...

  5. Python 小而美的函数

    python提供了一些有趣且实用的函数,如any all zip,这些函数能够大幅简化我们得代码,可以更优雅的处理可迭代的对象,同时使用的时候也得注意一些情况   any any(iterable) ...

  6. JavaScript之父Brendan Eich,Clojure 创建者Rich Hickey,Python创建者Van Rossum等编程大牛对程序员的职业建议

    软件开发是现时很火的职业.据美国劳动局发布的一项统计数据显示,从2014年至2024年,美国就业市场对开发人员的需求量将增长17%,而这个增长率比起所有职业的平均需求量高出了7%.很多人年轻人会选择编 ...

  7. 可爱的豆子——使用Beans思想让Python代码更易维护

    title: 可爱的豆子--使用Beans思想让Python代码更易维护 toc: false comments: true date: 2016-06-19 21:43:33 tags: [Pyth ...

  8. 使用Python保存屏幕截图(不使用PIL)

    起因 在极客学院讲授<使用Python编写远程控制程序>的课程中,涉及到查看被控制电脑屏幕截图的功能. 如果使用PIL,这个需求只需要三行代码: from PIL import Image ...

  9. Python编码记录

    字节流和字符串 当使用Python定义一个字符串时,实际会存储一个字节串: "abc"--[97][98][99] python2.x默认会把所有的字符串当做ASCII码来对待,但 ...

  10. Apache执行Python脚本

    由于经常需要到服务器上执行些命令,有些命令懒得敲,就准备写点脚本直接浏览器调用就好了,比如这样: 因为线上有现成的Apache,就直接放它里面了,当然访问安全要设置,我似乎别的随笔里写了安全问题,这里 ...

随机推荐

  1. 前端接收后端文件流导出excel文档遇到的问题

    先上代码: Vue.prototype.download = function(oUrl, filename) { this.axios .get(oUrl, { responseType: 'arr ...

  2. github gist 无法访问

    转自这里 以管理员身份在hosts文件: Windows: C:\Windows\System32\drivers\etc Ubuntu: /etc/hosts 添加: 192.30.253.118 ...

  3. Docker in Action All In One

    Docker in Action All In One Docker https://www.docker.com/play-with-docker https://hub.docker.com/ $ ...

  4. CSS 检测 IE 浏览器

    CSS 检测 IE 浏览器 <!--[if IE]> <link href="ie.css" rel="stylesheet"> < ...

  5. 读写 LED 作业 台灯的 频闪研究1

    读写 LED 作业 台灯的 频闪研究: 核心提示: 随着科技的持续发展,目前已经商业化的照明产品从第一代的白炽灯: 第二代的荧光灯.卤灯: 第三代的高强度气体放电灯; 以及当下主流的, 第四代的发光二 ...

  6. 如何禁用 Chrome Taps Group feature &#128169;

    如何禁用 Chrome Taps Group feature bug https://support.google.com/chrome/go/feedback_confirmation How to ...

  7. zsh all in one

    zsh all in one zsh https://ohmyz.sh/ # install $ sh -c "$(curl -fsSL https://raw.github.com/ohm ...

  8. 运行Chrome浏览器如何添加Options

    原文档 Options Windows: 退出Chrome的所有正在运行的实例. 右键单击"Chrome"快捷方式 选择属性 在"目标:"行的末尾添加命令行标志 ...

  9. Python 与 excel的简单应用

    1.pip openpyxl库: pip install openpyxl -i http://pypi.douban.com/simple --trust-host pypi.douban.com ...

  10. Mybites学习

    参考链接:https://www.cnblogs.com/dongying/p/4073259.html <select <!-- 1. id (必须配置) id是命名空间中的唯一标识符, ...