Python3实现循环链表

循环链表

一、概要:

循环链表是相对于单链表而言的一种特殊情况,就是将最后一个节点的next域赋值给头结点(图一为单链表,图二为循环链表)
在这里插入图片描述

级循环链表与单链表的比较

循环链表的特点是无须增加存储量,仅对表的链接方式稍作改变,即可使得表处理更加方便灵活。①循环链表中没有None域。涉及遍历操作时,其终止条件就不再是像非循环链表那样判别p或p->next是否为None,而是判别它们是否等于某一指定指针,如头指针或尾指针等。 ②在单链表中,从一已知结点出发,只能访问到该结点及其后续结点,无法找到该结点之前的其它结点。而在单循环链表中,从任一结点出发都可访问到表中所有结点,这一优点使某些运算在单循环链表上易于实现。

二、实现代码:

(1)建立节点:

class Node():
    """
        建立节点
    """
    def __init__(self,date):
        self.date=date
        self.next=Node

(2)建立带有头节点的循环链表:

判断循环链表是否为空

def is_empty(self):
        """判断链表是否为空"""
        return self.__head==None

求循环链表的长度

def length(self):
        """
            求链表的长度
        """
        if self.is_empty():
            return 0
        else:
            count=1
            cur=self.__head
            while cur.next!=self.__head:
                count+=1
                cur=cur.next
                return count

求循环链表的长度


    def travel(self):
        cur=self.__head
        if self.is_empty():
            print("该链表为空,无法进行遍历")
        else:
            while cur!=self.__head:
                print(str(cur.date),end=" ")
                cur=cur.next
            print(str(cur.date))#用于遍历最后一个节点

这行代码要注意最后一行,不能少,否则不能够输出最后一个节点,因为在while循环中,扫描到最后一个节点时cur==self.__head 不能够进入while循环中,所以需要单独输出

从头插入节点:

    def HeadInsert(self,num):
        """
            头部插入节点
        """
        node=Node(num)
        if self.is_empty():
            self.__head=node
            node.next=node
        else:
            cur=self.__head
            while cur.next!=self.__head:
                cur=cur.next
            node.next=self.__head
            self.__head=node
            cur.next=self.__head

从尾部插入节点:

    def TailInsert(self,num):
        """
            尾部插入节点
        """
        node=Node(num)
        if self.is_empty():
            self.__head=node
            node.next=self.__head
        else:
            cur=self.__head
            while cur.next!=self.__head:
                cur=cur.next
            node.next=self.__head
            cur.next=node

索引插入:

       def NodeInsert(self,index,num):
        """在指定位置添加元素"""
        #指向不是头部元素,self.__head的地址
        # 为下一个元素,所以pre为下一个元素
        if index <= 0:
            #认为是头插法
            self.HeadInsert(num)
        elif index > (self.length()-1):
            self.TailInsert(num)
        else:
            pre_cur = self.__head
            count = 0
            while count < (index-1):
                count+=1
                pre_cur = pre_cur.next
            node = Node(num)
            node.next = pre_cur.next
            pre_cur.next = node

在这里插入图片描述

完整代码:

import random
class Node():
    """
        建立节点
    """
    def __init__(self,date):
        self.date=date
        self.next=None
class LinkNode():
    def __init__(self,node=None):
        self.__head=node
        if node:
            node.next=self.__head #建立一个循环头节点
    def is_empty(self):
        """判断链表是否为空"""
        return self.__head==None

    def length(self):
        """
            求链表的长度
        """
        if self.is_empty():
            return 0
        
        count=1
        cur=self.__head
        while cur.next!=self.__head:
            count+=1
            cur=cur.next
        return count

    def travel(self):
        """遍历整个链表"""
        if self.is_empty():
            return
        #建立游标等于起始节点
        cur = self.__head
        while cur.next != self.__head:
            print(cur.date,end=" ")
            cur = cur.next
        print(cur.date,end="\n")
    def HeadInsert(self,num):
        """
            头部插入节点
        """
        node=Node(num)
        if self.is_empty():
            self.__head=node
            node.next=node
        else:
            cur=self.__head
            while cur.next!=self.__head:
                cur=cur.next
            node.next=self.__head
            self.__head=node
            cur.next=self.__head
    def TailInsert(self,num):
        """
            尾部插入节点
        """
        node=Node(num)
        if self.is_empty():
            self.__head=node
            node.next=self.__head
        else:
            cur=self.__head
            while cur.next!=self.__head:
                cur=cur.next
            cur.next=node   
            node.next=self.__head

            
    def NodeInsert(self,index,num):
        """在指定位置添加元素"""
        #指向不是头部元素,self.__head的地址
        # 为下一个元素,所以pre为下一个元素
        if index <= 0:
            #认为是头插法
            self.HeadInsert(num)
        elif index > (self.length()-1):
            self.TailInsert(num)
        else:
            pre_cur = self.__head
            count = 0
            while count < (index-1):
                count+=1
                pre_cur = pre_cur.next
            node = Node(num)
            node.next = pre_cur.next
            pre_cur.next = node

a=LinkNode()
for i in range(6):
    a.TailInsert(random.randint(0,10))#随机从插入6个数字
a.travel()
a.NodeInsert(5,100)
a.travel()

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

William_Tao(攻城狮)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值