python實現單鏈表

2021-09-02 12:38:03 字數 4273 閱讀 9837

[code="python"][/code]

# coding:utf-8

class node:

def __init__(self, value):

self.data = value

self.next = none

class linklist:

def __init__(self, data=[0]):

self.head = none

self.init_link_list(data)

# 初始化鍊錶

# data 為陣列

def init_link_list(self, data):

if len(data) == 0:

print("initialization data is null")

return

self.head = node(data[0])

current = self.head

for index in data[1:]:

current.next = node(index)

current = current.next

# 獲取當前結點

def get_node(self, index):

if self.is_empty():

print("link is empty")

return

if index > self.get_length() or index <= 0:

print("node is not exist")

return

current = self.head

i = 0

while i < index:

if i == index - 1:

return current

current = current.next

i += 1

# 獲取當前元素的值

def get_data(self, index):

current = self.get_node(index)

if current is none:

return "node is not exist"

return current.data

# 列印鍊錶

def print_link(self):

if self.is_empty():

return

list =

current = self.head

while current.next is not none:

current = current.next

else:

print(list)

# 獲取鍊錶長度

def get_length(self):

if self.is_empty():

return 0

current = self.head

count = 0

while current.next is not none:

count += 1

current = current.next

else:

count += 1

return count

# 判斷鍊錶是否為空

# 如果為空,返回true

# 如果不為空,返回false

def is_empty(self):

return self.head is none

# 當前元素之後插入乙個元素

# index 元素索引

# data 插入的值

def add_after(self, index, data):

current = self.get_node(index)

if current is none:

return "node is not exist"

current_next = current.next

current.next = node(data)

current = current.next

current.next = current_next

# 在當前元素之前插入乙個元素

def add_before(self, index, data):

if index == 1:

current = self.get_node(index)

self.head = node(data)

self.head.next = current

return

pre = self.get_pre_node(index)

current = pre.next

pre.next = node(data)

pre = pre.next

pre.next = current

# 獲取當前元素的前乙個元素

def get_pre_node(self, index):

if self.is_empty():

print("link is empty")

return

if index > self.get_length() or index <= 1:

print("node is not exist")

return

pre = self.head

i = 0

while i < index:

if i == index - 2:

return pre

pre = pre.next

i += 1

# 刪除指定元素,並返回刪除元素的值

def remove(self, index):

if index == 1 and not self.is_empty():

data = self.head.data

self.head = self.head.next

return data

pre_node = self.get_pre_node(index)

current = self.get_node(index)

if pre_node is none or current is none:

print("data is not exist")

pre_node.next = current.next

return current.data

# 修改當前結點的值

def update(self, index, data):

current = self.get_node(index)

if current is none:

return "current node is none"

current.data = data

# 將新鍊錶合併到當前鍊錶

def merge(self, data):

size = self.get_length()

last_node = self.get_node(size)

last_node.next = data.head

return self

# test

y = (1,2,3,4)

s = ["a", "b", "c", "d"]

linklist = linklist(y)

# linklist.init_link_list(["a", "b", "c", "d"])

# second = linklist()

# second.init_link_list(["x", "y", "z"])

# linklist.add_after(-1, "x")

# linklist.add_after(1, "y")

# linklist.init_link_list()

# linklist.add_before(1,"x")

# linklist.print_link()

# print("item:", linklist.get_data(2))

# print("length:", linklist.get_length())

# print("is empty", linklist.is_empty())

# print(linklist.get_pre_node(3).data)

# print("remove node:",linklist.remove(2))

# linklist.merge(second)

linklist.print_link()

單鏈表 Python實現

class lnode self.elem 結點資料域 方法的第二個引數名是為了與python自帶函式next 區分 def init self,elem,next none self.elem elem self.next next 鍊錶類定義如下,其中包含了很多鍊錶操作方法,如向鍊錶中插入元素 ...

單鏈表的python實現

首先說下線性表,線性表是一種最基本,最簡單的資料結構,通俗點講就是一維的儲存資料的結構。順序表示指的是用一組位址連續的儲存單元依次儲存線性表的資料元素,稱為線性表的順序儲存結構或順序映像 鏈式表示指的是用一組任意的儲存單元儲存線性表中的資料元素,稱為線性表的鏈式儲存結構。而他既可以是連續的也可以不連...

多種單鏈表 python實現

本 參照裘宗燕老師的 資料結構與演算法 python語言描述 看了幾章感覺不錯,講解十分清晰 話不多說直接上碼 coding utf 8 created on thu jul 18 10 26 53 2019 author 糰子 import random class lnode def init ...