Advertisement
cwchen

C-style Linked List Demo in Python

Feb 10th, 2016
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.46 KB | None | 0 0
  1. # Just for demo.  DON'T DO THIS IN PRODUCTION CODE
  2.  
  3. import sys
  4.  
  5. class List(object):
  6.     class Node(object):
  7.         def __init__(self, data):
  8.             self.data = data
  9.             self.next = None
  10.  
  11.     def __init__(self):
  12.         self.head = None
  13.         self.current = None
  14.  
  15.     def __iter__(self):
  16.         while self.current is not None:
  17.             yield self.current.data
  18.             self.current = self.current.next
  19.         self.current = self.head  # reset self.current for later use
  20.  
  21.     def __len__(self):
  22.         count = 0
  23.         while self.current is not None:
  24.             count += 1
  25.             self.current = self.current.next
  26.         self.current = self.head  # reset self.current for later use
  27.         return count
  28.  
  29.     def __getitem__(self, key):
  30.         if not isinstance(key, int):
  31.             raise TypeError
  32.  
  33.         value = None
  34.         if key >= 0:
  35.             count = 0
  36.             while self.current is not None:
  37.                 if count == key:
  38.                     value = self.current.data
  39.                     break
  40.                 count += 1
  41.                 self.current = self.current.next
  42.  
  43.             self.current = self.head
  44.  
  45.             if key > count:
  46.                 raise IndexError
  47.         else:
  48.             count = -len(self)
  49.  
  50.             if key < count:
  51.                 raise IndexError
  52.  
  53.             while self.current is not None:
  54.                 if count == key:
  55.                     value = self.current.data
  56.                     break
  57.                 count += 1
  58.                 self.current = self.current.next
  59.  
  60.             self.current = self.head
  61.         return value
  62.  
  63.     def __setitem__(self, key, value):
  64.         if not isinstance(key, int):
  65.             raise TypeError
  66.  
  67.         if key >= 0:
  68.             count = 0
  69.             while self.current is not None:
  70.                 if key == count:
  71.                     self.current.data = value
  72.                     break
  73.                 count += 1
  74.                 self.current = self.current.next
  75.             self.current = self.head
  76.             if key > count:
  77.                 raise IndexError
  78.         else:
  79.             count = -len(self)
  80.             if key < count:
  81.                 raise IndexError
  82.             while self.current is not None:
  83.                 if key == count:
  84.                     self.current.data = value
  85.                     break
  86.                 count += 1
  87.                 self.current = self.current.next
  88.             self.current = self.head
  89.  
  90.     def append(self, data):
  91.         klass = globals()[self.__class__.__name__]
  92.         node = klass.Node(data)
  93.  
  94.         if self.head is None:
  95.             self.head = node
  96.             self.current = self.head
  97.         else:
  98.             current = self.head
  99.             while current.next is not None:
  100.                 current = current.next
  101.             current.next = node
  102.  
  103.  
  104. if __name__ == '__main__':
  105.     _list = List()
  106.     _list.append(33)
  107.     _list.append(55)
  108.     _list.append(88)
  109.     _list.append(77)
  110.  
  111.     print 'length:', len(_list)
  112.     print '3rd item:', _list[2]
  113.     print 'last item:', _list[-1]
  114.  
  115.     try:
  116.         print _list[99]
  117.     except IndexError:
  118.         sys.stderr.write("Out of range\n")
  119.  
  120.     for e in _list:
  121.         print e
  122.  
  123.     print 'Change _list item'
  124.     _list[0] = 100
  125.     for e in _list:
  126.         print e
  127.  
  128.     try:
  129.         _list[99] = 66
  130.     except IndexError:
  131.         sys.stderr.write("Out of range\n")
  132.  
  133.     for e in _list:
  134.         print e
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement