链接列表中的队列

时间:2015-03-13 01:04:56

标签: python linked-list queue

我正在尝试在我的LinkedList中实现Queues,我已经在一段时间后完成了我的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'的代码,它基本上测试了我的Stack(已经完成)和我的队列(我遇到了麻烦)。

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'))

现在这里是我的队列课程,现在在您询问“尺寸函数中的'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')

如果有人可以帮我写出工作队列课程,我们将不胜感激。

1 个答案:

答案 0 :(得分:0)

你试图调用一个函数'head'但据我所知,head是一些变量,而不是函数。

tempNode = self.head(0)

尝试

tempNode = self.head[0]