Advertisement
Guest User

Untitled

a guest
Oct 6th, 2015
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.78 KB | None | 0 0
  1. class SinglyLinkedList(object):
  2.  
  3.     def __init__(self, head=None):
  4.         super(SinglyLinkedList, self).__init__()
  5.         self.head=head
  6.         pass
  7.  
  8.     def len(self):
  9.         current=self.head
  10.         count=0
  11.         while current:
  12.             count+=1
  13.             current=current.next
  14.         return count
  15.         # TODO
  16.        pass
  17.  
  18.     def __iter__(self):
  19.         # TODO
  20.        return self
  21.         pass
  22.  
  23.     def containts(self, item):
  24.         # TODO
  25.        current=self.head
  26.         found= False
  27.         while current and found is False:
  28.             if (current.item is item):
  29.                 found=True
  30.                 print("Item is found")
  31.             else:
  32.                 current=current.next
  33.         if found is False:
  34.             print("item not in the list")
  35.         pass
  36.  
  37.     def remove(self, item):
  38.         # TODO: find item and remove it.
  39.        current=self.head
  40.         previous=None
  41.         found=False
  42.         while current and found is False:
  43.             if (current.item is item):
  44.                 found=True
  45.             else:
  46.                 previous=current
  47.                 current=current.next
  48.         if found is False:
  49.             print("Item not in the list")
  50.         elif previous is None:
  51.             self.head=current.next
  52.         else:
  53.             previous.next=current.next
  54.         pass
  55.  
  56.     def prepend(self, item):
  57.         # TODO ad item to the front of the list
  58.        new_node=SinglyLinkedNode(item)
  59.         new_node.next=self.head
  60.         self.head=new_node
  61.         pass
  62.  
  63.     def __repr__(self):
  64.         s = "List:" + "->".join([item for item in self])
  65.         return s
  66.  
  67.     def list_print(self):
  68.         node = self.head
  69.         while node:
  70.             print (node.item)
  71.             node = node.next
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement