【问题标题】:How do I dequeue using popLeft in a queue properly?如何在队列中正确使用 popLeft 出列?
【发布时间】:2018-06-29 11:27:54
【问题描述】:
import collections
class MyStack:
    def __init__(self):
        self._data = []

    def push(self, value):
        self._data.append(value)

    def size(self):
        #Return the number of elements in the stack
        return len(self._data)

    def toString(self):
        #Return a string representing the content of this stack
        return str(self._data)

class MyQueue:
    def __init__(self):
        self._data = collections.deque([])

    def enqueue(self, value):
        self._data.append(value)

    def dequeue(self):
        return self._data.popleft()

    def size(self):
        #return the number of elements in the queue
        return len(self._data)

queue1 = MyQueue()
dq = MyStack()
stack1 = MyStack()

stack1.push(['stone', 'atone'])
print "size of stack is now :" ,stack1.size()
queue1.enqueue(stack1)
print "size of queue is now :", queue1.size()
print "size of stack is now :" ,stack1.size()
stack1.push(['stone', 'shone'])
stack1.push(['stone', 'scone'])

dq = queue1.dequeue() # i would like dq to be ['stone','atone']

print dq.toString()

这是一个学校作业,我不能修改函数 enqueue 和 dequeue 以及 def__init__(self) 的代码。 我正在尝试使用 popleft 作为将项目从队列中出列的一种方式。但是,当我使用 popleft 时,编译器会返回整个队列,而不仅仅是队列的第一个堆栈。话虽如此,队列的大小减一。对此有何解释?

当我只希望它返回 ['stone', ' 时,编译器返回 [['stone', 'atone'], ['stone', 'shone'], ['stone', 'scone']]赎罪']。

【问题讨论】:

  • 您将 Stack 对象 添加到队列中,而不是 in 堆栈中的元素。这就是为什么当您.dequeue() 时,您会返回 Stack 对象,该对象现在包含 [['stone', 'atone'], ['stone', 'shone'], ['stone', 'scone']](您将其 .pushed 到堆栈中)。我不确定我是否完全理解您的问题,但这解释了您所看到的行为。
  • 换句话说,我认为您需要在这里详细说明一下。我怀疑你误解了你的任务的第一部分,你的任务是使用堆栈来填充队列吗?
  • 我实际上应该按照数据结构和算法实现的列表创建一个字梯游戏。我只是从我的作业中提取了一些代码来问这个问题,否则这个问题将包含太多代码。无论如何,我只是按照给我的算法。现在,我需要从队列中取出第一个项目(这是一个堆栈)并能够打印出这个第一个项目(这是一个堆栈)。

标签: python algorithm stack queue logic


【解决方案1】:

就像上面的注释一样,您传递的是Stack 对象而不是堆栈中的元素。试试这个

import collections
class MyStack:
    def __init__(self):
        self._data = []

    def push(self, value):
        self._data.append(value)

    def size(self):
        #Return the number of elements in the stack
        return len(self._data)

    def toString(self):
        #Return a string representing the content of this stack
        return str(self._data)

class MyQueue:
    def __init__(self):
        self._data = collections.deque([])

    def enqueue(self, value):
        self._data.append(value)

    def dequeue(self):
        return self._data.popleft()

    def size(self):
        #return the number of elements in the queue
        return len(self._data)

queue1 = MyQueue()
dq = MyStack()
stack1 = MyStack()

stack1.push(['stone', 'atone'])
print "size of stack is now :" ,stack1.size()
queue1.enqueue(stack1.toString())
print "size of queue is now :", queue1.size()
print "size of stack is now :" ,stack1.size()
stack1.push(['stone', 'shone'])
stack1.push(['stone', 'scone'])

dq = queue1.dequeue() # i would like dq to be ['stone','atone']

print dq

【讨论】:

    猜你喜欢
    • 2012-03-20
    • 1970-01-01
    • 1970-01-01
    • 2013-02-15
    • 1970-01-01
    • 2016-12-16
    • 2015-07-30
    • 1970-01-01
    • 2011-07-21
    相关资源
    最近更新 更多