我有一個作業程式可以洗掉鏈表的第一個元素。我不確定為什么 self._first = self._first.next 行會洗掉串列的第一個元素。有人可以為我澄清一下嗎?
def remove_first(self) -> Any:
curr = self._first
if curr is None:
raise IndexError
else:
first = curr.item
self._first = self._first.next
return first
uj5u.com熱心網友回復:
看起來你的鏈表是這樣實作的:
List .first --> Item #1 .next ---> Item#2 .next --> Item#3 等等
代碼將類的.first成員變數重新分配List為指向Item#2. 因此,如果您從 開始遍歷串列,.first則Item#1不再“在串列中”。它還遞減 refcount,Item#1當 refcount 變為零時,將為其解除分配。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/414122.html
標籤:
上一篇:如何從串列中取出字典
下一篇:將串列作為字串列印在一行上
