"""Linked List implementation"""classNode(object):"""Node class"""def__init__(self,value):self.value=valueself.next=Nonedef__repr__(self):returnstr(self.value)classList(object):"""List class"""def__init__(self):self.root=Noneself.count=0definsert_start(self,value):"""Inster value at beggining of list"""ifnotself.root:self.root=Node(value)else:node=Node(value)node.next=self.rootself.root=nodeself.count=self.count+1definsert_end(self,value):"""Inster value at end of list"""ifnotself.root:self.root=Node(value)else:aux=self.rootwhileaux.next:aux=aux.nextaux.next=Node(value)self.count=self.count+1defremove(self,value):"""Remove all itens that match the value"""ifnotself.root:raiseException('The list is empty')ifself.root.value==value:matched=self.rootself.root=self.root.nextdelmatchedself.count=self.count-1else:aux=self.rootwhileaux.next:ifaux.next.value==value:matched=aux.nextaux.next=aux.next.nextdelmatchedself.count=self.count-1aux=aux.next
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.