簡體   English   中英

雙鏈表python

[英]Doubly-Linked List python

我正在使用Python 3.0 ,並且必須創建以下代碼:

1)實現一個稱為Setplus的ADT作為有序雙向鏈接列表,其中項從列表中的最小項到最大項排序

所以首先我創建了一個名為Double_Node的模塊

class Double_Node:
    """
    Fields: value stores any value
            next points to the next node in the list
            prev points to the previous node in the list
    """

    ## Double_Node() produces a newly constructed empty node.
    ## __init__: Any -> Node
    def __init__(self, value, prev = None, next = None):
        self.value = value 
        self.prev_node = prev
        self.next_node = next

    def get_next (self):
        return self.next_node
    def set_next (self, n):
        self.next_node = n
    def get_prev (self):
        return self.prev_node
    def set_prev (self, p):
        self.next_prev_node = p
    def get_value (self):
        return self.value
    def set_value (self, d):
        self.value = d


    ## print(self) prints the value stored in self.
    ## Effect: Prints value.
    ## __str__: Node -> Str
    def __str__(self):
        return str(self.value)

然后,我創建一個名為Setplus的類:

class Setplus:
    """
    Field: _head points to the first node in the linked list
           _tail points to the last node in a linked list
    """


    ## Setplus() produces a newly constructed empty setplus.
    ## __init__: -> Setplus
    def __init__(self):
        self._head = None
        self._tail = None

    ## self.empty() produces True if self is empty.
    ## empty: Setplus -> Bool
    def empty(self):
        return self._head == None

    ## value in self produces True if value is an item in self.
    ## __contains__: Setplus Any -> Bool
    def __contains__(self, value):
        current = self._head
        while current:
            if current.get_value == value:
                return True
            else:
                current = current.get_next
        return False

    ## self.distinct() produces True if all items are distinct.
    ## distinct: Setplus -> Bool
    #def distinct(self):

    ## count(value) produces the number of occurrences of value.
    ## count: Setplus Any -> Int
        def count(self, value):
            counter = 0
            current = self._head
            while current != None:
                if current.value == value:
                    counter += 1
                    print (counter)
                else:
                    current = current.next
            return counter

    ## self.add(value) adds value as an item in order.
    ## Effects: Mutates self.
    ## add: Setplus Any -> None 
    def add(self, value):
        new_node = Double_Node(value)
        if self.head == None:
            self.head = new_node
        if self.tail != None:
            slef.tail.next = new_node

        self.tail = new_node

我在創建創建一個contains方法(計數)時遇到麻煩,該方法計數值的數量並添加(add)(按正確的非降序順序添加節點)。

提前致謝

代碼中的第一個主要問題是拼寫錯誤和名稱錯誤。

還有一個明顯的錯字, slef而不是self在你的功能之一。

在很多地方,您使用兩個不同的名稱來代表相同的屬性( _headheadnextnext_node )。

您還已經在Double_Node類中定義了getter和setter函數,但是只有在Setplus嘗試使用它們時,才引用該方法而不調用它。 current = current.get_next幾乎可以肯定是current = current.get_next()

關於getter和setter函數的簡要說明:Python類通常不需要它們。 只需直接使用屬性。 如果以后發現您需要更多奇特的行為(例如,新設置值的驗證或即時生成請求的值),則可以使用property更改類,以將屬性訪問語法轉換為方法調用。 在其他編程語言中,通常不能以這種方式離開屬性訪問,因此,為了從一開始就具有可擴展的API,建議使用getter和setter方法。

(請注意,如果您是學生,您的講師對Python的熟悉程度可能不如其他語言,因此,盡管他們通常在Python代碼中風格很差,但他們仍希望您編寫getter和setter。請考慮學習如何使用而不是property ,以后您可能會大吃一驚!)

我只是Double_Node風格Double_Node中的getter和setter函數。 但是 ,如果要保留它們(也許是因為它們是分配作業所必需的),則應在代碼中實際使用它們!

最后,為了獲得您需要幫助的實際問題,按排序順序將其插入鏈表,您可能需要執行以下操作:

def add(self, value):
    new_node = Double_Node(value)
    if self._head == None: # inserting into empty list
        self._head = self._tail = new_node

    else: # inserting into a list that contains at least one node already
        current = self._head
        while current and current.value < value: # find a node to insert before
            current = current.get_next()

        if current: # not inserting at end of list
            prev = current.get_prev()
            if prev: # not inserting at start
                new_node.set_prev(prev)
                prev.set_next(new_node)

            else: # inserting at start
                self._head = new_node

            new_node.set_next(current)
            current.set_prev(new_node)

        else: # inserting at end
            new_node.set_prev(self._tail)
            self._tail.set_next(new_node)
            self._tail = new_node

在按排序順序完成add插入后,其他方法可以利用這一事實。 例如,如果__contains__看到的值大於要查找的值,則可以停止搜索,並且count將在一個連續的組中找到所有匹配的值。

如果您想為此使用現有的Python類,則可能會發現它們很有用。

雙鏈表:

deque ,來自collections模塊:

https://docs.python.org/3/library/collections.html#collections.deque

從最小到最大的有序數據結構:

heapq模塊,用於實現最小堆。

https://docs.python.org/3/library/heapq.html

暫無
暫無

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

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