zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    javascript 备忘 细节 相关
    内存泄漏
    css相关 细节 优化 备忘
    nodeType
    事件冒泡 模型
    两个感叹号作用
    非冒泡事件
    DOM 对象
    Quartz2D 之 绘制文本
    Voilin 与 乐谱
  • 原文地址:https://www.cnblogs.com/xuanxuanlove/p/9925483.html
Copyright © 2011-2022 走看看