【问题标题】:Implementing a queue in LinkedList在 LinkedList 中实现队列
【发布时间】:2015-03-13 20:23:24
【问题描述】:

对于一个家庭作业问题,我的老师希望我在 LinkedList 中实现一个堆栈和一个队列。我使堆栈类没有问题,但是队列部分是我遇到的问题。

这是我的 ListNode 类:

class ListNode(object):

    def __init__(self, item = None, link = None):

        '''creates a ListNode with the specified data value and link
        post: creates a ListNode with the specified data value and link'''

        self.item = item
        self.link = link

这是标记为“Palindrome”的代码,它基本上测试了我的堆栈(已经完成)和我的队列(我遇到了问题)。

from MyQueue import Queue
from MyStack import Stack
import string

#------------------------------------------------------------

def isPalindrome(phrase):
    forward = Queue()
    reverse = Stack()
    extractLetters(phrase, forward, reverse)
    return sameSequence(forward, reverse)

#------------------------------------------------------------

def extractLetters(phrase, q, s):
    for ch in phrase:
        if ch.isalpha():
            ch = ch.lower()
            q.enqueue(ch)
            s.pushItem(ch)

#------------------------------------------------------------

def sameSequence(q, s):
    while q.size() > 0:
        ch1 = q.dequeue()
        ch2 = s.pop()
        if ch1 != ch2:
            return False
    return True


print(isPalindrome('CooperepooC'))

现在这是我的 Queue 类,我知道我需要先弹出添加到列表中的第一件事,但我该怎么做呢?在我的代码中,我正在弹出 self.head ,它是列表中最新添加的(如堆栈),我需要做什么才能从头开始删除? PS。问题出在出队中。

这是我的队列类:

from ListNode import ListNode
#----------------------------------------------------------------------

class Queue:

    #----------------------------------------------------------------------

    def __init__(self):

        self.head = None

    #----------------------------------------------------------------------

    def size(self):

        '''return number of items in the queue

        pre: none

        post: returns number of items in the queue'''

        return self.size

    #----------------------------------------------------------------------

    def enqueue(self, item):

        '''insert x at end of queue

        pre: none

        post: x is added to the queue'''

        tempNode = ListNode(item)
        tempNode.link = self.head

        self.head = tempNode

    #----------------------------------------------------------------------

    def front(self):

        '''return first item in queue

        pre: queue is not empty; IndexError is raised if empty

        post: returns first item in the queue'''

        return self.head[0]

    #----------------------------------------------------------------------

    def dequeue(self):

        '''remove and return first item in queue

        pre: queue is not empty; IndexError is raised if empty

        post: removes and returns first item in the queue'''

        if self.emptyList():
            raise IndexError("The list is empty so we cannot pop from it.")

        else:
            tempNode = self.head(0)
            self.head = self.head.link
            return tempNode

#----------------------------------------------------------------------

    def emptyList(self):

        return self.head == None


#----------------------------------------------------------------------

    def size(self):

        '''post: returns the number of elements in the stack'''

        return len('a')

如果有人能帮我写一个工作队列类,将不胜感激。

【问题讨论】:

标签: python linked-list queue


【解决方案1】:

如果您通过在列表头添加节点来实现入队,则必须通过删除尾部来出队。如果以另一种方式实现入队,出队会更容易。由于这是作业,我只想给出提示。

【讨论】:

  • 所以我必须先做一个尾巴和一个头?
  • 想通了!非常感谢您给我提示,而不是代码!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-03
  • 1970-01-01
  • 2016-12-12
相关资源
最近更新 更多