Python 学习:单链表的实现

要定义一个单链表,首先定义链表元素:Element.它包含3个字段:
list:标识自己属于哪一个list
datum:改元素的value
next:下一个节点的位置

  1. class LinkedList(object):
  2.     
  3.     class Element(object):
  4.         
  5.         def __init__(self,list,datum,next):
  6.             self._list = list
  7.             self._datum = datum
  8.             self._next = next

  9.         def getDatum(self):
  10.             return self._datum

  11.         datum = property(
  12.             fget = lambda self: self.getDatum())

  13.         def getNext(self):
  14.             return self._next

  15.         next = property(
  16.             fget = lambda self: self.getNext())


  17.     def __init__(self):

  18.         self._head = None
  19.         self._tail = None
  20.     def getHead(self):
  21.         return self._head
  22.     head = property(
  23.         fget = lambda self: self.getHead())
  24.     def prepend(self,item):
  25.         tmp = self.Element (self,item,self._head)
  26.         if self._head is None:
  27.             self._tail = tmp
  28.         self._head = tmp

  29.     def insert(self, pos, item):
  30.         i = 0
  31.         p = self._head
  32.         while p != None and i < pos -1:
  33.             p = p._next
  34.             i += 1
  35.         if p == None or i > pos-1:
  36.             return -1
  37.         tmp = self.Element(self, item, p._next)
  38.         p._next = tmp
  39.         return 1
  40.     def getItem(self, pos):
  41.         i = 0
  42.         p = self._head
  43.         while p != None and i < pos -1:
  44.             p = p._next
  45.             i += 1
  46.         if p == None or i > post-1:
  47.             return -1
  48.         return p._datum
  49.     def delete(self, pos):
  50.         i = 0
  51.         p = self._head
  52.         while p != None and i < pos -1:
  53.             p = p._next
  54.             i += 1
  55.         if p == None or i > post-1:
  56.             return -1
  57.         q = p._next
  58.         p._nex = q._next
  59.         datum = p._datum
  60.         return datum
  61.     def setItem(self, pos, item):
  62.         i = 0
  63.         p = self._head
  64.         while p != None and i < pos -1:
  65.             p = p._next
  66.             i += 1
  67.         if p == None or i > post-1:
  68.             return -1
  69.         p._datum = item
  70.         return 1
  71.     def find(self, pos, item):
  72.         i = 0
  73.         p = self._head
  74.         while p != None and i < pos -1:
  75.             if p._datum == item:
  76.                 return 1
  77.             p = p._next
  78.             i += 1
  79.         return -1
  80.     def empty(self):
  81.         if self._head == None:
  82.             return 1
  83.         return 0
  84.     def size(self):
  85.         i = 0
  86.         p = self._head
  87.         while p != None and i < pos -1:
  88.             p = p._next
  89.             i += 1
  90.         return i

  91.     def clear(self):
  92.         self._head = None
  93.         self._tail = None


  94. test = LinkedList()
  95. test.prepend('test0')
  96. print test.insert(1, 'test')
  97. print test.head.datum
  98. print test.head.next.datum

你可能感兴趣的:(python,Python,学习:单链表的实现)