0

我究竟做错了什么?这是我正在努力完成的家庭作业。我得到了奇怪的语法错误:

  File "C:\Users\Ranbir\Desktop\myList2.py", line 56, in add
    myList.prev = myList.cursor
AttributeError: 'MyList' object has no attribute 'prev'

我看不出我做错了什么,代码对我来说看起来很完美。prev 在 ListNode 和 mkListNode 中定义。

class EmptyListNode():
    """
    An empty list is an instance of EmptyListNode.
    """
    __slots__ = ()

class ListNode():
    """
    All true value-containing wheel nodes are represented as
    instances of ListNode.
    """
    __slots__ = ('data', 'next', 'prev')

class MyList():
    """
    The container for a linked wheel class.
    It contains a reference to a cursor in the wheel.
    Invariant: size==0 iff type(cursor)==EmptyListNode
    """
    __slots__ = ('cursor', 'size')

def mkList():
    """
    Makes a new list, with size 0, pointing to a Empty Node
    """
    mylist = MyList()
    mylist.cursor = EmptyListNode
    mylist.size = 0
    return mylist

def mkListNode(data, next, prev):
    """
    Make a new list node
    Returns a new node
    """
    listNode = ListNode()
    listNode.data = data
    listNode.next = next
    listNode.prev = prev
    return ListNode

def add(myList, element):
    """
    add: Add a node to the wheel right after the cursor
    Effect: A new node is in the wheel just after the cursor
    """
    myList.size += 1
    if myList.cursor == EmptyListNode:
        myList.cursor = mkListNode(EmptyListNode, element, EmptyListNode)
        myList.cursor.prev = myList.cursor
        myList.cursor.next = myList.cursor
    else:
        temp = mkListNode(myList.cursor.prev, element, myList.cursor.next)
        myList.cursor.prev.next = temp
        myList.cursor.prev = temp

def backward(List):
    """
    advance: Advance the cursor
    Effect: The cursor is now at the next node in the wheel
    """
    myList.cursor = List.cursor.prev

def forward(myList):
    """
    advance: Advance the cursor
    Effect: The cursor is now at the next node in the wheel
    """
    myList.cursor = myList.cursor.next

def getNext(List):
    """
    getNextElement: Get element at node after cursor position
    Effect: The element after cursor position is returned
    """
    return List.cursor.next.data

def getPrev(List):
    """
    getNextElement: Get element at node after cursor position
    Effect: The element after cursor position is returned
    """
    return List.cursor.prev.data

def printList(myList):
    """
    Prints the all the items in the wheel
    """
    string1 = ""
    current = myList.cursor
    for i in range(mylist.size):
        string1 += current.data
        current = current.next

def size(myList):
    """
    size: wheel -> numl
    """
    return mylist.size

def remove(myList):
    """
    remove: Remove the node at the cursor from the wheel
    Effect: The node after the wheel is removed
    Precondition: wheel size > 0
    """
    if size(myList) == 1:
        myList.cursor = EmptyListNode()
    else:
        myList.cursor.next.prev= List.cursor.prev
        myList.cursor.prev.next = List.cursor.next
        myList.cursor.data = List.cursor.prev.data
        myList.cursor = List.cursor.prev
        myList.cursor.prev.next = List.cursor
    myList.size -= 1

有什么帮助吗?谢谢

4

1 回答 1

0
myList.prev = myList.cursor

myList显然是 type MyList,它没有定义prev为插槽。也许你的意思是这个?

myList.cursor.prev = myList.cursor
myList.cursor.next = myList.cursor
于 2013-11-13T06:34:51.543 回答