美文网首页我爱编程
python实现循环单链表

python实现循环单链表

作者: 玄月府的小妖在debug | 来源:发表于2018-04-15 09:27 被阅读38次
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    #python实现循环单链表
    class Node:
        def __init__(self,initdata):
            self.__data=initdata
            self.__next=None
        def getData(self):
            return self.__data
        def getNext(self):
            return self.__next
        def setData(self,newData):
            self.__data=newData
        def setNext(self,newNext):
            self.__next=newNext
    class SinCycLinkedList:
        def __init__(self):
            self.head=Node(None)
            self.head.setNext(self.head)
        def add(self,item):
            temp=Node(item)
            temp.setNext(self.head.getNext())
            self.head.setNext(temp)
        def search(self,item):
            cur=self.head.getNext()
            while cur !=self.head:
                if cur.getData()==item:
                    return True
                cur=cur.getNext()
            return False
        def remove(self,item):
           pre=self.head
           while pre.getNext()!=self.head:
               cur = pre.getNext()
               if cur.getData() == item:
                   pre.setNext(cur.getNext())
               pre= pre.getNext()
    
        def empty(self):
            return self.head.getNext()==self.head
        def size(self):
            count=0
            cur=self.head.getNext()
            while cur != self.head:
                count+=1
                cur=cur.getNext()
            return count
    if __name__=='__main__':
        s=SinCycLinkedList()
        print('s.empty() == %s, s.size() == %s' % (s.empty(), s.size()))
        s.add(6)
        s.add(89)
        print('s.empty() == %s, s.size() == %s' % (s.empty(), s.size()))
    
        print s.search(6)
        s.remove(6)
        print('s.empty() == %s, s.size() == %s' % (s.empty(), s.size()))
    
    

    参考:

    用Python实现的数据结构与算法:链表

    相关文章

      网友评论

        本文标题:python实现循环单链表

        本文链接:https://www.haomeiwen.com/subject/syqdkftx.html