栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python数据结构:03单向循环链表

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python数据结构:03单向循环链表

class Node:
    def __init__(self, elem, next=None):
        self.elem = elem
        self.next = next


class SinglelinkList:
    """单向循环链表"""
    def __init__(self, node=None):
        self.__head = node
        if node:
            node.next = node

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

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

    def travel(self):
        """遍历整个链表"""
        if self.is_empty():
            return
        cur = self.__head
        print(cur.elem, end=' ')
        while cur.next is not self.__head:
            cur = cur.next
            print(f'{cur.elem} ', end='')
        print('')

    def add(self, elem):
        """链表头部添加信息"""
        node = Node(elem)
        if self.is_empty():
            self.__head = node
            node.next = node
        else:
            cur = self.__head
            while cur.next is not self.__head:
                cur = cur.next
            cur.next = node
            node.next = self.__head
            self.__head = node

    def append(self, elem):
        """链表尾部添加元素"""
        if self.__head is None:
            self.add(elem)
        else:
            cur = self.__head
            while cur.next is not self.__head:
                cur = cur.next
            cur.next = Node(elem, next=self.__head)

    def insert(self, pos, elem):
        """指定位置添加元素"""
        if pos <= 0:
            self.add(elem)
        elif 0 < pos < self.length():
            cur = self.__head.next
            pre = self.__head
            while pos > 1:
                cur = cur.next
                pre = pre.next
                pos -= 1
            node = Node(elem, cur)
            pre.next = node
        else:
            self.append(elem)

    def remove(self, elem):
        """删除节点"""
        if self.is_empty():
            print('列表为空')
            return

        pre = None
        cur = self.__head
        while cur.next is not self.__head:
            if cur.elem == elem:
                # 如果删除的是第一个节点
                if not pre:
                    # 如果列表只有一个节点
                    if cur.next == self.__head:
                        self.__head = None
                    else:
                        self.__head = cur.next
                # 删除的是最后一个节点
                elif cur.next == self.__head:
                    pre.next = self.__head
                else:
                    pre.next = cur.next
                break
            else:
                pre = cur
                cur = cur.next
        else:
            print('没有这个值')

    def search(self, elem):
        """查找节点是否存在"""
        if self.is_empty():
            print("列表为空")
            return
        cur = self.__head
        while cur.next is not self.__head:
            if cur.elem == elem:
                return True
            else:
                cur = cur.next
        if cur.elem == elem:
            return True
        else:
            return False


if __name__ == '__main__':
    pass
    # 测试过程省略
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/423791.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号