1

コードに問題があります。コードでエラーが発生し、Node の __add__() 演算子に「前」が含まれていないことが示されていますが、メイン プログラムではエラーが発生しません

class Node():
    def __init__(self):
        self.next = None
        self.prev = None
        self.data = None

    def getData(self):
        return self.data

class LinkedList():
    def __init__(self):
        self.count = 0
        self.last = Node()
        self.first = Node()
        self.first.next = self.last
        self.last.previous = self.first

    def append(self, data):
        self.last.data = data
        self.last.next = Node()
        tmp = self.last
        self.last = self.last.next
        self.last.previous = tmp
        self.count += 1

    def prepend(self, data):
        self.first.data = data
        self.first.previous = Node()
        tmp = self.first
        self.first = self.first.previous
        self.first.next = tmp
        self.count += 1

    def front(self):
        if self.count == 0: return None
        return self.first.next

    def back(self):
        if self.count == 0: return None
        return self.last.previous

    def size(self):
        return self.count

    def __getitem__(self, node):
        count = self.first
        while count.data:
            if count.data == node:
                return count
            count = count.next
        return None

    def __iter__(self):
        count = self.first.next
        while count.data:
            print("here")
            yield count.data
            count = count.next

class BigInt:
    def __init__(self, initValue = "0"):
        self.data = LinkedList()
        for count in initValue:
            self.data.append(count)
        self.Neg = False

    def __repr__(self):
        integer = ""
        node = self.data.front()
        while node.next:
            integer= integer+(node.getData())
            node = node.next
        return "".join(integer)

    def toString(self):
        return self.__repr__()

    def isNeg(self):
        return self.Neg

    def __add__(self, rhs):
        node1 = self.data.back()
        node2 = rhs.data.back()
        if self.isNeg() and not rhs.isNeg():
            return rhs - self
        elif rhs.isNeg() and not self.isNeg():
            return self - rhs

        summation = LinkedList()
        carryOne = 0
        print(node1.previous.previous.getData())
        while node1.previous.getData() is not None or node2.previous.getData() is not None:
            tot = int(node1.getData())+int(node2.getData())+carryOne
            summation.prepend((tot)%10)
            carryOne = 0
            if tot >= 10: carryOne = 1
            node1 = node1.previous
            node2 = node2.previous
        ret = ""
        for digit in summation:
            ret = ret + digit
            print(digit)
        print (ret)

    def __sub__():
        pass

a = LinkedList()
a.prepend(4)
a.prepend(5)
a.append(23)
print (type(a.back()))
print(a.back().previous.previous.getData())

g = BigInt("2")
h = BigInt("3")
(g+h)
print (g.toString())
4

1 に答える 1

4

previous新しく構築されたメンバーは存在せずNode、のみprevです。

の一部のインスタンスは、Node後でと呼ばれるメンバーを取得しpreviousます。これは次のようなコードによるものです:

self.last.previous = self.first

(それを指摘してくれた@David Robinsonに感謝します。)

于 2012-11-08T07:28:40.453 に答える