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()

运行结果:


猜你喜欢

转载自www.cnblogs.com/xuanxuanlove/p/9925483.html
今日推荐