python 单链表
来源:互联网 发布:淘宝做图片用什么软件 编辑:程序博客网 时间:2024/05/21 15:58
闲着无聊,python单链表
#coding:utf-8class Linked: def __init__(self): self.L = None def insert(self, k): self.pprint() node = self.Node(k) p = temp = self.L # 找到合适位置的前一个节点p, 若p==temp,表示应插入到头结点 while temp and cmp(temp.k, k) == -1: p = temp temp = temp.next # 两种情况, 插在头结点还是其他位置 if p==temp==self.L: node.next = self.L self.L = node else: node.next = p.next p.next = node def search(self, k): node = self.L while node and cmp(node.k, k) != 0: node = node.next return node def remove(self, k): self.pprint() p = temp = self.L while temp and temp.k != k: p = temp temp = temp.next if temp is None: # 两种情况,链表为空或者找到了最后 if temp == p: print 'null linked' else: print 'not find %s' % str(k) else: # 两种情况,头结点和其余的节点 if temp == p: self.L = temp.next else: p.next = temp.next del temp print 'delete %s' % str(k) def pprint(self): node = self.L while node: print '%s(%s, %s)' % (str(id(node)), str(node.k), str(node.next)), node = node.next print @classmethod def test(cls): L = cls() L.insert(20) L.insert(18) L.insert(21) L.insert(19) L.remove(24) L.remove(21) L.remove(19) L.remove(18) L.remove(24) L.remove(20) L.remove(20) L.pprint() print 'test over' class Node: def __init__(self, k): self.k = k self.next = None def __str__(self): return unicode(id(self))Linked.test()
1 0
- python 单链表
- python 单链表
- Python单链表
- python单链表
- 单链表反转python实现
- 单链表的Python实现
- python 构造单链表
- 用python实现单链表
- 单链表反转python实现
- 单链表-Python操作
- python中的单链表操作
- python 实现单链表
- Python数据结构--单链表
- Python数据结构之单链表
- python描述单链表
- Python
- Python
- Python
- hdu3507Print Article【斜率优化入门题】
- 什么是批处理
- android 帧动画注意事项
- 免费的论文查重网站
- Cocos2d-x开发FlappyBird03-GameScene游戏场景初进入
- python 单链表
- HDU 4001 To Miss Our Children Time(DP LIS)
- 三星FOTA升级,还安卓一个公道
- 20160224 mystrcpy
- XXX电子商务平台技术框架
- Android权限
- Android之ListView中的分割线
- Redis的内部结构
- Tomcat中的线程池(APR和ThreadPool)