【笨办法学python 进阶版】【ex15_Stack+Queue】笨办法实现--Stack+Queue

一、以前也有不少使用queue的经历,所以这节课还挺顺利,就是stack,我给硬生生,整反了栈顶和栈底,我理解zed的top为第一个入栈的了,后来看了zed的视频给改了,后面就不贴出这个脚本了直接贴出修改后的stack.py。
另外提示一下语法说明,也是作者zed强调的

语法: and  or 
nval = self.next and self.next.value or None 等价于:
if self.next:
    nval = self.next.value
else:
    nval = None
语法: or 
def setting(doit=None):
    should_doit = doit or "NoWay"
    print(should_doit)
等价于:
def setting(doit=None):
    if doit:
        should_doit = doit
    else:
        should_doit = "NoWay"
    print(should_doit)

二、pycharm+python3.8.3

1、简单画一下stack的一个思路:
node.next = self.top
self.top = node
在这里插入图片描述
stack.py脚本如下:主要方法参考单链表处理思路。

class StackNode():
     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)}]"

         return f"[{self.value}, {repr(nval)}]"

class Stack():

    def __init__(self):
        self.top = None

    def push(self, obj):
        node = StackNode(obj, None)
        if self.top == None:
            self.top = node
        else:
            node.next = self.top
            self.top = node

    def count(self):
        node = self.top
        count = 0
        while node:
            node = node.next
            count += 1
        return  count

    def pop(self):
        node = self.top
        if self.top == None:
            return  None
        elif self.count == 1:
            top_value = self.top.value
            self.top = None
            return top_value
        else:
            # top_value = self.top.value
            self.top = self.top.next
            # return top_value
            return node.value

    def first(self):
        if self.top == None:
            return None
        else:
            return self.top.value

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


def test_push():
    colors = Stack()
    colors.push("Pthalo Blue1")
    assert colors.count() == 1
    colors.push("Ultramarine Blue2")
    assert colors.count() == 2
    colors.push("yue3")
    assert colors.count() == 3

def test_pop():
    colors = Stack()
    colors.push("Magenta1")
    colors.push("Alizarin2")
    colors.push("yue3")
    assert colors.pop() == "yue3"
    assert colors.pop() == "Alizarin2"
    assert colors.pop() == "Magenta1"
    assert colors.pop() == None


def test_top():
    colors = Stack()
    colors.push("Cadmium Red Light1")
    assert colors.first() == "Cadmium Red Light1"
    colors.push("Hansa Yellow2")
    assert colors.first() == "Hansa Yellow2"
    colors.push("Pthalo Green3")
    assert colors.first() == "Pthalo Green3"
    colors.dump()

test_push()

test_pop()
test_top()

my_queue.py脚本如下:其中方法参考双链表

class QueueNode():

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

    def __repr__(self):
        nval = self.next and self.next.value or None
        pval = self.prev and self.prev.value or None
        # if self.prev:
        #     pval = self.prev.value
        # else:
        #     pval = None
        # if self.next:
        #     nval = self.next.value
        # else:
        #     nval = None
        return f"[{self.value}, {repr(nval)}, {repr(pval)}]"

class Queue():

    def __init__(self):
        self.begin = None
        self.end = None

    def shift(self, obj):
    ##入队
        node = QueueNode(obj, None, None)
        if self.begin == None and self.end == None:
            self.begin = node
            self.end = node
        else:
            self.end.next = node
            node.prev = self.end
            self.end = node

    def unshift(self):
    ###出队,FIFO
        node = self.begin
        if self.begin == None and self.end == None:
            return None
        elif self.begin == self.end:
            self.begin = None
            self.end = None
            return node.value
        else:
            self.begin = self.begin.next
            self.begin.prev = None
            return node.value

    def count(self):
        node = self.begin
        count = 0
        while node:
            node = node.next
            count += 1
        return count

    def first(self):
        if self.begin == None and self.end == None:
            return None
        else:
            node = self.begin
            return node.value

    def drop(self):
    ###删除最后一个元素
        if self.begin == None and self.end == None:
            return None
        elif self.begin == self.end:
            self.begin = None
            self.end = None
        else:
            self.end = self.end.prev
            self.end.next = None

def test_first():
    colors = Queue()
    colors.shift("Cadmium Red Light")
    assert colors.first() == "Cadmium Red Light"
    colors.shift("Hansa Yellow")
    assert colors.first() == "Cadmium Red Light"
    colors.shift("Pthalo Green")
    assert colors.first() == "Cadmium Red Light"

def test_drop():
    colors = Queue()
    colors.shift("Cad Red1")
    colors.shift("Hansa Yellow2")
    assert colors.count() == 2
    colors.drop()
    assert colors.count() == 1
    assert colors.first() == "Cad Red1"
    colors.drop()
    assert colors.first() == None

def test_unshift():
    colors = Queue()
    colors.shift("Magenta1")
    colors.shift("Alizarin2")
    assert colors.unshift() == "Magenta1"
    assert colors.unshift() == "Alizarin2"
    assert colors.unshift() == None



test_unshift()
test_first()
test_drop()

三、说明
1、《笨办法学python 进阶版》作者链接:https://learncodethehardway.org/more-python-book/
作者github:https://github.com/zedshaw/learn-more-python-the-hard-way-solutions
可以看一下zed的源码以及处理思路。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值