zoukankan      html  css  js  c++  java
  • 单向链表

    class Hero(object):
        def __init__(self, no=None, nickname=None, name=None, pNext=None):
            self.no = no
            self.nickname = nickname
            self.name = name
            self.pNext = pNext
    
    
    def getHero(head, no):
        cur = head
        while cur.pNext != None:
            if cur.no == no:
                print("找到英雄编号是:%s,姓名是:%s,外号是:%s" % (cur.no, cur.name, cur.nickname))
                break
            cur = cur.pNext
        else:
            print("没有添加该英雄")
    
    
    def add_hero(head, pNew):
        # 1.直接在链表最后加上
        # 先找到链表的最后
        cur = head
        # while cur.pNext != None:
        #     cur = cur.pNext
        # # 当退出链表的时候就是到队尾了
        # cur.pNext = pNew
    
        # 2.在指定位置添加
        # 判断是不是尾节点,如果不是,则是插入操作
        while cur.pNext != None:
            if cur.pNext.no > pNew.no:
                # 找到位置了
                break
            # 没有找到位置,继续往下走
            cur = cur.pNext
    
        pNew.pNext = cur.pNext
        cur.pNext = pNew
    
    
    def show_hero(head):
        if isEmpty(head):
            return None
    
        cur = head
        # 一直循环,直到最后一个元素
        while cur.pNext != None:
            print("英雄编号:%s, 外号:%s, 姓名:%s" % (cur.pNext.no, cur.pNext.nickname, cur.pNext.name))
            cur = cur.pNext
    
    
    def isEmpty(head):
        if head.pNext == None:
            return True
    
        return False
    
    
    def delHero(head, no):
        cur = head
        while cur.pNext != None:
            # 如果循环到指定的元素
            if cur.pNext.no == no:
                # 开始删除
                cur.pNext = cur.pNext.pNext
                break
            cur = cur.pNext
        else:
            print("没有找到")
    
    
    def updateHero(head, no, name):
        cur = head
        while cur.pNext != None:
            if cur.pNext.no == no:
                cur.pNext.name = name
                break
            cur = cur.pNext
        else:
            print("没找到该英雄")
    
    
    # 头结点, 不存放数据
    head = Hero()
    
    # 首节点
    h1 = Hero(1, "及时雨", "松江")
    
    h2 = Hero(2, "玉麒麟", "卢俊义")
    h4 = Hero(4, "入云龙", "公孙胜")
    h6 = Hero(6, "豹子头", "林冲")
    
    add_hero(head, h1)
    add_hero(head, h2)
    add_hero(head, h6)
    add_hero(head, h4)
    
    # 展示所有英雄
    show_hero(head)
    
    updateHero(head, 1, "宋江")
    
    getHero(head, 1)
    
  • 相关阅读:
    mycat分表扩展之全局序列(数据库方式)
    mycat分表扩展之按日期(天)分片
    mycat分表扩展之范围约定
    mycat分表扩展之分片枚举
    jdk导入证书链步骤说明
    oracle md5加密函数
    oracle常用(11g)
    oracle 11g 常规sql
    oracle归档日志
    oracle定时任务
  • 原文地址:https://www.cnblogs.com/qiaoqianshitou/p/9928333.html
Copyright © 2011-2022 走看看