刚开始,这是作业,所以我只是在这里寻找提示。我对Python和编程一般都很陌生。我应该实现双重链接的基于光标的列表。我在插入列表时遇到了一些麻烦。我的教师提供了一个简单的Node类和Node2Way类。他还提供了init方法:Python 3插入双链表
from node import Node
class Node2Way(Node):
def __init__(self,initdata):
Node.__init__(self,initdata)
self.previous = None
def getPrevious(self):
return self.previous
def setPrevious(self,newprevious):
self.previous = newprevious
这里是我迄今为止(只是相关的方法):
from node2way import Node2Way
class CursorBasedList(object):
""" Linked implementation of a positional list."""
def __init__(self):
""" Creates an empty cursor-based list."""
self._header = Node2Way(None)
self._trailer = Node2Way(None)
self._trailer.setPrevious(self._header)
self._header.setNext(self._trailer)
self._current = None
self._size = 0
def insertAfter(self, item):
"""Inserts item after the current item, or
as the only item if the list is empty. The new item is the
current item."""
temp = Node2Way(item)
if self.isEmpty():
self._header.setNext(temp)
self._trailer.setPrevious(temp)
else:
temp.setNext(self._current.getNext())
self._current.setNext(temp)
temp.setPrevious(self._current)
self._current = temp
self._size+=1
当我测试的insertAfter方法了,它适用于添加的第一个项目,但是当我尝试添加第二个项目时,它说self._current是None类型,不能使用getNext方法。我不知道是否有另一种方法可以在当前节点之后获取临时节点的引用。我不确定我做错了什么,或者即使我做的任何事情都是对的。我想一旦我得到了insertAfter方法,我会很好地用insertBefore方法。
任何提示将不胜感激。先谢谢你! :)
嘿,你可以发布代码,你实际上插入到'CursorBasedList()',以及错误消息? – 2013-02-23 23:12:32