单链表

单链表

"""

#### 梁山好汉排行榜

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)

"""
posted @ 2020-04-08 23:17  alen_zhan  阅读(96)  评论(0编辑  收藏  举报
返回顶部