单向循环链表python实现

简单实现单向循环链表

链表操作

  • 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 SinCycLinkList(object):
    """单向循环链表"""
    def __init__(self):
        self.__head = None

    def is_empty(self):
        """判断是否为空"""
        return self.__head == None

    def length(self):
        """返回链表的长度"""
        if self.is_empty():
            return 0
        count = 1
        cur = self.__head
        while cur.next != self.__head:
            cur = cur.next
            count += 1

        return count
    def travel(self):
        """遍历链表"""
        if self.is_empty():
            return
        cur = self.__head
        print(cur.item)
        while cur.next != self.__head:
            cur = cur.next
            print(cur.item)

        print("", end="")

    def add(self, item):
        """头部添加元素"""
        node = Node(item)
        if self.is_empty():
            self.__head = node
            node.next = self.__head
        else:
            # 添加的节点指向_head
            node.next = self.__head
            # 移动到链表尾部
            cur = self.__head
            while cur.next != self.__head:
                cur = cur.next

            cur.next = node
            # 将头_head指向node
            self.__head = node

    def append(self, item):
        """尾部添加元素"""
        node = Node(item)
        if self.is_empty():
            self.__head = node
            node.next = self.__head

        else:
            # 移动到链表底部
            cur = self.__head
            while cur.next != self.__head:
                cur = cur.next
            # 将尾节点指向node
            cur.next = node
            # 将node节点指向_head
            node.next = self.__head

    def insert(self, pos, item):
        """指定位置添加元素"""
        if pos <= 0:
            self.add(item)
        elif pos > (self.length() - 1):
            self.append(item)

        else:
            node = Node(item)
            cur = self.__head
            count = 0
            # 移动到指定位置前一个
            while count < (pos - 1):
                cur = cur.next
                count += 1
            node.next = cur.next
            cur.next = node

    def remove(self, item):
        """删除节点"""
        if self.is_empty():
            return
        else:
            # 将cur指向头节点
            cur = self.__head
            pre = 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
                        # 头节点指向cur的next
                        self.__head = cur.next
                        rear.next = self.__head
                    else:
                        # 中间节点
                        pre.next = cur.next
                    return
                else:
                    pre = cur
                    cur = cur.next
            # 退出循环,cur指向尾节点
            if cur.item == item:
                if cur == self.__head:
                    # 链表只有一个节点
                    self.__head = None
                else:
                    pre.next = self.__head

    def search(self, item):
        """查找节点元素是否存在"""
        if self.is_empty():
            return False
        cur = self.__head
        if cur.item == item:
            return True
        while cur.next != self.__head:
            cur = cur.next
            if cur.item == item:
                return True

        return False

猜你喜欢

转载自blog.csdn.net/Mr_Duanlife/article/details/85808670
今日推荐