python雙向循環鏈表實例詳解_第1頁
python雙向循環鏈表實例詳解_第2頁
python雙向循環鏈表實例詳解_第3頁
python雙向循環鏈表實例詳解_第4頁
python雙向循環鏈表實例詳解_第5頁
已閱讀5頁,還剩5頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

第python雙向循環鏈表實例詳解使用python實現雙向循環鏈表,供大家參考,具體內容如下

雙向循環鏈表:將所有的數據存放到節點中,每一個節點相連接,首尾鏈接,

每一個節點中有一個數據存儲區,和兩個鏈接區,一個鏈接前一個節點,一個鏈接下一個節點

雙向鏈表操作

1、鏈表是否為空

2、鏈表的長度

3、遍歷鏈表

4、鏈表頭部添加元素

5、鏈表尾部添加元素

6、鏈表指定位置添加元素

7、鏈表刪除節點

8、查找節點是否存在

代碼實現

#Functions

函數聲明

classNode():

"""實例化節點類"""

def__init__(self,item):

self.item=item

self.prev=None

self.next=None

classLinklist():

"""

存放節點類

"""

def__init__(self):

self.head=None

#1.鏈表是否為空

defis_empty(self):

returnself.head==None

#2.鏈表的長度

deflength(self):

"""

返回鏈表中所有數據的個數

實例化游標,遍歷鏈表,使用計數器自增一

空鏈表

"""

#實例化游標

cur=self.head

#判斷是否為空

ifself.is_empty():

return0

else:

#不為空

#定義計數

count=1

whilecur.next!=self.head:

count+=1

cur=cur.next

returncount

pass

#3.遍歷鏈表

deftravel(self):

"""

遍歷鏈表

實例化游標,遍歷鏈表,每次輸出節點的數據

空鏈表

只有頭節點

"""

#實例化游標

cur=self.head

#判斷是否為空

ifself.is_empty():

returnNone

else:

#不為空的情況

whilecur.next!=self.head:

print(cur.item,end='')

cur=cur.next

print(cur.item)

pass

#4.鏈表頭部添加元素

defadd(self,item):

"""

頭節點添加

實例化節點,

"""

#實例化節點

node=Node(item)

#實例化游標

cur=self.head

#判斷是否為空

ifself.is_empty():

node.next=node

node.prev=node

self.head=node

else:

#鏈表不為空的情況

#只有一個節點的情況

#node.next=self.head

node.next=cur

node.prev=cur

ifcur.next==self.head:

#print(cur.item)

cur.prev=node

cur.next=node

self.head=node

elifcur.next!=self.head:

pro=self.head

whilecur.next!=self.head:

cur=cur.next

pro.prev=node

cur.next=node

self.head=node

pass

#5.鏈表尾部添加元素

defappend(self,item):

"""

鏈表尾部添加數據

實例化節點,實例化游標,指向尾部節點,修改指向

鏈表為空

"""

#實例化節點

node=Node(item)

#實例化游標

cur=self.head

ifself.is_empty():

self.add(item)

else:

#不為空的情況

#指針指向最后一個節點

self.head.prev=node

node.next=self.head

whilecur.next!=self.head:

cur=cur.next

node.prev=cur

cur.next=node

pass

#6.鏈表指定位置添加元素

definsert(self,index,item):

"""

指定位置添加數據

實例化節點,實例化游標

移動游標到索引位置,更改指向

輸入索引大小判斷

鏈表是否為空

"""

#實例化節點

node=Node(item)

#實例化游標

cur=self.head

ifindex=0:

self.add(item)

elifindex(self.length()-1):

self.append(item)

else:

#中間添加數據

#聲明計數

count=0

print(index)

whilecountindex-1:

count+=1

cur=cur.next

#print(cur.item)

node.next=cur.next

node.prev=cur

cur.next.prev=node

cur.next=node

pass

#7.鏈表刪除節點

defremove(self,item):

"""

刪除數據

實例化游標,遍歷鏈表,查找有沒有改數據

有,對改數據兩側的節點進行指向修改

"""

#實例化游標

cur=self.head

#判斷是否為空

ifself.is_empty():

returnNone

else:

#不為空的情況下

#如果刪除的是頭節點

ifcur.item==item:

#如果只有一個頭節點

ifcur.next==self.head:

self.head=None

else:

#self.head=cur.next

pro=cur.next

whilecur.next!=self.head:

cur=cur.next

cur.next=pro

pro.prev=cur

self.head=pro

pass

else:

whilecur.next!=self.head:

ifcur.item==item:

#print(cur.item)

pro=cur.prev

nex=cur.next

pro.next=cur.next

nex.prev=pro

returnTrue

else:

cur=cur.next

#如果是最后一個節點

ifcur.item==item:

cur.prev.next=self.head

self.head.prev=cur.prev

#8.查找節點是否存在

defsearch(self,item):

"""

查詢指定的數據是否存在

實例化游標

遍歷所有的節點。每個節點中判斷數據是否相等,相等,返回True

"""

#實例化游標

cur=self.head

#判斷是否為空

ifself.is_empty():

returnNone

else:

#不為空的情況

#遍歷所有的節點

whilecur.next!=self.head:

ifcur.item==item:

returnTrue

else:

cur=cur.next

ifcur.item==item:

returnTrue

pass

測試運行

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論