fork download
  1. # リストの要素を定義するクラス
  2. class Cell():
  3. def __init__(self, n, k, v):
  4. self.next = n # 次のCell
  5. self.key = k # キー(文字列)
  6. self.value = v # 値(数値)
  7.  
  8.  
  9. # リストに要素を挿入する関数
  10. def insertCell(head, key, value):
  11. # ここを埋める
  12. pass
  13.  
  14.  
  15. # リストからkeyに対応する要素を削除する関数
  16. def deleteCell(head, key):
  17. # ここを埋める
  18. pass
  19.  
  20.  
  21. # リストからkeyに対応する要素の値を得る関数
  22. def searchCell(head, key):
  23. # ここを埋める
  24. pass
  25.  
  26.  
  27. # リストを全てクリアする関数
  28. def clearCell(head):
  29. while head.next != None:
  30. tmp = head.next.next
  31. del head.next
  32. head.next = tmp
  33.  
  34. # リストを全て表示する関数
  35. def printCell(head) :
  36. text = "[ "
  37. while head.next != None:
  38. text += str(head.next.value)
  39. if head.next.next != None:
  40. text += ", "
  41. head = head.next
  42. text += " ]"
  43. print(text)
  44.  
  45. if __name__ == "__main__":
  46. # リストの先頭
  47. # Noneは次の要素が無いことを表す
  48. head = Cell(None, "head", -1)
  49.  
  50. insertCell(head, "one", 1)
  51. insertCell(head, "two", 2)
  52. insertCell(head, "three", 3)
  53. printCell(head); #[1, 2, 3]
  54.  
  55. deleteCell(head, "two");
  56. printCell(head) # [1, 3]
  57.  
  58. insertCell(head, "four", 4)
  59. insertCell(head, "five", 5)
  60. deleteCell(head, "five")
  61. printCell(head) # [1, 3, 4]
  62.  
  63. print(searchCell(head, "three")) # 3
  64. print(searchCell(head, "two")) # not found
  65.  
  66. clearCell(head)
  67. printCell(head) # []
  68.  
  69.  
Runtime error #stdin #stdout #stderr 0.17s 23464KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 50, in <module>
NameError: name 'insertCell' is not defined