码迷,mamicode.com
首页 > 其他好文 > 详细

单链表

时间:2020-04-09 00:23:56      阅读:53      评论:0      收藏:0      [点我收藏+]

标签:python   print   排行榜   pytho   class   while   遍历   内存   sel   

单链表

"""

#### 梁山好汉排行榜

class Hero():

    def __init__(self, no=None, name=None, nickname=None, pNext=None):
        self.no = no
        self.name = name
        self.nickname = nickname
        self.pNext = pNext


def add(head, hero):
    #### head 节点不能动,因此需要第三方的临时变量帮助head去遍历
    cur = head

    while cur.pNext != None:
        ### 把下一个节点的内存地址付给  cur ,那此时cur就指向下一个节点
        cur = cur.pNext

    ### 当退出上述循环的时候偶,cur就已经指向尾节点
    cur.pNext = hero

def getAll(head):
    cur = head

    while cur.pNext != None:
        cur = cur.pNext
        print(‘编号是:%s, 名称是:%s, 外号是:%s‘ % (cur.no, cur.name, cur.nickname))


def delHero(head, no):
    cur = head

    while cur.pNext != None:
        if cur.pNext.no == no:
            break
        cur = cur.pNext

    cur.pNext = cur.pNext.pNext





head = Hero()

h1 = Hero(1, ‘宋江‘, ‘及时雨‘)
add(head, h1)

h2 = Hero(2, ‘卢俊义‘, ‘xxx‘)
add(head, h2)

h3 = Hero(3, ‘西门庆‘, ‘dsadsad‘)
add(head, h3)

getAll(head)

"""

单链表

标签:python   print   排行榜   pytho   class   while   遍历   内存   sel   

原文地址:https://www.cnblogs.com/yafeng666/p/12663785.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!