2013-02-23 7 views
0

그냥 시작하는 것은 숙제이므로 여기에서 힌트를 찾고 있습니다. 저는 파이썬과 프로그래밍에 익숙하지 않습니다. 이중 연결되어있는 커서 기반 목록을 구현해야합니다. 목록에 삽입하는 데 문제가 있습니다. 강사가 Node2Way 클래스에 간단한 Node 클래스를 제공했습니다. 나는 insertafter 속성의 방법을 테스트 할 때Python 3 이중 링크 된 목록에 삽입

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 

, 그것은 첫 번째 항목을 추가하는 작업 :

여기
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 

내가 지금까지 (단지 적절한 방법)이 무엇 : 그는 또한 init 메소드를 제공 ,하지만 두 번째 항목을 추가하려고하면 self._current가 None 유형이고 getNext 메서드를 사용할 수 없다고 표시됩니다. temp를 현재 노드 다음에 참조하도록하는 또 다른 방법이 있는지 나는 모른다. 내가 뭘 잘못하고 있는지, 아니면 내가하고있는 일이 옳은지 모르겠다. 일단 insertAfter 메소드를 얻으면 insertBefore 메소드를 사용할 수 있다고 생각합니다.

힌트를 보내 주시면 감사하겠습니다. 미리 감사드립니다. :) 당신은 temp의 이전 및 다음 노드를 설정하지

if self.isEmpty(): 
     self._header.setNext(temp) 
     self._trailer.setPrevious(temp) 

의 경우

+0

안녕하세요, 실제로 두 항목을'CursorBasedList()'에 삽입 할 때 오류 메시지와 함께 코드를 게시 할 수 있습니까? –

답변

1

에게.

+0

팁 주셔서 감사합니다. 링크 된 목록을 이해하는 데 어려움이 있습니다. : \ – AbigailB