Python實現(xiàn)數(shù)據(jù)結(jié)構(gòu)線性鏈表(單鏈表)算法示例
本文實例講述了Python實現(xiàn)數(shù)據(jù)結(jié)構(gòu)線性鏈表(單鏈表)算法。分享給大家供大家參考,具體如下:
初學python,拿數(shù)據(jù)結(jié)構(gòu)中的線性鏈表存儲結(jié)構(gòu)練練手,理論比較簡單,直接上代碼。
#!/usr/bin/python
# -*- coding:utf-8 -*-
# Author: Hui
# Date: 2017-10-13
# 結(jié)點類,
class Node:
def __init__(self, data):
self.data = data # 數(shù)據(jù)域
self.next = None # 指針域
def get_data(self):
return self.data
# 鏈表類
class List:
def __init__(self, head):
self.head = head # 默認初始化頭結(jié)點
def is_empty(self): # 空鏈表判斷
return self.get_len() == 0
def get_len(self): # 返回鏈表長度
length = 0
temp = self.head
while temp is not None:
length += 1
temp = temp.next
return length
def append(self, node): # 追加結(jié)點(鏈表尾部追加)
temp = self.head
while temp.next is not None:
temp = temp.next
temp.next = node
def delete(self, index): # 刪除結(jié)點
if index < 1 or index > self.get_len():
print "給定位置不合理"
return
if index == 1:
self.head = self.head.next
return
temp = self.head
cur_pos = 0
while temp is not None:
cur_pos += 1
if cur_pos == index-1:
temp.next = temp.next.next
temp = temp.next
def insert(self, pos, node): # 插入結(jié)點
if pos < 1 or pos > self.get_len():
print "插入結(jié)點位置不合理..."
return
temp = self.head
cur_pos = 0
while temp is not Node:
cur_pos += 1
if cur_pos == pos-1:
node.next = temp.next
temp.next =node
break
temp = temp.next
def reverse(self, head): # 反轉(zhuǎn)鏈表
if head is None and head.next is None:
return head
pre = head
cur = head.next
while cur is not None:
temp = cur.next
cur.next = pre
pre = cur
cur = temp
head.next = None
return pre
def print_list(self, head): # 打印鏈表
init_data = []
while head is not None:
init_data.append(head.get_data())
head = head.next
return init_data
if __name__ == '__main__':
head = Node("head")
list = List(head)
print '初始化頭結(jié)點:\t', list.print_list(head)
for i in range(1, 10):
node = Node(i)
list.append(node)
print '鏈表添加元素:\t', list.print_list(head)
print '鏈表是否空:\t', list.is_empty()
print '鏈表長度:\t', list.get_len()
list.delete(9)
print '刪除第9個元素:\t',list.print_list(head)
node = Node("insert")
list.insert(3, node)
print '第3個位置插入‘insert'字符串 :\t', list.print_list(head)
head = list.reverse(head)
print '鏈表反轉(zhuǎn):', list.print_list(head)
執(zhí)行結(jié)果:

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python加密解密算法與技巧總結(jié)》、《Python編碼操作技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
- python單鏈表實現(xiàn)代碼實例
- 單鏈表反轉(zhuǎn)python實現(xiàn)代碼示例
- Python實現(xiàn)針對給定單鏈表刪除指定節(jié)點的方法
- Python數(shù)據(jù)結(jié)構(gòu)與算法之鏈表定義與用法實例詳解【單鏈表、循環(huán)鏈表】
- Python3實現(xiàn)的反轉(zhuǎn)單鏈表算法示例
- python環(huán)形單鏈表的約瑟夫問題詳解
- 使用python實現(xiàn)鏈表操作
- python雙向鏈表實現(xiàn)實例代碼
- python數(shù)據(jù)結(jié)構(gòu)鏈表之單向鏈表(實例講解)
- Python單鏈表原理與實現(xiàn)方法詳解
相關(guān)文章
詳解python實現(xiàn)識別手寫MNIST數(shù)字集的程序
這篇文章主要介紹了詳解python實現(xiàn)識別手寫MNIST數(shù)字集的程序,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08
python利用pandas和csv包向一個csv文件寫入或追加數(shù)據(jù)
這篇文章主要給大家介紹了關(guān)于python利用pandas和csv包向一個csv文件寫入或追加數(shù)據(jù)的相關(guān)資料,我們越來越多的使用pandas進行數(shù)據(jù)處理,有時需要向一個已經(jīng)存在的csv文件寫入數(shù)據(jù),需要的朋友可以參考下2023-07-07
pytorch/transformers?最后一層不加激活函數(shù)的原因分析
這里給大家解釋一下為什么bert模型最后都不加激活函數(shù),是因為損失函數(shù)選擇的原因,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-01-01
Python實現(xiàn)接口自動化封裝導出excel和讀寫excel數(shù)據(jù)
這篇文章主要為大家詳細介紹了Python如何實現(xiàn)接口自動化封裝導出excel和讀寫excel數(shù)據(jù),文中的示例代碼簡潔易懂,希望對大家有所幫助2023-07-07
Python爬蟲之獲取心知天氣API實時天氣數(shù)據(jù)并彈窗提醒
今天我們來學習如何獲取心知天氣API實時天氣數(shù)據(jù),制作彈窗提醒,并設(shè)置成自啟動項目.文中有非常詳細的代碼示例及介紹,對正在學習python的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-05-05

