设为首页 收藏本站
查看: 737|回复: 0

[经验分享] python LinkedDeque,PositionalList的实现

[复制链接]

尚未签到

发表于 2015-12-15 09:04:29 | 显示全部楼层 |阅读模式
首先是双链表。



  • class _DoublyLinkedBase:

  •     #A base class providing a doubly llinked list representation

  •     class _Node:
  •         #nonpublic class for storing a doubly linked node
  •         __slots__='_element','_prev','_next'

  •         def __init__(self,element,prev,next):
  •             self._element=element
  •             self._prev=prev
  •             self._next=next

  • #----------------------------------------------------------------
  •     def __init__(self):
  •         #create an empty list
  •         self._header=self._Node(None,None,None)
  •         self._trailer=self._Node(None,None,None)
  •         self._header._next=self._trailer
  •         self._trailer._prev=self._header
  •         self._size=0

  •     def __len__(self):
  •         return self._size

  •     def is_empty(self):
  •         return self._size==0

  •     def _insert_between(self,e,predecessor,successor):
  •         newest=self._Node(e,predecessor,successor)
  •         predecessor._next=newest
  •         successor._prev=newest
  •         self._size+=1
  •         return newest

  •     def _delete_node(self,node):
  •         #Delete nonsentinel node from the list and return its element
  •         predecessor=node._prev
  •         successor=node._next
  •         predecessor._next=successor
  •         successor._prev=predecessor
  •         self._size-=1
  •         element=node._element
  •         node._prev=node._next=node._element=None
  •         return element
linkedQueue




  • class LinkedDeque(_DoublyLinkedBase):

  •     #Double-ended queue implementation

  •     def first(self):
  •         #return but not remove the element at the front
  •         if self.is_empty():
  •             raise Exception('Deque is empty')
  •         return self._header._next._element          #real item after header

  •     def last(self):
  •         if self.is_empty():
  •             raise Exception('Deque is empty')
  •         return self._trailer._prev._element         #real item before trailer

  •     def insert_first(self,e):
  •         #Add an element to the front of the deque
  •         self._insert_between(e,self._header,self._head._next) #after header

  •     def insert_last(self,e):
  •         #Add an element to the back of the deque
  •         self._insert_between(e,self._trailer._prev,self._trailer) #before trailer

  •     def delete_first(self):
  •         #remove and return the element from the front of the deque
  •         if self.is_empty():
  •             raise Exception('Deque is empty')
  •         return self._delete_node(self._header._next) #use inherited method

  •     def delete_last(self):
  •         #remove and return the element from the back of the deque

  •         if self.is_empty():
  •             raise Exception('Deque is empty')
  •         return self._delete_node(self._trailer._prev)
PositionalList




  • class PositionalList(_DoublyLinkedBase):

  •     #A sequential container of elements allowing positional access

  •     class Position:
  •         #An abstraction representing the location of a single element

  •         def __init__(self,container,node):
  •             #constructor should not be invoked by user
  •             self._container=container
  •             self._node=node

  •         def element(self):
  •             #return the element stored at this position
  •             return self._node._element

  •         def __eq__(self,other):
  •             #return true if other is a Position representing the same location
  •             return type(other) is type(self) and other._node is self._node

  •         def __ne__(self,other):
  •             #return True if other does not represent the same location
  •             return not(self==other)
  • #----------------------------------------------------------------------

  •     def _validate(self,p):
  •         #Return position's node, or raise appropriate error if invalid
  •         if not isinstance(p,self.Position):
  •             raise TypeError(\'P must be proper Position type\')
  •         if p._container is not self:
  •             raise ValueError(\'P does not belong to this container\')
  •         if p._node._next is None:
  •             raise ValueError(\'p is not longer valid\')
  •         return p._node


  •     def _make_position(self,node):
  •         #return position instance for given node (or None if sentinel)
  •         if node is self._header or node is self._trailer:
  •             return None
  •         else:
  •             return self.Position(self,node)

  •     #accessors
  •     def first(self):
  •         #return the first position in the list (or None if list is empty)
  •         return self._make_position(self._header._next)

  •     def last(self):
  •         #return the last position in the list(or None if list is empty)
  •         return self._make_position(self._tailer._prev)

  •     def before(self,p):
  •         #return the Position just before Position p(or None if p is first)
  •         node=self._validate(p)
  •         return self._make_position(node._prev)

  •     def after(self,p):
  •         #return the Positoin just after Position p(or None if p is last)
  •         node=self._validate(p)
  •         return self._make_position(node._next)

  •     def __iter__(self):
  •         #generate a forward iteration of the elements of the list
  •         cursor=self.first()
  •         while cursor is not None:
  •             yield cursor.element()
  •             cursor=self.after(cursor)

  •     #override inherited version to return Positions,rather than Node
  •     def _insert_between(self,e,predecessor,successor):
  •         #Add element between existing nodes and return new Position
  •         node=super()._insert_between(e,predecessor,successor)
  •         return self._make_position(node)

  •     def add_first(self,e):
  •         #insert element e at the front of the list and return the position
  •         return self._insert_between(e,self._header,self._header._next)

  •     def add_last(self,e):
  •         #insert element e at the back of the list and return new position
  •         return self._insert_between(e,self._trailer._prev,self._trailer)

  •     def add_before(self,p,e):
  •         #insert element e into list before Position p and return new Position
  •         original=self._validate(p)
  •         return self._insert_between(e,original._prev,original)

  •     def add_after(self,p,e):
  •         original=self._validate(p)
  •         return self._insert_between(e,original,original._next)

  •     def delete(self,p):
  •         #remove and return the element at position p
  •         original=self._validate(p)
  •         return self._delete_node(original)

  •     def replace(self,p,e):
  •         #replace the element at position p with e
  •         #return the element formerly at position p
  •         original=self._validate(p)
  •         old_value=original._element
  •         original._element=e
  •         return old_value

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-151350-1-1.html 上篇帖子: python杂记(二) 下篇帖子: windows8 64位系统安装python环境以及paramiko
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表