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. codefforces 877B

    B. Nikita and stringtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inpu ...

  2. 记一次 lampiao渗透(Drupal+脏牛提权)

    vulnhub|渗透测试lampiao 题记 最近在打靶机,发现了一个挺有意思的靶机,这里想跟大家分享一下. 环境准备 vulnhub最近出的一台靶机 靶机(https://www.vulnhub.c ...

  3. sql-libs(6) 双引号的报错注入

    payload:http://192.168.48.130/sqli-laaess-6/?id=1" and updatexml(1,concat(0x7e,(SELECT schema_n ...

  4. JSON-LD 结构化数据

    JSON-LD 结构化数据 SEO JSON-LD JSON for Linking Data JSON 链接数据 https://json-ld.org/ https://en.wikipedia. ...

  5. Linux bash shell All In One

    Linux bash shell All In One Linux https://tinylab.gitbooks.io/shellbook/content/zh/chapters/01-chapt ...

  6. TypeScript & React & Jest

    TypeScript & React & Jest create-react-app Jest ``tsx import React from 'react'; import { re ...

  7. Web 安全 & cookies & HttpOnly

    Web 安全 & cookies & HttpOnly cookie HttpOnly 禁止 js 读取 cookie 的方法 HttpOnly 实现原理 document.cooki ...

  8. copyright@xgqfrms

    copyright@xgqfrms copyright & seo ## refs *** <div> <a href="https://info.flagcoun ...

  9. Flutter & APP & UI Components

    Flutter & APP & UI Components 下拉刷新或者上拉加载 https://github.com/OpenFlutter/flutter_screenutil h ...

  10. 内存包装类 Memory 和 Span 相关类型

    1. 前言 2. 简介 3. Memory<T>和Span<T>使用准则 3.1. 所有者, 消费者和生命周期管理 3.2. Memory<T> 和所有者/消费者模 ...