python循坏单链表

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


class CirculationLinkList(object):
    def __init__(self):
        self.__head = None

    def is_empty(self):
        """判断链表是否为空"""
        return self.__head is 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):
        """遍历"""
        if self.is_empty():
            return
        cur = self.__head
        print(cur.item, end=' ')
        while cur.next != self.__head:
            cur = cur.next
            print(cur.item, end=' ')

    def add(self, item):
        """在头部添加一个节点"""
        node = Node(item)
        if self.is_empty():
            self.__head = node
            node.next = node
            return
        else:
            cur = self.__head
            while cur.next != self.__head:
                cur = cur.next
            # 当前位置为最后一个节点
            node.next = self.__head
            self.__head = node
            cur.next = node

    def append(self, item):
        """在尾部添加一个节点"""
        if self.is_empty():
            self.add(item)
        else:
            cur = self.__head
            while cur.next != self.__head:
                cur = cur.next
            # 当前节点为最后的节点
            node = Node(item)
            node.next = self.__head
            cur.next = node

    def insert(self, pos, item):
        """在指定位置pos添加节点"""
        if pos <= 0:
            self.add(item)
        elif pos > self.length() - 1:
            self.append(item)
        else:
            cur = self.__head
            index = 0
            while index < (pos - 1):
                cur = cur.next
                index += 1
            # 得到插入位置前节点
            node = Node(item)
            node.next = cur.next
            cur.next = node

    def remove(self, item):
        """删除一个节点"""
        cur = self.__head
        pre = None
        while cur.next != self.__head:
            if cur.item == item:
                if pre is None:
                    self.__head = cur.next
                else:
                    pre.next = cur.next
                return
            pre = cur
            cur = cur.next
        if cur.item == item:
            if cur == self.__head:
                self.__head = None
            else:
                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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值