Python数据结构与算法03-单循环链表

单循环链表

复制代码
class ListNode:
    def __init__(self,val,next=None):
        self.val=val
        self.next=next

class SingleLoopLinkList:
    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
        cur = self.__head
        count = 0
        while cur.next != self.__head:
            count += 1
            cur = cur.next
        return count+1

    def travel(self):
        '''遍历链表'''
        if self.is_empty():
            tip='NUll'
            print(tip)
            return 
        cur = self.__head
        while cur.next != self.__head:
            print(cur.val, end=' ')
            cur = cur.next
        print(cur.val)#此时cur指向尾节点但是上面的循环没有打印尾节点
        print(' ')

    def append(self, item):
        '''在链表尾部添加元素'''
        node = ListNode(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 add(self, item):
        '''在链表头部添加元素'''
        node=ListNode(item)
        if self.is_empty():#空链表处理
            self.__head=node
            node.next=self.__head
            return
        cur=self.__head
        node.next=cur
        self.__head=node
        while cur.next!=node.next:
            cur=cur.next
        cur.next=self.__head


    def insert(self, pos, item):
        '''在指定位置添加元素
        :param pos 索引从0开始
        '''
        if pos < 0:
            self.add(item)
        elif pos >= self.length():
            self.append(item)
        else:
            node = ListNode(item)
            pre = self.__head
            count = 0
            while count < pos - 1:
                pre = pre.next
                count += 1
            node.next = pre.next
            pre.next = node

    def search(self, item):
        '''查找元素
        若找到元素返回元素索引
        若找不到返回-1
        '''
        cur = self.__head
        count = 0
        while cur.next!= self.__head:
            count += 1
            if cur.val == item:
                return count - 1
            else:
                cur = cur.next
        if cur.val==item:
            return count
        else:
            return -1

    def remove(self, item):
        '''删除制定元素病返回删除前元素的索引'''
        if self.is_empty():
            return
        tail=self.__head
        while tail.next!=self.__head:
            tail=tail.next
        #tail指向尾部
        cur = self.__head
        pre = None
        if cur.val==item:#处理头
            if ll.length()==1:
                self.__head=None
            else:
                self.__head=cur.next
                tail.next=cur.next

        elif tail.val==item:#处理尾
            while cur.next!= self.__head:
                    pre = cur
                    cur = cur.next#退出循环的时候cur指向尾节点
            pre.next=cur.next

        else:#处理中间
            while cur.next!= self.__head:
                if cur.val == item:
                    pre.next = cur.next

                    break
                else:
                    pre = cur
                    cur = cur.next


ll=SingleLoopLinkList()
ll.append(1)

ll.travel()
ll.remove(1)
ll.travel()
复制代码

 

posted @   Junior_bond  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示