• python 数据结构与算法 day02 单向循环链表


    1. 实现单向循环链表

    class Node():
        """定义结点"""
        def __init__(self,item):
            self.item=item
            self.next=None
    class SingleLoopLinkList(object):
        """单向循环链表"""
        def __init__(self,node=None):
            self.__head=node
            if node:  # 如果把一个节点挂到单向循环链表的链子中,需要指向自身(构成一个循环)
                node.next=node
    
    
        def is_empty(self):
            """判断单向循环链表是否为空"""
            if self.__head is None:
                return True
            return False
    
        def length(self):
            """求单链表的长度"""
            count=1
            cur=self.__head
            while cur.next is not self.__head:
                count+=1
                cur=cur.next
            return count
    
        def travel(self):
            """单向循环链表的遍历"""
            cur=self.__head
            while cur.next is not self.__head:
                print(cur.item,end=" ")
                cur=cur.next
            print(cur.item,end=" ")
            print(" ")
    
        def add(self,item):
            """单向循环链表头部添加元素"""
            node=Node(item)
            cur=self.__head
            if cur is None:
                self.__head=node
                node.next=self.__head
            else:
                while cur.next is not self.__head:
                    cur=cur.next
                node.next=self.__head
                self.__head=node
                cur.next=node
    
        def append(self,item):
            """单向循环链表尾部追加元素"""
            node=Node(item)
            cur=self.__head
            if cur is None:
                self.__head=node
                node.next=self.__head
            else:
                while cur.next is not self.__head:
                    cur=cur.next
                cur.next=node
                node.next=self.__head
    
        def insert(self,pos,item):
            """单向循环链表指定位置插入元素"""
            count=0
            node=Node(item)
            cur=self.__head
            if pos<=0:
                self.add(item)
            elif pos>=self.length():
                self.append(item)
            else:
                while count<pos-1:
                    cur=cur.next
                    count+=1
                node.next=cur.next
                cur.next=node
    
        def search(self,item):
            """判断单向循环链表是否存在某一个元素"""
            cur=self.__head
            while cur.next is not self.__head:
                if cur.item ==item:
                    return True
                cur=cur.next
            if cur.item==item:
                return True
            return False
    
        def remove(self,item):
            """单向循环链表删除某一个元素"""
            if self.search(item):
                pre=None
                cur=self.__head
                while cur.next is not self.__head:
                    if cur.item==item:
                        if cur ==self.__head:
                            pre=cur.next
                            while cur.next is not self.__head:
                                cur=cur.next
                            cur.next=pre
                            self.__head=pre
                        else:
                            pre.next=cur.next
                        break
                    else:
                        pre=cur
                        cur=cur.next
                if cur.item==item:
                    pre.next=self.__head
    
    if __name__=="__main__":
        slll=SingleLoopLinkList()
        print(slll.is_empty())
        slll.add(10000000)
        slll.append(1)
        slll.append(2)
        slll.append(3)
        slll.append(4)
        slll.add(0)
        print(slll.length())
        slll.travel()
        slll.insert(0,1000)
        slll.insert(2,3000)
        slll.insert(20,2000)
        slll.travel()
        slll.remove(1000)
        slll.remove(2000)
        slll.travel()

    运行结果:


    2. 单向循环链表(版本二)

    头插法,尾插法,查找,删除元素,一定要考虑特殊情况:比如链表是否为空,链表是否只有一个结点,链表的头结点,尾部结点等!!

    class Node():
        """定义结点"""
        def __init__(self,item):
            self.item=item
            self.next=None
    class SingleLoopLinkList(object):
        """单向循环链表"""
        def __init__(self,node=None):
            self.__head=node
            if node:  # 如果把一个节点挂到单向循环链表的链子中,需要指向自身(构成一个循环)
                node.next=node
    
    
        def is_empty(self):
            """判断单向循环链表是否为空"""
            if self.__head is None:
                return True
            return False
    
        def length(self):
            """求单向循环链表的长度"""
            count=1
            cur=self.__head
            if self.__head is None:  # 单项循环链表为空
                return 0
            else:
                while cur.next is not self.__head:
                    count+=1
                    cur=cur.next
                return count
    
        def travel(self):
            """单向循环链表的遍历"""
            cur=self.__head
            if self.is_empty():  # 是一个空链表
                return
            while cur.next is not self.__head:
                print(cur.item,end=" ")
                cur=cur.next
            print(cur.item,end=" ")
            print(" ")
    
        def add(self,item):
            """单向循环链表头部添加元素"""
            node=Node(item)
            cur=self.__head
            if cur is None:
                self.__head=node
                node.next=self.__head
            else:
                while cur.next is not self.__head:
                    cur=cur.next
                node.next=self.__head
                self.__head=node
                cur.next=node
    
        def append(self,item):
            """单向循环链表尾部追加元素"""
            node=Node(item)
            cur=self.__head
            if cur is None:
                self.__head=node
                node.next=self.__head
            else:
                while cur.next is not self.__head:
                    cur=cur.next
                cur.next=node
                node.next=self.__head
    
        def insert(self,pos,item):
            """单向循环链表指定位置插入元素"""
            count=0
            node=Node(item)
            cur=self.__head
            if pos<=0:
                self.add(item)
            elif pos>=self.length():
                self.append(item)
            else:
                while count<pos-1:
                    cur=cur.next
                    count+=1
                node.next=cur.next
                cur.next=node
    
        def search(self,item):
            """判断单向循环链表是否存在某一个元素"""
            cur=self.__head
            if self.is_empty():  # 空链表
                return
            while cur.next is not self.__head:
                if cur.item ==item:
                    return True
                cur=cur.next
            if cur.item==item:
                return True
            return False
    
        def remove(self,item):
            """单向循环链表删除某一个元素"""
            if self.search(item):
                pre=None
                cur=self.__head
                while cur.next is not self.__head:
                    if cur.item==item:
                        if cur ==self.__head:
                            pre=cur.next
                            while cur.next is not self.__head:
                                cur=cur.next
                            cur.next=pre
                            self.__head=pre
                        else:
                            pre.next=cur.next
                        break
                    else:
                        pre=cur
                        cur=cur.next
                if cur.item==item:
                    if self.length()==1:  # 单项循环链表只有一个元素,且是需要删除的元素
                        self.__head=None
                    else:
                        pre.next=self.__head
    
    if __name__=="__main__":
        slll=SingleLoopLinkList()
        print(slll.is_empty())
        slll.add(10000000)
        slll.remove(10000000)
        print(slll.length())
        slll.append(1)
        slll.append(2)
        slll.append(3)
        slll.append(4)
        slll.add(0)
        print(slll.length())
        slll.travel()
        slll.insert(0,1000)
        slll.insert(2,3000)
        slll.insert(20,2000)
        slll.travel()
        slll.remove(1000)
        slll.remove(2000)
        slll.travel()
    View Code
    talk is cheap,show me the code
  • 相关阅读:
    matlab 高阶(一) —— assignin与evalin
    OpenCV 图像白平衡算法(相机自动白平衡)
    matlab 警告(warning)、错误(error)、异常(exception)与断言(assert)
    matlab 警告(warning)、错误(error)、异常(exception)与断言(assert)
    OpenCV 图像清晰度评价(相机自动对焦)
    Matlab Tricks(二十五) —— 二维图像的 shuffle
    Matlab Tricks(二十五) —— 二维图像的 shuffle
    toolbox、library 的组织
    toolbox、library 的组织
    WinEdt && LaTex(三)—— 宏包
  • 原文地址:https://www.cnblogs.com/xuanxuanlove/p/9925483.html
Copyright © 2020-2023  润新知