笨办法学python3续 learn more python3 in hard way ex15-3 stack栈

代码:

class StackNode(object):

    def __init__(self, value, nxt):
        self.value = value
        self.next = nxt

    def __repr__(self):
        nval = self.next and self.next.value or None
        return f"[{self.value}:{repr(nval)}]"


class Stack(object):
    def __init__(self):
        self.top=None

    def push(self,obj):
        """push a new value to the top of the Stack """

        # if self.top !=None:
        #     node=StackNode(obj,self.top)
        #     self.top=node
        # else:
        #     node=StackNode(obj,None)
        #     self.top=node
        ##简便成
        self.top=StackNode(obj,self.top)


    def pop(self):
        """pops the value that is currently on the top of the stack """
        if self.top:
            node=self.top
            self.top=node.next
            return node.value
        else:
            return None

    # def top(self):#重名报错
    #     """returns a  reference to the first item,does not remove """
    #     return self.top!=None and self.top.value or None
    def first(self):
        """Returns a *reference* to the first item, does not remove."""
        return self.top != None and self.top.value or None

    def count(self):
        """count the numbers of elements in the stack """
        count=0
        node=self.top
        while node:
            count +=1
            node=node.next
        return count


    def dump(self,mark="------"):
        print(mark,end=" ")
        node=self.top
        while node:
            print(node,end=" ")
            node=node.next
        print()

colors = Stack()
colors.push("Cadmium Red Light")
print(colors.first())
colors.push("Hansa Yellow")
print(colors.dump("test"))
print(colors.first())
colors.push("Pthalo Green")
print(colors.first())

自动化测试:

from Stack import *

def test_push():
    colors = Stack()
    colors.push("Pthalo Blue")
    assert colors.count() == 1
    colors.push("Ultramarine Blue")
    assert colors.count() == 2

def test_pop():
    colors = Stack()
    colors.push("Magenta")
    colors.push("Alizarin")
    assert colors.pop() == "Alizarin"
    assert colors.pop() == "Magenta"
    assert colors.pop() == None

def test_top():
    colors = Stack()
    colors.push("Cadmium Red Light")
    assert colors.first() == "Cadmium Red Light"
    colors.push("Hansa Yellow")
    assert colors.first() == "Hansa Yellow"
    colors.push("Pthalo Green")
    assert colors.first() == "Pthalo Green"

代码中两处注释:
1.原来是这样想的 栈和单链表很像 按照单链表的原理,这样打的
zed修改过的是个递归 重复操作top.next=next
2.显示栈顶元素 的方法名叫top()原来
self.top 会有bug 换了个名字

原理:
在这里插入图片描述
在这里插入图片描述

出栈:
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值