Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1782363
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: Python/Ruby

2015-07-10 14:13:45

首先是双链表。

点击(此处)折叠或打开

  1. class _DoublyLinkedBase:
  2.     #A base class providing a doubly llinked list representation

  3.     class _Node:
  4.         #nonpublic class for storing a doubly linked node
  5.         __slots__='_element','_prev','_next'

  6.         def __init__(self,element,prev,next):
  7.             self._element=element
  8.             self._prev=prev
  9.             self._next=next

  10. #----------------------------------------------------------------
  11.     def __init__(self):
  12.         #create an empty list
  13.         self._header=self._Node(None,None,None)
  14.         self._trailer=self._Node(None,None,None)
  15.         self._header._next=self._trailer
  16.         self._trailer._prev=self._header
  17.         self._size=0

  18.     def __len__(self):
  19.         return self._size

  20.     def is_empty(self):
  21.         return self._size==0

  22.     def _insert_between(self,e,predecessor,successor):
  23.         newest=self._Node(e,predecessor,successor)
  24.         predecessor._next=newest
  25.         successor._prev=newest
  26.         self._size+=1
  27.         return newest

  28.     def _delete_node(self,node):
  29.         #Delete nonsentinel node from the list and return its element
  30.         predecessor=node._prev
  31.         successor=node._next
  32.         predecessor._next=successor
  33.         successor._prev=predecessor
  34.         self._size-=1
  35.         element=node._element
  36.         node._prev=node._next=node._element=None
  37.         return element
linkedQueue

点击(此处)折叠或打开

  1. class LinkedDeque(_DoublyLinkedBase):
  2.     #Double-ended queue implementation

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

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

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

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

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

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

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

点击(此处)折叠或打开

  1. class PositionalList(_DoublyLinkedBase):
  2.     #A sequential container of elements allowing positional access

  3.     class Position:
  4.         #An abstraction representing the location of a single element

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

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

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

  15.         def __ne__(self,other):
  16.             #return True if other does not represent the same location
  17.             return not(self==other)
  18. #----------------------------------------------------------------------

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


  28.     def _make_position(self,node):
  29.         #return position instance for given node (or None if sentinel)
  30.         if node is self._header or node is self._trailer:
  31.             return None
  32.         else:
  33.             return self.Position(self,node)

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

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

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

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

  49.     def __iter__(self):
  50.         #generate a forward iteration of the elements of the list
  51.         cursor=self.first()
  52.         while cursor is not None:
  53.             yield cursor.element()
  54.             cursor=self.after(cursor)

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

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

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

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

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

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

  77.     def replace(self,p,e):
  78.         #replace the element at position p with e
  79.         #return the element formerly at position p
  80.         original=self._validate(p)
  81.         old_value=original._element
  82.         original._element=e
  83.         return old_value


阅读(1770) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~