我试图从2小时整理我的程序,但每次我都得到同样的错误。
我正在使用python编写Stack, Queue and Linked List
程序。
请帮帮我,我的代码有什么问题吗?
import random
class Node(object):
def __init__(self, other=None):
self.other = other
self.next = next
self.previous = previous
def __str__(self):
return str(self.other)
class List(object):
def __init__(self):
self.head = None
self.tail = None
def append(self, value):
newNode = Node(value)
if self.head == None:
self.head = newNode
self.tail = newNode
else:
self.tail.next = newNode
newNode.previous = self.tail
self.tail = newNode
def __str__(self):
someString = ""
current = self.head
while current != None:
someString = someString + current.other
current = current.next
return someString
class Queue(List):
def enqueue(self, value):
self.append(value)
def dequeue(self):
self.temp = self.head
self.head = self.head.next
return temp
class Stack(List):
def push(self, value):
self.append(value)
def pop(self):
self.temp = self.tail
self.tail = self.tail.previous
return self.temp
def main():
list1 = List()
for i in range(0, 100):
list1.append(str(random.randrange(0, 100)))
print(list1)
queue = Queue()
for i in range(0, 100):
queue.enqueue(str(random.randrange(0, 100)))
for i in range(0, 100):
print(queue.dequeue)
for i in range(0, 10):
print("----------------------------------------")
stack = Stack()
for i in range(0, 100):
stack.push(str(random.randrange(0, 100)))
for i in range(0, 100):
print(stack.pop())
if __name__ == "__main__":
main()
这是我输出的屏幕截图:
截图帮助将是感激的!!
这是我的建议。输出似乎很好。
import random
class Node(object):
def __init__(self, other=None):
self.other = other
self.next = None # next -> None
self.previous = None # previous -> None
def __str__(self):
return str(self.other)
class List(object):
def __init__(self):
self.head = None
self.tail = None
def append(self, value):
newNode = Node(value)
if self.head == None:
self.head = newNode
self.tail = newNode
else:
self.tail.next = newNode
newNode.previous = self.tail
self.tail = newNode
def __str__(self):
someString = ""
current = self.head
while current != None:
someString = someString + current.other
current = current.next
return someString # declease indent (get it out of the while loop)
class Queue(List):
def enqueue(self, value):
self.append(value)
def dequeue(self):
self.temp = self.head
self.head = self.head.next
if self.head is None: # add
self.tail = None # add
else: # add
self.head.previous = None # add (cut the link to the dequeued node)
return self.temp # temp -> self.temp
class Stack(List):
def push(self, value):
self.append(value)
def pop(self):
self.temp = self.tail
self.tail = self.tail.previous
if self.tail is None: # add
self.head = None # add
else: # add
self.tail.next = None # add (cut the link to the popped node)
return self.temp
def main():
list1 = List()
for i in range(0, 100):
list1.append(str(random.randrange(0, 100)))
print(list1)
queue = Queue()
for i in range(0, 100):
queue.enqueue(str(random.randrange(0, 100)))
for i in range(0, 100):
print(queue.dequeue()) # dequeue -> dequeue()
# I have no idea why this for is here.
# for i in range(0, 10):
print("----------------------------------------") # adjust indent
stack = Stack() # adjust indent
for i in range(0, 100):
stack.push(str(random.randrange(0, 100)))
for i in range(0, 100):
print(stack.pop())
if __name__ == "__main__":
main()