Python單鏈表的簡(jiǎn)單實(shí)現(xiàn)方法
本文實(shí)例講述了Python單鏈表的簡(jiǎn)單實(shí)現(xiàn)方法,分享給大家供大家參考。具體方法如下:
通常來(lái)說(shuō),要定義一個(gè)單鏈表,首先定義鏈表元素:Element.它包含3個(gè)字段:
list:標(biāo)識(shí)自己屬于哪一個(gè)list
datum:改元素的value
next:下一個(gè)節(jié)點(diǎn)的位置
具體實(shí)現(xiàn)代碼如下:
class LinkedList(object):
class Element(object):
def __init__(self,list,datum,next):
self._list = list
self._datum = datum
self._next = next
def getDatum(self):
return self._datum
datum = property(
fget = lambda self: self.getDatum())
def getNext(self):
return self._next
next = property(
fget = lambda self: self.getNext())
def __init__(self):
self._head = None
self._tail = None
def getHead(self):
return self._head
head = property(
fget = lambda self: self.getHead())
def prepend(self,item):
tmp = self.Element (self,item,self._head)
if self._head is None:
self._tail = tmp
self._head = tmp
def insert(self, pos, item):
i = 0
p = self._head
while p != None and i < pos -1:
p = p._next
i += 1
if p == None or i > pos-1:
return -1
tmp = self.Element(self, item, p._next)
p._next = tmp
return 1
def getItem(self, pos):
i = 0
p = self._head
while p != None and i < pos -1:
p = p._next
i += 1
if p == None or i > post-1:
return -1
return p._datum
def delete(self, pos):
i = 0
p = self._head
while p != None and i < pos -1:
p = p._next
i += 1
if p == None or i > post-1:
return -1
q = p._next
p._nex = q._next
datum = p._datum
return datum
def setItem(self, pos, item):
i = 0
p = self._head
while p != None and i < pos -1:
p = p._next
i += 1
if p == None or i > post-1:
return -1
p._datum = item
return 1
def find(self, pos, item):
i = 0
p = self._head
while p != None and i < pos -1:
if p._datum == item:
return 1
p = p._next
i += 1
return -1
def empty(self):
if self._head == None:
return 1
return 0
def size(self):
i = 0
p = self._head
while p != None and i < pos -1:
p = p._next
i += 1
return i
def clear(self):
self._head = None
self._tail = None
test = LinkedList()
test.prepend('test0')
print test.insert(1, 'test')
print test.head.datum
print test.head.next.datum
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
- 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)的數(shù)據(jù)結(jié)構(gòu)與算法之鏈表詳解
- 淺談Python單向鏈表的實(shí)現(xiàn)
- Python數(shù)據(jù)結(jié)構(gòu)與算法之列表(鏈表,linked list)簡(jiǎn)單實(shí)現(xiàn)
- Python實(shí)現(xiàn)針對(duì)給定單鏈表刪除指定節(jié)點(diǎn)的方法
- python雙向鏈表實(shí)現(xiàn)實(shí)例代碼
- python實(shí)現(xiàn)雙鏈表
相關(guān)文章
Python XML模塊數(shù)據(jù)解析與生成利器的使用掌握
這篇文章主要為大家介紹了Python XML模塊數(shù)據(jù)解析與生成利器的使用實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
Python光學(xué)仿真學(xué)習(xí)處理高斯光束分布圖像
這篇文章主要為大家介紹了Python光學(xué)仿真學(xué)習(xí)之如何處理高斯光束的分布圖像,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-10-10
Python編程中flask的簡(jiǎn)介與簡(jiǎn)單使用
今天小編就為大家分享一篇關(guān)于Python編程中flask的簡(jiǎn)介與簡(jiǎn)單使用,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
python入門:這篇文章帶你直接學(xué)會(huì)python
本教程并未涵蓋Python語(yǔ)言的全部?jī)?nèi)容,只是一個(gè)入門的教程,Python有非常多的庫(kù)以及很多的功能特點(diǎn)需要學(xué)習(xí),小編只是拋磚引玉,希望大家可以從中受益2018-09-09
Python實(shí)現(xiàn)的一個(gè)簡(jiǎn)單LRU cache
這篇文章主要介紹了Python實(shí)現(xiàn)的一個(gè)簡(jiǎn)單LRU cache,本文根據(jù)實(shí)際需求總結(jié)而來(lái),需要的朋友可以參考下2014-09-09

