Python實(shí)現(xiàn)的數(shù)據(jù)結(jié)構(gòu)與算法之鏈表詳解
本文實(shí)例講述了Python實(shí)現(xiàn)的數(shù)據(jù)結(jié)構(gòu)與算法之鏈表。分享給大家供大家參考。具體分析如下:
一、概述
鏈表(linked list)是一組數(shù)據(jù)項的集合,其中每個數(shù)據(jù)項都是一個節(jié)點(diǎn)的一部分,每個節(jié)點(diǎn)還包含指向下一個節(jié)點(diǎn)的鏈接。
根據(jù)結(jié)構(gòu)的不同,鏈表可以分為單向鏈表、單向循環(huán)鏈表、雙向鏈表、雙向循環(huán)鏈表等。其中,單向鏈表和單向循環(huán)鏈表的結(jié)構(gòu)如下圖所示:

二、ADT
這里只考慮單向循環(huán)鏈表ADT,其他類型的鏈表ADT大同小異。單向循環(huán)鏈表ADT(抽象數(shù)據(jù)類型)一般提供以下接口:
① SinCycLinkedlist() 創(chuàng)建單向循環(huán)鏈表
② add(item) 向鏈表中插入數(shù)據(jù)項
③ remove(item) 刪除鏈表中的數(shù)據(jù)項
④ search(item) 在鏈表中查找數(shù)據(jù)項是否存在
⑤ empty() 判斷鏈表是否為空
⑥ size() 返回鏈表中數(shù)據(jù)項的個數(shù)
單向循環(huán)鏈表操作的示意圖如下:

三、Python實(shí)現(xiàn)
Python的內(nèi)建類型list底層是由C數(shù)組實(shí)現(xiàn)的,list在功能上更接近C++的vector(因為可以動態(tài)調(diào)整數(shù)組大小)。我們都知道,數(shù)組是連續(xù)列表,鏈表是鏈接列表,二者在概念和結(jié)構(gòu)上完全不同,因此list不能用于實(shí)現(xiàn)鏈表。
在C/C++中,通常采用“指針+結(jié)構(gòu)體”來實(shí)現(xiàn)鏈表;而在Python中,則可以采用“引用+類”來實(shí)現(xiàn)鏈表。在下面的代碼中,SinCycLinkedlist類代表單向循環(huán)鏈表,Node類代表鏈表中的一個節(jié)點(diǎn):
#!/usr/bin/env python
# -*- coding: utf-8 -*-
class Node:
def __init__(self, initdata):
self.__data = initdata
self.__next = None
def getData(self):
return self.__data
def getNext(self):
return self.__next
def setData(self, newdata):
self.__data = newdata
def setNext(self, newnext):
self.__next = newnext
class SinCycLinkedlist:
def __init__(self):
self.head = Node(None)
self.head.setNext(self.head)
def add(self, item):
temp = Node(item)
temp.setNext(self.head.getNext())
self.head.setNext(temp)
def remove(self, item):
prev = self.head
while prev.getNext() != self.head:
cur = prev.getNext()
if cur.getData() == item:
prev.setNext(cur.getNext())
prev = prev.getNext()
def search(self, item):
cur = self.head.getNext()
while cur != self.head:
if cur.getData() == item:
return True
cur = cur.getNext()
return False
def empty(self):
return self.head.getNext() == self.head
def size(self):
count = 0
cur = self.head.getNext()
while cur != self.head:
count += 1
cur = cur.getNext()
return count
if __name__ == '__main__':
s = SinCycLinkedlist()
print('s.empty() == %s, s.size() == %s' % (s.empty(), s.size()))
s.add(19)
s.add(86)
print('s.empty() == %s, s.size() == %s' % (s.empty(), s.size()))
print('86 is%s in s' % ('' if s.search(86) else ' not',))
print('4 is%s in s' % ('' if s.search(4) else ' not',))
print('s.empty() == %s, s.size() == %s' % (s.empty(), s.size()))
s.remove(19)
print('s.empty() == %s, s.size() == %s' % (s.empty(), s.size()))
運(yùn)行結(jié)果:
$ python sincyclinkedlist.py s.empty() == True, s.size() == 0 s.empty() == False, s.size() == 2 86 is in s 4 is not in s s.empty() == False, s.size() == 2 s.empty() == False, s.size() == 1
希望本文所述對大家的Python程序設(shè)計有所幫助。
- Python實(shí)現(xiàn)環(huán)形鏈表
- Python3實(shí)現(xiàn)的判斷環(huán)形鏈表算法示例
- 使用python實(shí)現(xiàn)鏈表操作
- python單鏈表實(shí)現(xiàn)代碼實(shí)例
- 淺談Python單向鏈表的實(shí)現(xiàn)
- Python單鏈表的簡單實(shí)現(xiàn)方法
- Python數(shù)據(jù)結(jié)構(gòu)與算法之列表(鏈表,linked list)簡單實(shí)現(xiàn)
- Python實(shí)現(xiàn)針對給定單鏈表刪除指定節(jié)點(diǎn)的方法
- python雙向鏈表實(shí)現(xiàn)實(shí)例代碼
- python實(shí)現(xiàn)雙鏈表
相關(guān)文章
Python?Asyncio?庫之同步原語常用函數(shù)詳解
這篇文章主要為大家介紹了Python?Asyncio?庫之同步原語常用函數(shù)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
django model去掉unique_together報錯的解決方案
本文給大家分享的是在使用django model去掉unique_together時報錯的解決思路和具體步驟,提供給大家參考下,希望對大家學(xué)習(xí)使用django能夠有所幫助2016-10-10
pandas?數(shù)據(jù)透視和逆透視的實(shí)現(xiàn)
本文介紹了pandas?數(shù)據(jù)透視和逆透視的實(shí)現(xiàn),包含pivot()方法透視及pivot_table()方法逆透視,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-12-12
numpy array找出符合條件的數(shù)并賦值的示例代碼
本文主要介紹了numpy array找出符合條件的數(shù)并賦值的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
python使用HTMLTestRunner導(dǎo)出餅圖分析報告的方法
這篇文章主要介紹了python使用HTMLTestRunner導(dǎo)出餅圖分析報告的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Python和OpenCV庫實(shí)現(xiàn)識別人物出現(xiàn)并鎖定
本文主要介紹了Python和OpenCV庫實(shí)現(xiàn)識別人物出現(xiàn)并鎖定,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04

