Python 中鏈表的個(gè)人理解

鏈表組成

Python 中鏈表由 head、 節(jié)點(diǎn)、tail、 三部分組成。

1.節(jié)點(diǎn)為Python 鏈表中最重要的部分,通過構(gòu)建class Node()類,節(jié)點(diǎn)引入并存儲(chǔ)value和next變量,其中value為Node中存儲(chǔ)的鏈表內(nèi)容,next為Node中存儲(chǔ)的指針,指向下一個(gè)Node。即Node由指針域next和結(jié)構(gòu)域value構(gòu)成。

2.鏈表由上述Node連結(jié)而成,其中head指向鏈表的第一個(gè)節(jié)點(diǎn),tail指向鏈表最后一個(gè)節(jié)點(diǎn)。

3.tail指向的尾端Node的next指向(存儲(chǔ))None,即該Node的指針域存儲(chǔ) None的內(nèi)存地址。

示例代碼:

? ? class Node():

? ? def __init__(self, context=None, next=None):

? ? ? ? self._context = context? # 提高代碼的健壯性? 類似Java 的處理 需要定義函數(shù)獲取參數(shù)數(shù)據(jù)

? ? ? ? self._next = next

? ? def getContext(self):

? ? ? ? return self._context

? ? def getNext(self):

? ? ? ? return self._next

? ? def setContext(self, newContext):

? ? ? ? self._context = newContext

? ? def setNext(self, newNext):

? ? ? ? self._next = newNext

? ? class LinkedList():

? ? def __init__(self):

? ? ? ? self._head = None

? ? ? ? self._tail = None

? ? ? ? self._length = 0

? ? def isempty(self):

? ? ? ? return self._head is None

? ? def add(self, newadding):

? ? ? ? newNode = Node()

? ? ? ? newNode.setContext(newadding)

? ? ? ? newNode.setNext(self._head)

? ? ? ? self._head = newNode

? ? ? ? if self._tail is None:

? ? ? ? ? ? self._tail = newNode

? ? ? ? self._length += 1

? ? def append(self, newAppending):

? ? ? ? newANode = Node()

? ? ? ? newANode.setContext(newAppending)

? ? ? ? if self._head is None:

? ? ? ? ? ? self._tail = newANode

? ? ? ? ? ? self._head = newANode

? ? ? ? else:

? ? ? ? ? ? self._tail.setNext(newANode)

? ? ? ? ? ? self._tail = newANode

? ? ? ? self._length += 1

? ? def remove(self, context):

? ? ? ? previous = None

? ? ? ? current = self._head

? ? ? ? while current is not None:

? ? ? ? ? ? if current.getContext() == context:

? ? ? ? ? ? ? ? if not previous:

? ? ? ? ? ? ? ? ? ? self._head = current.getNext()

? ? ? ? ? ? ? ? else:

? ? ? ? ? ? ? ? ? ? previous.setNext(current.getNext())

? ? ? ? ? ? ? ? break

? ? ? ? ? ? else:

? ? ? ? ? ? ? ? previous = current

? ? ? ? ? ? ? ? current = current.getNext()

? ? def search(self, context):

? ? ? ? current = self._head

? ? ? ? result = False

? ? ? ? while current is not None and not result:

? ? ? ? ? ? if current.getContext() == context:

? ? ? ? ? ? ? ? result = True

? ? ? ? ? ? else:

? ? ? ? ? ? ? ? current = current.getNext()

? ? ? ? return result

? ? def items(self):

? ? ? ? cur = self._head

? ? ? ? while cur is not None:

? ? ? ? ? ? yield cur.getContext()

? ? ? ? ? ? cur = cur.getNext()

? ? @property

? ? def tail(self):

? ? ? ? return self._tail

? ? if __name__ == '__main__':

? ? LL = LinkedList()

? ? print(LL.isempty())

? ? LL.add(1)

? ? LL.add(2)

? ? print(LL._tail.getContext())

? ? print(LL)

? ? for i in LL.items():

? ? ? ? print(i)

? ? print(LL.search(2))

? ? LL.append(3)

? ? for i in LL.items():

? ? ? ? print(i)

? ? LL.remove(1)

? ? for i in LL.items():

? ? ? ? print(i)

? ? print(LL.search(1))

? ? print(LL._tail.getContext())

上述示例運(yùn)行結(jié)果如下:

? ? True

? ? 1

? ? <__main__.LinkedList object at 0x000002157A80BFD0>

? ? 2

? ? 1

? ? True

? ? 2

? ? 1

? ? 3

? ? 2

? ? 3

? ? False

? ? 3

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容