免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1161 | 回复: 1
打印 上一主题 下一主题

求助. MIT一道题. [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-28 15:16 |只看该作者 |倒序浏览
class Node(object):
    '''A node in a linked list.'''

    def __init__(self, data):
        '''(Node, object) -> NoneType
        Create a Node containing data with None as next.'''

        self.data = data
        self.next = None


def to_string(front):
    '''(Node) -> str
    Return a list representation of the linked list front in the Python list
    style. If the list is circular, include ',...' at the end.'''

    res = '['

    if front:
        t = front

        # Treat the first node separately because we want one fewer commas than
        # there are nodes.
        res += str(t.data)
        t = t.next

        # Move along until we fall off or wrap back around.
        while t and t != front:
            res += ', %s' % t.data
            t = t.next

        # If we wrapped around, mark it as circular.
        if t == front:
            res += ', ...'

    res += ']'

    return res


def append(list1, list2):
    '''(Node, Node) -> Node
    Return the head of the linked list made by appending list2 to list1.
    Precondition: neither are circular.'''
    if list1[-2] != "." and list2[-2] != "." :
        
        
        
   



   


def toggle_circular(front):
    '''(Node) -> NoneType
    If linked list front is circular, make it non-circular. If linked list
    front is non-circular, make it circular. If front is None, do nothing.'''

    pass


def remove_values(front, v):
    '''(Node, object) -> Node
    Remove all Nodes from linked list front whose data are equal to v and
    return the front of the resulting linked list.
    Precondition: front is not circular.'''

    pass


if __name__ == '__main__':
    print to_string(None)

    lst = Node(1)
    print to_string(lst)

    lst.next = lst  # Make it circular.
    print to_string(lst)


    lst.next = Node(2)  # Make it non-circular and append a second Node.
    print to_string(lst)

    lst.next.next = lst  # Make it circular.
    print to_string(lst)





追问: CLASS很难理解,可以简单概括下CLASS的用处吗

论坛徽章:
0
2 [报告]
发表于 2012-06-13 08:12 |只看该作者
回复 1# kendriczz


    class 就是类亚,难理解在那里?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP