1. 程式人生 > >笨辦法學python3續 learn more python 3 in hard way ex15-2 queue in smart way

笨辦法學python3續 learn more python 3 in hard way ex15-2 queue in smart way

程式碼:

from DoubleLinkedListNode import *
from dllist import *
class Queue(object):

    def __init__(self):
        self.list = DoubleLinkedList()

    def shift(self, obj):
        """Shifts a new element onto the back of the queue."""
        self.list.shift(obj)

    def unshift(self):
        """Removes the element that is first in the queue."""
self.list.unshift() def dump(self,mark="=----="): self.list.dump()

基本上這佇列和雙重連結串列是一樣的 出佇列是先進先出 呼叫unshift方法

這裡有個問題是我把剛剛的那個自動測試搞過來 unshift() 的assert就一直報錯 在這裡插入圖片描述 然後我嘗試把自動化測試雙向連結串列 = -= 沒有問題 反正就很奇怪 最後我直接把測試寫在同一個檔案裡

from DoubleLinkedListNode import *
from dllist import *
class Queue(object
): def __init__(self): self.list = DoubleLinkedList() def shift(self, obj): """Shifts a new element onto the back of the queue.""" self.list.shift(obj) def unshift(self): """Removes the element that is first in the queue.""" self.list.unshift() def
dump(self,mark="=----="): self.list.dump() colors = DoubleLinkedList() colors.shift("Viridian") colors.shift("Sap Green") colors.shift("Van Dyke") print(colors.dump("all")) print( colors.unshift()) print( colors.unshift()) print( colors.unshift()) print( colors.unshift())

在這裡插入圖片描述 沒毛病= ----------------= 奇了怪了