单链表
单链表
class Hero(): def __init__(self,num=None,name=None,nickname=None,pNext=None): self.num = num self.name = name self.nickname = nickname self.pNext = pNext def add(head,hero): 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.num,cur.name,cur.nickname)) def delHero(head,num): cur = head while cur.pNext != None: if cur.pNext.num == num: break cur = cur.pNext cur.pNext = cur.pNext.pNext head = Hero() # 头节点 h1 = Hero(1,'宋江','及时雨') # h1 实际上是个内存地址 add(head,h1) h2 = Hero(1,'王五','xxx') # h1 实际上是个内存地址 add(head,h2) getAll(head)