【发布时间】: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