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

    class Hero(object):
        def __init__(self,no=None,name=None,nickname=None,pNext=None):
            self.no = no
            self.name = name
            self.nickname = nickname
            self.pNext = pNext
    
    
    def addHero(head,hero):
        cur = head
        ####第一种 添加到聊表后面
        '''
        while cur.pNext != None:
            cur = cur.pNext
        cur.pNext = hero
        '''
    
        #####第二种 指定添加
    
        while cur.pNext != None:
            ####找到了指定位置,直接跳出while循环
            if cur.pNext.no >hero.no:
                break
            ####没有找到的话 继续找
            cur = cur.pNext
    
        hero.pNext = cur.pNext###本质是插入数据指向插入数据后面的内存地址
        cur.pNext = hero   #本质使插入数据前的pnext指向插入数据的内存地址
    
    
    def getAll(head):
        cur = head
        while cur.pNext != None:
            cur = cur.pNext
            print('编号是:%s,姓名是%s,外号是%s'%(cur.no,cur.name,cur.nickname))
    
    head = Hero()
    hero1 = Hero(1,'宋江','及时雨')
    addHero(head,hero1)
    
    hero2 = Hero(2,'卢俊义','玉麒麟')
    
    addHero(head,hero2)
    
    hero4 = Hero(4,'武松','行者')
    addHero(head,hero4)
    
    hero3 = Hero(3,'金莲','大嫂子')
    addHero(head,hero3)
    
    getAll(head)

  • 相关阅读:
    php
    php
    linux 网络管理基础 OSI ISO IOS的区别
    Linux 添加交换分区的步骤
    linux 命令
    linux命令
    linux 命令
    linux 命令
    Linux命令
    linux命令- 挂载命令 mount
  • 原文地址:https://www.cnblogs.com/tangda/p/11123110.html
Copyright © 2011-2022 走看看