簡體   English   中英

如何創建一個可以使用兩個鏈表的 function?

[英]How to create a function where I can use two linked lists?

class Node:
    def __init__(self, data = None):
        self.data = data
        self.ref  = None

class LinkedList:
    def __init__ (self):
        self.head = None

    def add_beginning(self, data):
        newNode = Node(data)

        if self.head is None:
            self.head= newNode
        else:
            n = self.head
            while n.ref:
                n = n.ref
            n.ref = newNode

    def print(self):
        if self.head == None:
            print(None)
        else:
            n = self.head
            while n:
                print(n.data,"-->", end= " ")
                n =n.ref


if __name__ == "__main__":
    LL = LinkedList()

    LL.add_beginning(3)
    LL.add_beginning(2)
    LL.add_beginning(1)
    LL.add_beginning(3)
    LL.print()

    KK = LinkedList()
    KK.add_beginning(2)
    KK.add_beginning(6)
    KK.add_beginning(5)
else:
    print("error")

我不知道如何創建def ,我可以在其中使用兩個鏈表(例如 go)並比較每個元素? 我創建了兩個對象(即) KKLL ,我可以以某種方式使用它們嗎?

你的意思是這樣的嗎?

LL1 = LinkedList()
# add some items to LL1

LL2 = LinkedList()
# add some items to LL2

item1 = LL1.head
item2 = LL2.head

count = 1
while item1 and item2:
    print("item", count)
    print("LL1", item1.data)
    print("LL2", item2.data)
    item1 = item1.ref
    item2 = item2.ref
    count += 1

我認為用__eq__方法覆蓋比較運算符是您的最佳答案。 然后,您將同時遍歷兩個鏈接列表,直到結束。 如果兩個節點有不同的數據,則 function 停止並返回 False。 如果未找到不同的節點,則 function 返回 True。

class Node:
    def __init__(self, data = None):
        self.data = data
        self.ref  = None
        

class LinkedList:
    def __init__ (self):
        self.head = None

    # add_beginning method here
    # print method here

    def __eq__(self, other):  # <-- this method
        curr1, curr2 = self.head, other.head
        while curr1 != None or curr2 != None:
            if (curr1 == None) != (curr2 == None):  # check if a list has nodes left but not the other
                return False
            if curr1.data != curr2.data:  # check if both nodes have same data
                return False
            # iterate to next node
            curr1 = curr1.ref
            curr2 = curr2.ref
        return True

然后,您可以將鏈表與==運算符進行比較:

LL = LinkedList()

LL.add_beginning(3)
LL.add_beginning(2)
LL.add_beginning(1)
LL.add_beginning(3)

KK = LinkedList()
KK.add_beginning(2)
KK.add_beginning(6)
KK.add_beginning(5)
print(KK == LL)
>>> False
LL = LinkedList()

LL.add_beginning(3)
LL.add_beginning(2)
LL.add_beginning(1)

KK = LinkedList()
KK.add_beginning(3)
KK.add_beginning(2)
KK.add_beginning(1)
print(KK == LL)
>>> True

以下是如何編寫 function 來比較兩個LinkedList是否相等。

def equal(list1, list2):
    """Return True if data in the Nodes in two LinkedLists are all equal."""
    cur_node1, cur_node2 = list1.head, list2.head
    while cur_node1 and cur_node2:
        if cur_node1.data != cur_node2.data:
            return False
        cur_node1, cur_node2 = cur_node1.ref, cur_node2.ref

    return not cur_node1 and not cur_node2  # True if lists were same length.

if __name__ == "__main__":
    LL = LinkedList()

    LL.add_beginning(3)
    LL.add_beginning(2)
    LL.add_beginning(1)
    LL.add_beginning(3)
    LL.print()

    KK = LinkedList()
    KK.add_beginning(3)
    KK.add_beginning(2)
    KK.add_beginning(4)
    KK.add_beginning(3)

    print("Are LL and KK equal? ->", equal(LL, KK))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM