fork download
  1. #!/usr/bin/env python3
  2.  
  3. class Cell(object):
  4. def __init__(self, n, k, v):
  5. self.next = n
  6. self.key = k
  7. self.value = v
  8.  
  9. def insertCell(head, key, value):
  10. while head.next: head = head.next
  11. head.next = Cell(None, key, value)
  12.  
  13. def deleteCell(head, key):
  14. while key != head.next.key: head = head.next
  15. head.next = head.next.next
  16.  
  17. def searchCell(head, key):
  18. return "not found" if head.next == None else head.value if head.key == key else searchCell(head.next, key)
  19.  
  20. def clearCell(head):
  21. while head.next != None:
  22. tmp = head.next.next
  23. del head.next
  24. head.next = tmp
  25.  
  26. def printCell(head):
  27. text = "[ "
  28. while head.next != None:
  29. text += str(head.next.value)
  30. if head.next.next != None:
  31. text += ", "
  32. head = head.next
  33. text += " ]"
  34. print(text)
  35.  
  36. if __name__ == '__main__':
  37. head = Cell(None, "head", -1)
  38.  
  39. insertCell(head, "one", 1)
  40. insertCell(head, "two", 2)
  41. insertCell(head, "three", 3)
  42. printCell(head)
  43.  
  44. deleteCell(head, "two")
  45. printCell(head)
  46.  
  47. insertCell(head, "four", 4)
  48. insertCell(head, "five", 5)
  49. deleteCell(head, "five")
  50. printCell(head)
  51.  
  52. print(searchCell(head, "three"))
  53. print(searchCell(head, "two"))
  54.  
  55. clearCell(head)
  56. printCell(head)
  57.  
Success #stdin #stdout 0.02s 9132KB
stdin
Standard input is empty
stdout
[ 1, 2, 3 ]
[ 1, 3 ]
[ 1, 3, 4 ]
3
not found
[  ]