Python實現(xiàn)數(shù)據(jù)結(jié)構(gòu)線性鏈表(單鏈表)算法示例
本文實例講述了Python實現(xiàn)數(shù)據(jù)結(jié)構(gòu)線性鏈表(單鏈表)算法。分享給大家供大家參考,具體如下:
初學(xué)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 游戲大作炫酷機甲闖關(guān)游戲爆肝數(shù)千行代碼實現(xiàn)案例進階
本篇文章給大家?guī)鞵ython的一個游戲大制作—機甲闖關(guān)冒險,數(shù)千行代碼實現(xiàn)的游戲,過程很詳細,對大家的學(xué)習(xí)或工作具有一定的借鑒價值,需要的朋友可以參考下2021-10-10
python連接PostgreSQL數(shù)據(jù)庫的過程詳解
這篇文章主要介紹了python連接PostgreSQL數(shù)據(jù)庫的過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-09-09
Python實現(xiàn)GB格式序列文件轉(zhuǎn)換Fasta格式文件
這篇文章主要為大家介紹了Python實現(xiàn)GB格式序列文件轉(zhuǎn)換Fasta格式文件示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-07-07

