2020-12-26

python之单向循环链表

12.26 铁铁,只有自己才能为自己的无能“买单”!!!

单向循环链表各种操作
is_empty() 判断链表是否为空
length() 返回链表的长度
travel() 遍历
add(item) 在头部添加一个节点
append(item) 在尾部添加一个节点
insert(pos, item) 在指定位置pos添加节点
remove(item) 删除一个节点
search(item) 查找节点是否存在

class Node(object):
    """结点"""
    def __init__(self,item):
        self.item=item
        self.next=None

class SimpleCycLinkList(object):
    """单向循环链表"""
    def __init__(self,node=None):
        self.head=node
        if node:             #???
            node.next=node

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

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

    def travel(self):
        if self.is_empty():
            return
        else:
            cur=self.head
            while cur.next!=self.head:
                print(cur.item,end=" ")
                cur=cur.next
            print(cur.item)

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

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

    def insert(self,pos,item):
        if pos<=0:
            self.add(item)
        elif pos>=self.length():
            self.append(item)
        else:
            node=Node(item)
            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
        cur=self.head
        prev=None
        while cur.next!=self.head:
            if cur.item==item:
                if cur==self.head:
                    rear=self.head
                    while rear.next!=self.head:
                        rear=rear.next
                    rear.next=cur.next
                    self.head=cur.next
                else:
                    prev.next=cur.next
                return
            else:
                prev=cur
                cur=cur.next
        if cur.item==item:
            if cur==self.head:
                self.head=None
            else:
                prev.next=self.head
            
        

    def search(self,item):
        if self.is_empty():
            return False
        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

if __name__=="__main__":
    ls=SimpleCycLinkList()
    print(ls.is_empty())
    print(ls.length())
    print(ls.search(70))
    ls.insert(2,89)
    ls.append(20)
    ls.append(54)
    ls.add(35)
    ls.remove(89)
    print(ls.is_empty())
    print(ls.length())
    ls.travel()
    print(ls.search(70))

至此,2020年快结束,铁汁们在这一年又学“废”了什么?嘿嘿,让我们一起学python吧,俗话说得好:“人生苦短,我用python”。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值