Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SinglyLinkedList(object):
- def __init__(self, head=None):
- super(SinglyLinkedList, self).__init__()
- self.head=head
- pass
- def len(self):
- current=self.head
- count=0
- while current:
- count+=1
- current=current.next
- return count
- # TODO
- pass
- def __iter__(self):
- # TODO
- return self
- pass
- def containts(self, item):
- # TODO
- current=self.head
- found= False
- while current and found is False:
- if (current.item is item):
- found=True
- print("Item is found")
- else:
- current=current.next
- if found is False:
- print("item not in the list")
- pass
- def remove(self, item):
- # TODO: find item and remove it.
- current=self.head
- previous=None
- found=False
- while current and found is False:
- if (current.item is item):
- found=True
- else:
- previous=current
- current=current.next
- if found is False:
- print("Item not in the list")
- elif previous is None:
- self.head=current.next
- else:
- previous.next=current.next
- pass
- def prepend(self, item):
- # TODO ad item to the front of the list
- new_node=SinglyLinkedNode(item)
- new_node.next=self.head
- self.head=new_node
- pass
- def __repr__(self):
- s = "List:" + "->".join([item for item in self])
- return s
- def list_print(self):
- node = self.head
- while node:
- print (node.item)
- node = node.next
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement