資料結構(Python) 單迴圈鍊錶實現

2021-08-20 22:15:53 字數 4019 閱讀 2581

# coding=utf-8

import random

random.seed(1)

class node():

def __init__(self, x, next=none):

self.val = x

self.next = next

# 迴圈鍊錶

class circular_linked_list():

# 帶頭節點迴圈鍊錶的建立

def create(self, length, value=none):

head_node = node("head")

res = head_node

for i in range(length):

if value == none:

# 隨機產生length個0-100之間的數

x = random.randint(0, 100)

else:

x = value[i]

head_node.next = node(x)

head_node = head_node.next

head_node.next = res

return res

# 迴圈鍊錶列表值列印

# 遍歷鍊錶

def print_linked_list(self, linked_list):

res_list =

head = linked_list

while linked_list.next != head:

linked_list = linked_list.next

print(res_list)

# 判斷列表是否為空

def isempty(self, linked_list):

if linked_list.next == none:

return true

else:

return false

# 獲取鍊錶長度

def get_len(self, linked_list):

head = linked_list

i = 1

while linked_list.next != head:

linked_list = linked_list.next

i += 1

return i

# 單迴圈鍊錶插入操作

# 在單鏈表的第n號位置插入value值(頭節點的後乙個節點認為是0號位置)

def insert(self, linked_list, n, node):

head = linked_list

for i in range(n):

linked_list = linked_list.next

node.next = linked_list.next

linked_list.next = node

return head

# 單迴圈鍊錶刪除操作

def delete(self, linked_list, n):

head = linked_list

for i in range(n):

linked_list = linked_list.next

linked_list.next = linked_list.next.next

return head

# 單迴圈鍊錶刪除元素值的操作

def delete_value(self,linked_list, value=none):

head = linked_list

if value == none:

return head

else:

while linked_list.next != head:

if linked_list.next.val == value:

linked_list.next = linked_list.next.next

return head

else:

linked_list = linked_list.next

print("鍊錶中不存在%s,刪除失敗" % str(value))

return head

# 兩個單迴圈鍊錶合併操作

#(b插入到a後面,b的尾指標轉向a的頭節點,刪除b的頭節點)

def combine_ab(self, a, b):

head_a, head_b = a, b

while a.next != head_a:

a = a.next

a.next = b.next

while b.next != head_b:

b = b.next

b.next = head_a

return head_a

if __name__ == "__main__":

cll = circular_linked_list()

# 建立迴圈鍊錶

value = [12, 24, 36, 48, 44, 52, 21, 90, 88, 87]

# circular_linked_list = cll.create(len(value), value)

circular_linked_list = cll.create(10) # [17, 72, 97, 8, 32, 15, 63, 97, 57, 60]

cll.print_linked_list(circular_linked_list)

# 判斷迴圈鍊錶是否為空

result = cll.isempty(circular_linked_list) # false

print(result)

# 獲取鍊錶長度

length = cll.get_len(circular_linked_list) # 11(包含了頭節點的長度)

print(length)

# 在單鏈表的第5號位置(第6個數)插入value值

node1 = node(101)

circular_linked_list = cll.insert(circular_linked_list, 5, node1) # ['head', 17, 72, 97, 8, 32, 101, 15, 63, 97, 57, 60]

cll.print_linked_list(circular_linked_list)

# 刪除第4號位置的節點(不包括頭節點)

circular_linked_list = cll.delete(circular_linked_list, 4) # ['head', 17, 72, 97, 8, 101, 15, 63, 97, 57, 60]

cll.print_linked_list(circular_linked_list)

# 刪除鍊錶節點元素值

circular_linked_list = cll.delete_value(circular_linked_list, 101) # ['head', 17, 72, 97, 8, 15, 63, 97, 57, 60]

cll.print_linked_list(circular_linked_list)

circular_linked_list = cll.delete_value(circular_linked_list, 16) # 鍊錶中不存在16,刪除失敗

# 合併兩個單迴圈鍊錶 a - b

# 建立新的鍊錶b

value_b = [1, 4, 6, 9, 5, 3]

circular_linked_list_b = cll.create(len(value_b), value=value_b)

combine_ab = cll.combine_ab(circular_linked_list, circular_linked_list_b)

cll.print_linked_list(combine_ab) # ['head', 17, 72, 97, 8, 15, 63, 97, 57, 60, 1, 4, 6, 9, 5, 3]

資料結構 線性表 單迴圈鍊錶

資料結構 線性表的鏈式表示 單鏈表 迴圈鍊錶 線性表元素序號從1算起 date 2017 4 13 include include define initsize 100 define elemtype char typedef struct lnodelnode,linklist linklist...

單迴圈鍊錶

頭插 尾插 顯示 頭刪 尾刪 按值插入 按位置插入 查詢 長度 逆序 清除 摧毀 初始化 排序 按位置刪除 按值刪除 可以進一步優化 ifndef sclist h define sclist h include typedef int elementtype typedef enum bool 鍊...

單迴圈鍊錶

乙個遊戲,數到第n人出列 include include include struct people struct people creat struct people head,int n else p struct people malloc sizeof struct people tail ...