zoukankan      html  css  js  c++  java
  • 单循环链表

    class Node():
        def __init__(self,item):
            self.item=item
            self.next=None
    class CycleSingleLinkList():
        def __init__(self,node=None):
            self.__head=node
        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 is not self.__head:
                count+=1
                cur=cur.next
            return count
        def travel(self):
            if self.is_empty():
                print('')
                return
            cur=self.__head
            while cur.next is not self.__head:
                print(cur.item,end='')
                cur=cur.next
            print(cur.item)
        def search(self,item):
            if self.is_empty():
                return False
            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 add(self,item):
            node=Node(item)
            if self.is_empty():
                self.__head=node
                node.next=self.__head
            cur=self.__head
            while cur.next is not self.__head:
                cur=cur.next
            cur.next=node
            node.next=self.__head
            self.__head=node
        def append(self,item):
            node=Node(item)
            if self.is_empty():
                self.__head = node
                node.next=self.__head
            cur=self.__head
            while cur.next is not self.__head:
                cur=cur.next
            cur.next=node
            node.next=self.__head
        def insert(self,pos,item):
            node=Node(item)
            if pos<=0:
                self.add(item)
            elif pos>self.length():
                self.append(item)
            else:
                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
            cur=self.__head
            pre=None
            while cur.next is not self.__head:
                if cur.item==item:
                    if cur==self.__head:
                        tnode=self.__head
                        while tnode.next is not self.__head:
                            tnode=tnode.next
                        self.__head=cur.next
                        tnode.next=self.__head
                    else:
                        pre.next=cur.next
                        return
                pre=cur
                cur=cur.next
            if cur.item==item:
                if cur==self.__head:
                    self.__head=None
                else:
                    pre.next=self.__head
    
    if __name__ == '__main__':
        cc=CycleSingleLinkList()
        for i in range(10):
            cc.append(i)
        cc.travel()
        print(cc.length())
        cc.add(1)
        cc.travel()
        cc.insert(2,3)
        cc.travel()
        cc.remove(3)
        cc.travel()
  • 相关阅读:
    解决sqlite3 dos下显示中文乱码
    毕业两年
    成就感
    重构html的下拉框select
    ie6 select不兼容处理(转)
    全选删除确认改进
    GridView移动行变色
    gridview固定列的宽度并且能换行
    分页控件结合分页存储过程
    网页滚动条向下拉动奇慢的原因
  • 原文地址:https://www.cnblogs.com/zhangweijie01/p/10229822.html
Copyright © 2011-2022 走看看