循环单链表

class SinleNode:
    def __init__(self,item):
        self.item=item
        self.next=None

class CycleList:
    def __init__(self):
        self.head=None

    def is_empty(self):
        return self.head==None

    def length(self):
        cur=self.head
        count=0
        while cur.next!=self.head:
            cur=cur.next
            count+=1
        return count

    def travel(self):
        cur=self.head
        print('--------travel---------')
        while cur.next!=self.head:
            print(cur.item,end='\t')
            cur=cur.next
        print(cur.item)
        print("---------end--------")


    def add(self,item):
        node = SinleNode(item)
        if self.is_empty():
            self.head=node
            node.next=self.head
            return
        else:
            node.next=self.head
            cur=self.head
            while cur.next!=self.head:
                cur=cur.next
            cur.next=node
            self.head=node

    def append(self,item):
        node = SinleNode(item)
        if self.is_empty():
            self.head=node
            node.next=self.head
            return
        cur=self.head
        while cur.next!=self.head:
            cur=cur.next
        cur.next=node
        node.next=self.head

    def insert(self,pos,item):
        node = SinleNode(item)
        if pos<=0:
            self.add(item)
        elif pos>(self.length()-1):
            self.append(item)
        else:
            cur=self.head
            count=0
            while count<(pos-1):
                count+=1
                cur=cur.next
            node.next=cur.next
            cur.next=node


    def remove(self,item):
        if self.is_empty():
            return
        else:
            cur=self.head
            pre=None
            while cur.next!=self.head:
                if cur.item==item:
                    # 删除头结点
                    if pre==None:
                        if cur.next==None:
                            self.head=None
                        else:
                            while cur.next!=self.head:
                                cur=cur.next
                            cur.next=self.head.next
                            self.head=self.head.next
                    else:
                        pre.next=cur.next
                pre=cur
                cur=cur.next
            if cur.item==item:
                pre.next=self.head

    def search(self,item):
        cur=self.head
        while cur.next!=self.head:
            if cur.item==item:
                return True
            cur=cur.next
        if cur.item==item:
            return True
        return False



cycle_list = CycleList()
for i in [4,3,2,1,5,6]:
    cycle_list.append(i)

print('length:',cycle_list.length())
cycle_list.travel()
cycle_list.remove(6)
cycle_list.travel()
print(cycle_list.search(5))

循环单链表有初始化节点,初始化链表。初始化和普通单链表是一样的,初始化链表也和普通链表是一样的。

判空,求长度,遍历。判空和普通单链表是一样的,求长度与遍历和普通单链表的判断条件不一样,即循环终止的条件为next域为head节点。

头插法:首先判断是否为空,如果为空,head节点指向当前节点,当前节点的next域指向head。如果不为空,首先遍历到最后一个节点,使其指针指向当前节点,head指针改变为指向当前节点。
头部添加元素的时候,新的节点的next域指向head,然后让当前指针一直遍历到最后一个元素,改变最后那个元素的指向关系,最后那个元素一定要指向新的头结点。还需要修改头指针的指向关系,头指针指向新的节点。

尾插法:首先判断是否为空,如果为空,head节点指向当前节点,当前节点的next域指向head指针。如果不为空,首先遍历到最后一个节点,当前指针指向新的节点,新的节点的next域指向head指针。
需要注意的是:如果为空,一定要先让head指针指向当前新的节点

任意位置插:根据位置分为头部,尾部,中间位置。头部和尾部如上所示,若是中间位置,则调整指针。新的节点的next域指向当前节点的next域,指针指向当前节点

删除:如果是第一个元素,遍历找到最后一个指针,最后一个指针指向head的next域,调整head指针。
中间位置则,遍历指针,用一个指针保存当前指针的状态。找到元素后,保存的指针的next指向当前的next。
并对最后一个元素进行判断。

搜索:和遍历的代码本质上是一样的,都是循环遍历并进行查找。

单向循环链表注意self.head指针的指向关系

特别要注意修改第一个元素的时候和最后一个元素时,是否需要遍历整个链表
删除第一个元素的时候,需要分为只有一个节点和多个节点的情况

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值