double_linked_list in Python
来源:互联网 发布:ei数据库怎么进入 编辑:程序博客网 时间:2024/06/18 15:49
class Frob(object): def __init__(self, name): self.name = name self.before = None self.after = None def setBefore(self, before): # example: a.setBefore(b) sets b before a self.before = before def setAfter(self, after): # example: a.setAfter(b) sets b after a self.after = after def getBefore(self): return self.before def getAfter(self): return self.after def myName(self): return self.namedef insert(atMe, newFrob): """ atMe: a Frob that is part of a doubly linked list newFrob: a Frob with no links This procedure appropriately inserts newFrob into the linked list that atMe is a part of. """ atMe_temp = atMe if newFrob.name < atMe.name: while True: new = atMe.before if new == None: break if new.name < newFrob.name: break atMe = atMe.before if new == None: newFrob.after = atMe atMe.before = newFrob else: temp = new.after new.after = newFrob newFrob.before = new newFrob.after = temp temp.before = newFrob else: while True: new = atMe.after if new == None: break if new.name > newFrob.name: break atMe = atMe.after if new == None: atMe.after = newFrob newFrob.before = atMe else: temp = new.before temp.after = newFrob newFrob.before = temp new.before = newFrob newFrob.after = new eric = Frob('eric')andrew = Frob('andrew')ruth = Frob('ruth')fred = Frob('fred')martha = Frob('martha')insert(eric, andrew)insert(eric, ruth)insert(eric, fred)insert(ruth, martha)print andrew.after.nameprint eric.before.name,eric.after.nameprint fred.before.name,fred.after.nameprint martha.before.name,martha.after.nameprint ruth.before.namedef findFront(start): """ start: a Frob that is part of a doubly linked list returns: the Frob at the beginning of the linked list """ # Your Code Here if start.before == None: return start else: return findFront(start.before)print findFront(martha).nameprint findFront(fred).name
0 0
- double_linked_list in Python
- double_linked_list
- [Python]Understand attribute in Python
- Text Processing in Python
- Algorithm in Python(1)
- Patterns in Python
- metaclass in python
- list in python
- Python in a Nutshell
- File Management in Python
- Text Processing in Python
- Python in a Nutshell
- source backup in python
- Python in a Nutshell
- Debugging Python in VIM
- install python in linux
- python build-in functions
- Think in Python
- Ubuntu常用命令
- UVA 10951 Polynomial GCD 多项式欧几里德求最大公共多项式
- leetcode-3Sum
- 二分图匹配
- android 自己创建一个注释模板
- double_linked_list in Python
- nefu 636
- cocos2dx CCControlSlider
- 近期硬件的了解情况
- 潘悟云教授“新世纪语言学的转型“讲座笔记
- win7下IIS的安装和配置 图文教程
- poj/zoj Polynomial Showdown
- 【未测试】CentOS 6.5快速部署HTTP WEB服务器和FTP服务器
- cache机制