数据结构(Python)-单循环链表实现

# 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:
			res_list.append(linked_list.val)
			linked_list = linked_list.next
		res_list.append(linked_list.val)
		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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值