python單鏈表實(shí)現(xiàn)代碼實(shí)例
鏈表的定義:
鏈表(linked list)是由一組被稱為結(jié)點(diǎn)的數(shù)據(jù)元素組成的數(shù)據(jù)結(jié)構(gòu),每個(gè)結(jié)點(diǎn)都包含結(jié)點(diǎn)本身的信息和指向下一個(gè)結(jié)點(diǎn)的地址。由于每個(gè)結(jié)點(diǎn)都包含了可以鏈接起來的地址信息,所以用一個(gè)變量就能夠訪問整個(gè)結(jié)點(diǎn)序列。也就是說,結(jié)點(diǎn)包含兩部分信息:一部分用于存儲數(shù)據(jù)元素的值,稱為信息域;另一部分用于存儲下一個(gè)數(shù)據(jù)元素地址的指針,稱為指針域。鏈表中的第一個(gè)結(jié)點(diǎn)的地址存儲在一個(gè)單獨(dú)的結(jié)點(diǎn)中,稱為頭結(jié)點(diǎn)或首結(jié)點(diǎn)。鏈表中的最后一個(gè)結(jié)點(diǎn)沒有后繼元素,其指針域?yàn)榭铡! ?/P>
python單鏈表實(shí)現(xiàn)代碼:
#!/usr/bin/python
# -*- coding: utf-8 -*-
class Node(object):
def __init__(self,val,p=0):
self.data = val
self.next = p
class LinkList(object):
def __init__(self):
self.head = 0
def __getitem__(self, key):
if self.is_empty():
print 'linklist is empty.'
return
elif key <0 or key > self.getlength():
print 'the given key is error'
return
else:
return self.getitem(key)
def __setitem__(self, key, value):
if self.is_empty():
print 'linklist is empty.'
return
elif key <0 or key > self.getlength():
print 'the given key is error'
return
else:
self.delete(key)
return self.insert(key)
def initlist(self,data):
self.head = Node(data[0])
p = self.head
for i in data[1:]:
node = Node(i)
p.next = node
p = p.next
def getlength(self):
p = self.head
length = 0
while p!=0:
length+=1
p = p.next
return length
def is_empty(self):
if self.getlength() ==0:
return True
else:
return False
def clear(self):
self.head = 0
def append(self,item):
q = Node(item)
if self.head ==0:
self.head = q
else:
p = self.head
while p.next!=0:
p = p.next
p.next = q
def getitem(self,index):
if self.is_empty():
print 'Linklist is empty.'
return
j = 0
p = self.head
while p.next!=0 and j <index:
p = p.next
j+=1
if j ==index:
return p.data
else:
print 'target is not exist!'
def insert(self,index,item):
if self.is_empty() or index<0 or index >self.getlength():
print 'Linklist is empty.'
return
if index ==0:
q = Node(item,self.head)
self.head = q
p = self.head
post = self.head
j = 0
while p.next!=0 and j<index:
post = p
p = p.next
j+=1
if index ==j:
q = Node(item,p)
post.next = q
q.next = p
def delete(self,index):
if self.is_empty() or index<0 or index >self.getlength():
print 'Linklist is empty.'
return
if index ==0:
q = Node(item,self.head)
self.head = q
p = self.head
post = self.head
j = 0
while p.next!=0 and j<index:
post = p
p = p.next
j+=1
if index ==j:
post.next = p.next
def index(self,value):
if self.is_empty():
print 'Linklist is empty.'
return
p = self.head
i = 0
while p.next!=0 and not p.data ==value:
p = p.next
i+=1
if p.data == value:
return i
else:
return -1
l = LinkList()
l.initlist([1,2,3,4,5])
print l.getitem(4)
l.append(6)
print l.getitem(5)
l.insert(4,40)
print l.getitem(3)
print l.getitem(4)
print l.getitem(5)
l.delete(5)
print l.getitem(5)
l.index(5)
結(jié)果:
5
6
4
40
5
6
- Python實(shí)現(xiàn)環(huán)形鏈表
- Python3實(shí)現(xiàn)的判斷環(huán)形鏈表算法示例
- 使用python實(shí)現(xiàn)鏈表操作
- Python實(shí)現(xiàn)的數(shù)據(jù)結(jié)構(gòu)與算法之鏈表詳解
- 淺談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監(jiān)控主機(jī)是否存活并以郵件報(bào)警
本文是利用python腳本寫的簡單測試主機(jī)是否存活,此腳本有個(gè)缺點(diǎn)不適用線上,由于網(wǎng)絡(luò)延遲、丟包現(xiàn)象會造成誤報(bào)郵件,感興趣的朋友一起看看Python監(jiān)控主機(jī)是否存活并以郵件報(bào)警吧2015-09-09PyCharm連接遠(yuǎn)程服務(wù)器的超級詳細(xì)教程
Pycharm可以與服務(wù)器建立連接,把相應(yīng)的項(xiàng)目同步到服務(wù)器上,下面這篇文章主要給大家介紹了關(guān)于PyCharm連接遠(yuǎn)程服務(wù)器的超級詳細(xì)教程,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12python實(shí)現(xiàn)一個(gè)簡單的ping工具方法
今天小編就為大家分享一篇python實(shí)現(xiàn)一個(gè)簡單的ping工具方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01Python的Twisted框架中使用Deferred對象來管理回調(diào)函數(shù)
當(dāng)說起Twisted的異步與非阻塞模式等特性時(shí),回調(diào)函數(shù)的使用在其中自然就顯得不可或缺,接下來我們就來看Python的Twisted框架中使用Deferred對象來管理回調(diào)函數(shù)的用法.2016-05-05詳解Python中的from..import絕對導(dǎo)入語句
絕對導(dǎo)入其實(shí)非常簡單,即是用from語句在import前指明頂層package名,下面我們通過兩個(gè)例子來詳解Python中的from..import絕對導(dǎo)入語句2016-06-06python實(shí)現(xiàn)寫數(shù)字文件名的遞增保存文件方法
今天小編就為大家分享一篇python實(shí)現(xiàn)寫數(shù)字文件名的遞增保存文件方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10使用Djongo模塊在Django中使用MongoDB數(shù)據(jù)庫
Django框架為我們提供了簡潔方便的ORM模型供我們對數(shù)據(jù)庫進(jìn)行各種操作,但是這個(gè)“數(shù)據(jù)庫”卻并不包括NoSQL的典型——MongoDB。不少Django初學(xué)者也會到處詢問,如何才能在Django中使用MongoDB。本文將介紹使用Djongo來在Django中集成MongoDB數(shù)據(jù)庫2021-06-06python通過函數(shù)屬性實(shí)現(xiàn)全局變量的方法
這篇文章主要介紹了python通過函數(shù)屬性實(shí)現(xiàn)全局變量的方法,實(shí)例分析了Python中函數(shù)屬性的相關(guān)使用技巧,需要的朋友可以參考下2015-05-05