zoukankan      html  css  js  c++  java
  • python实现单向循环链表

    单向循环链表

    单链表的一个变形是单向循环链表,链表中最后一个节点的next域不再为None,而是指向链表的头节点。

    实现

    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 is None
    
        def length(self):
            """返回链表的长度"""
            if self.is_empty():
                return 0
            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)
            while cur.next != self.head:
                cur = cur.next
                print(cur.item)
            print("结束了")
    
        def add(self, item):
            """头部添加节点"""
            node = Node(item)
            # 如果链表为空
            if self.is_empty():
                # 把插入节点置为首节点
                self.head = node
                # node的下一个节点置为自身
                node.next = self.head
            else:
                # node指向以前的首节点
                node.next = self.head
                # 遍历,找到尾节点
                cur = self.head
                while cur.next != self.head:
                    cur = cur.next
                # 尾节点指向插入的节点
                cur.next = 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
                # 让尾节点指向插入的节点
                cur.next = node
                # 让插入的节点指向首节点
                node.next = self.head
    
        def insert(self, pos, item):
            """在指定的位置添加节点"""
            # 如果插入的索引小于0
            if pos <= 0:
                self.add(item)
            elif pos > self.length()-1:
                self.append(item)
            else:
                node = Node(item)
                num = 0
                cur = self.head
                # 找到插入位置的前一个节点
                while num < pos-1:
                    num += 1
                    cur = cur.next
                # 注意这里的更改顺序!!!
                node.next = cur.next
                cur.next = node
    
        def remove(self, item):
            """删除节点"""
            # 链表为空,则直接返回
            if self.is_empty():
                return
            cur = self.head
            pre = None
            # 如果头节点的元素就是要查找的元素item
            if cur.item == item:
                # 如果链表不止一个节点
                if cur.next != self.head:
                    while cur.next != self.head:
                        cur = cur.next
                    cur.next = self.head.next
                    self.head = self.head.next
                else:
                    # 链表只有一个节点
                    self.head = None
            else:
                pre = self.head
                # 第一个节点不是要删除的
                while cur.next != self.head:
                    # 找到要删除的节点
                    if cur.item == item:
                        # 删除
                        pre.next = cur.next
                        return
                    else:
                        pre = cur
                        cur = cur.next
                # 循环结束之后cur为尾节点
                if cur.item == item:
                    pre.next = cur.next
    
        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
            # 遍历一圈后都找不到,返回False
            return False
    链表实现
    if __name__ == '__main__':
        lst1 = SinCycLinkList()  # self.head=None
        lst1.add(1)
        lst1.add(2)
        lst1.append(3)
        lst1.append(4)
        lst1.travel()
        lst1.insert(1, 5)
        lst1.travel()
        lst1.insert(-2, 6)
        lst1.travel()
        lst1.insert(10, 7)
        lst1.travel()
        lst1.remove(1)
        lst1.travel()
        lst1.remove(10)
        lst1.travel()
        print('head:', lst1.head.item)
        print('length:', lst1.length())
    测试
  • 相关阅读:
    种子销售管理需求
    三角函数
    软件人性化的体现
    三角函数
    ProductManager
    不能说的秘密
    种子销售管理需求
    JTable使用
    不能说的秘密
    设计模式(了解篇)转载
  • 原文地址:https://www.cnblogs.com/zzliu/p/10555427.html
Copyright © 2011-2022 走看看