查找node_chain中是否存在重复项?

时间:2019-02-16 01:28:17

标签: python adt

  • 这是该算法的伪代码:
    1. 启动助行器沿着节点链行走
    2. 步行者还没有结束时
    3. 启动检查器,以从助行器走到链的末端
    4. 检查器没有结束
    5. 如果检查器的数据值与步行者的数据值相同,则返回True
    6. 如果walker到达链的末尾,则返回False
import node as node

def contains_duplicates(node_chain):
    walker = node_chain
    if walker is None:
        return False
    while walker is not None:
        checker = node.get_next(walker)
        while checker is not None:
            if node.get_data(walker) == node.get_data(checker):
                contains_duplicates(checker)
                return True
            elif node.get_data(walker) != node.get_data(checker):
                return False
        return

所以当我们输入时:

  1. chain1 =节点。创建(1, 节点。创建(2, 节点。创建(3, 节点。创建(4, 节点。创建(5))))) 打印(“重复项?”,包含重复项(chain1))

  2. chain2 =节点。创建(1, 节点。创建(2, 节点。创建(3, 节点。创建(4, 节点。创建(1))))) 打印(“重复项?”,包含重复项(chain2))

输出:

重复吗?假 重复吗?是

这是名为adt.py的adt

# Defines the Node ADT
# A node is a simple container with two pieces of information
#   data: the contained information
#   next: a reference to another node
# We can create node-chains of any size.
# Implementation notes:
#   This implementation uses a Python dictionary as a record.


def create(data, next=None):
    """
    Create a new node for the given data.
    Pre-conditions:
        data:  Any data value to be stored in the node
        next:  Another node (or None, by default)
    Post-condition:
        none
    Return:
        the node created
    """
    return {'data':data, 'next':next}


def get_data(node):
    """
    Retrieve the contents of the data field.
    Pre-conditions:
        node: a node created by create()
    Post-conditions:
        none
    Return
        the data value stored previously in the node
    """
    assert node is not None, "get_data() called with argument None"
    return node['data']


def get_next(node):
    """
    Retrieve the contents of the next field.
    Pre-conditions:
        node: a node created by create()
    Post-conditions:
        none
    Return
        the value stored previously in the next field
    """
    assert node is not None, "get_next() called with argument None"
    return node['next']


def set_data(node, val):
    """
    Set the contents of the data field to val.
    Pre-conditions:
        node: a node created by create()
        val:  a data value to be stored
    Post-conditions:
        stores the new data value, replacing the existing value
    Return
        none
    """
    assert node is not None, "set_data() called with argument None"
    node['data'] = val


def set_next(node, val):
    """
    Set the contents of the next field to val.
    Pre-conditions:
        node: a node created by create()
        val:  a node, or the value None
    Post-conditions:
        stores the new next value, replacing the existing value
    Return
        none
    """
    assert node is not None, "set_next() called with argument None"
    node['next'] = val



1 个答案:

答案 0 :(得分:0)

IIUC

我想这就是你想要的:

def contains_duplicates(node_chain):
    walker = node_chain
    if walker is None:
        return False
    while walker is not None:
        checker = node.get_next(walker)
        while checker is not None:
            if node.get_data(walker) == node.get_data(checker): //if there is a duplicate, return True
                return True
            else: //continue the loop until the end is reached, which will break the loop in line 3.
                contains_duplicates(checker)
                return

免责声明:我完全是在猜测,不知道这是什么意思:)