fork download
  1. class Node:
  2. def __init__(self):
  3. self.data = None
  4. self.next = None
  5. self.prev = None
  6.  
  7. def CreateList():
  8. front = None
  9. back = None
  10. arr = [1,2,3,4,5,6,7]
  11. for num in arr:
  12. if front is None:
  13. c = Node()
  14. c.data = num
  15. c.next = None
  16. c.prev = None
  17. front = back = c
  18. else:
  19. c = Node()
  20. c.data = num
  21. c.next = None
  22. c.prev = back
  23. back.next = c
  24. back = c
  25.  
  26. return [front, back]
  27.  
  28. def display_left(front):
  29. c = front
  30. while c:
  31. print(c.data, end =" ")
  32. c = c.next
  33. print()
  34.  
  35. def display_right(back):
  36. c = back
  37. while c:
  38. print(c.data, end =" ")
  39. c = c.prev
  40. print()
  41.  
  42. def remove(front, back, remove_node):
  43.  
  44. if front.data == remove_node:
  45. tmp = front
  46. front = front.next
  47. if front:
  48. front.prev = None
  49. del tmp
  50.  
  51. elif back.data == remove_node:
  52. tmp = back
  53. back = back.prev
  54. if back:
  55. back.next = None
  56. del tmp
  57. else:
  58. c = front
  59. while c.data != remove_node:
  60. c = c.next
  61. c.prev.next = c.next
  62. c.next.prev = c.prev
  63. del c
  64. return front, back
  65.  
  66. def main():
  67. list = CreateList()
  68. front = list[0]
  69. back = list[1]
  70. display_left(front)
  71. display_right(back)
  72. remove_node = 1
  73. front, back = remove(front, back, remove_node)
  74. print("remove:%d\n"%remove_node)
  75. display_left(front)
  76. display_right(back)
  77. remove_node = 7
  78. front, back = remove(front, back, remove_node)
  79. print("remove:%d\n"%remove_node)
  80. display_left(front)
  81. display_right(back)
  82. remove_node = 4
  83. front, back = remove(front, back, remove_node)
  84. print("remove:%d\n"%remove_node)
  85. display_left(front)
  86. display_right(back)
  87.  
  88. if __name__ == "__main__":
  89. main()
  90.  
Success #stdin #stdout 0.03s 9592KB
stdin
Standard input is empty
stdout
1 2 3 4 5 6 7 
7 6 5 4 3 2 1 
remove:1

2 3 4 5 6 7 
7 6 5 4 3 2 
remove:7

2 3 4 5 6 
6 5 4 3 2 
remove:4

2 3 5 6 
6 5 3 2