python創建一個鏈表


moveodinameselurndata:

1#-*-加密= utf-8-*-

2#軟件:算法訓練

3#datetime: 2020/4/8 11:13:

4:

5類節點(對象)。

6:

7 def __init __(獨立地,data = None,next = none)。

8 self._value =數據

9個獨立:_ next = next

10

11 def get_value(個體):

12個獨立返回

13:

14 def get_next(個體):

15自行歸還

16:

17 def set_value(獨立地為new_data):

18 self._value = new_data:

19

20 def set_next(獨立地為new_next)。

21 self._next = new_next:

22:

23:

24類LinkList(對象)。

25

26 def __init __(獨立)。

27 self._head =節點()

28個獨立:_tail = self._head

29本身:長度= 0

30

31 Def頭(獨立)。

32“”“

33鏈表的第一個元素(頭節點除外)

34:歸還

35“”“

36,如果獨立的話:_head.get_next():

37自己返回:_head.get_next()

其他38人。

39返回節點()

40

41顫抖的尾巴(獨立地)。

42“”“

43鏈表中的最后一項

44:歸還

45“”“

46獨立返回:_tail:

47:

48 def is_empty(獨立)。

49“”“

50確定列表是否為空

51:歸還

52“”“

53返回的自我身份._ length == 0

54:

大小55(個體經營)。

56“”“

57鏈表的大小

58歸還

59“”“

60個獨立退貨

61:

62 def加(獨立地,值)。

63“”“

64從頭部插入環

65:參數值。

66:歸還

67“”“

68 new_node =節點(值)

69 new_node.set_next(self._head.get_next())

70獨立:_head.set_next(新節點)

71本人._ length + = 1

72:

73 def附件(獨立,價值)。

74“”“

75從尾部增加結

76:參數值。

77:歸還互聯網價格圖片大全

78“”“

79 new_node =節點(值)

80如果self.is_empty():

81 self._head.set_next(new_node)

其他82個。

83當前=自我

84到當前。Get_next():

85當前=當前。Get_next()

86 current.set_next(new_node)

87 self._tail =新節點:

88獨立:_長度+ = 1

89

90 Def(搜索,值)。

91“”“

92查找數據,返回-1或節點索引

93.參數值。

94.回歸。

95“”“

96當前=獨立._ head.get_next()

97計數= 0

當前之前的98。Get_next():

如果當前為99,則Get_value()==值。

100收益計算

101當前=當前。Get_next()

102個帳戶+ + 1

103返回-1

104:

105 Def刪除(自動,值)。

106“”“

107刪除下載返回此數據或-1

108:參數值。

109:歸還

110“”“

111當前=自我

112首先=無

當前之前的113。Get_next():

114,如果當前.Get_value()==值。

115,如果沒有提前。

116獨立:_head = current。Get_next()

其他117人。

118 pre.set_next(current。Get_next())

119本身:_ length- = 1:

120返回當前值。

121第一=當前

122當前=當前。Get_next()

另一個123。

124返回-1

125

126:

安裝127個默認值(獨立,索引,值)。

128“”“

129插入數據節點

130:參數索引。

131:參數值。

132返回。

133“”“

134如果索引= 1:

135個(值)

136 Elif索引> self.size():

137個自我提升(價值)

138。

139 new_node =節點(值)

140次計算= 0

141當前=自我

142首=無

143並計數索引:

144計算+ = 1

145第一=當前

146 current = current.Get_next()

147 pre.set_next(new_node)

148 new_node.set_next(當前)

149 self__長度+ = 1

150:

151:

152 if __name__ ==‘__main__”。

153 arr = [1、4、2、6、8、4、9]

154年印刷(“質量長度”,Len(arr))

155 link_list =鏈接()

156打印(“空列表大小”,Link_list.size())

157打印(“列表是否為空?”,Link_list.is_empty())

158打印(“空列表中的第一個數據。”,Link_list.head()。Get_value())

159打印(“最新列表數據為空白。”,Link_list.tail()。Get_value())

160 for me arr。

161 link_list.append(i)

162:

163#添加:

164 link_list.append(10)

165 link_list.add(10)

166 link_list.insert(1、11)

167 link_list.insert(3,12)

168 link_list.insert(7,13)

169打印(“已刪除的元素”,Link_list.remove(1))

170 link_list.append(1)

171 link_list.add(10)

172:

173當前= link_list.head()

174 l =“”

當前為175,Get_next():

176 l + =金錢(當前。Get_value())+“”

177 current = current.Get_next()

178 l + = money(current。Get_value())

179年印刷(“檢查鏈表的值”,jb)

180次打印(“鏈接的尺寸列表”),Link_list.size())

181打印(“搜索鏈接列表。”,Link_list.search(8))

182打印(“鏈接列表中的第一個數據。”,Link_list.head()。Get_value())

183打印(“最近列出的數據”),Link_list.tail()。Get_value())

Python創建鏈接列表moveodinameselurndata

JS


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM