繁体   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