【问题标题】:Queues in a Linked List链表中的队列
【发布时间】:2015-03-13 01:04:55
【问题描述】:

我正在尝试在我的 LinkedList 中实现队列,一段时间后我已经完成了我的 Stack 实现现在每当我尝试测试我的队列时都会收到此错误。

C:\Python33\python.exe "C:/Users/koopt_000/Desktop/College/Sophomore Semester 2/Computer Science 231/Chapter5/palindrome.py"
Traceback (most recent call last):
  File "C:/Users/koopt_000/Desktop/College/Sophomore Semester 2/Computer Science 231/Chapter5/palindrome.py", line 33, in <module>
    isPalindrome('a')
  File "C:/Users/koopt_000/Desktop/College/Sophomore Semester 2/Computer Science 231/Chapter5/palindrome.py", line 12, in isPalindrome
    return sameSequence(forward, reverse)
  File "C:/Users/koopt_000/Desktop/College/Sophomore Semester 2/Computer Science 231/Chapter5/palindrome.py", line 27, in sameSequence
    ch1 = q.dequeue()
  File "C:\Users\koopt_000\Desktop\College\Sophomore Semester 2\Computer Science 231\Chapter5\MyQueue.py", line 65, in dequeue
    tempNode = self.head(0)
TypeError: 'ListNode' object is not callable

Process finished with exit code 1

这是我的 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 类,现在在您询问“大小函数中的 'a' 是什么?”之前我真的不知道该放什么。无论如何,我尝试将我在堆栈类中所做的大部分工作都实现到这个中,但它不起作用。谁能帮我重新安排我的队列类以使其正常工作?

这是我的队列类:

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】:

    您正在尝试调用函数“head”,但据我所知,head 是一些变量,而不是函数。

    tempNode = self.head(0)
    

    试试

    tempNode = self.head[0]
    

    【讨论】:

    • 当我尝试这个时它指出:类型错误:'ListNode'对象不支持索引
    • 那么你的 ListNode 是什么?
    • 我刚刚添加了,我知道我忘了添加一些东西
    • 所以self.head要么是None,要么是一个ListNode,也就是说它只有itemnext。然后省略任何提及 [0] 或 (0)。
    • 哦,你可能想return tempNode.item而不是tempNode
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-04
    • 2018-03-17
    • 2018-05-17
    • 2016-02-16
    • 1970-01-01
    • 2018-03-16
    • 2012-04-05
    相关资源
    最近更新 更多