python实现单向循环链表

普通单链表和单向循环链表的不同之处在于单向循环链表成环,在这种情况下链表指针移动到尾部的判定条件要发生改变,增删改查也要发生改变,但还是以本博客的"python实现单链表"中的代码为基础来实现的。

python实现单向循环链表_第1张图片

# 单向循环链表
class Node(object):
    """节点类"""
    def __init__(self,elem):
        self.elem = elem
        self.next = None
class SingLinkList(object):
    """单向循环链表类"""
    def __init__(self,node=None):
        self.__head = node
        if node:
            node.next = node
    def is_empty(self):
        """判断链表是否为空"""
        return self.__head == None

    def length(self):
        """返回链表的长度"""
        #如果是空链表则返回0
        if self.is_empty():
            return 0
        #cur游标移动,从而实现遍历元素的功能
        cur = self.__head
        #count用来计数
        count = 1
        while cur.next != self.__head:
            count += 1
            #让cur游标可以向下移动
            cur = cur.next
        return count

    def travel(self):
        """遍历整个链表"""
        if self.is_empty():
            return
        #建立游标等于起始节点
        cur = self.__head
        while cur.next != self.__head:
            print(cur.elem,end=" ")
            cur = cur.next
        print(cur.elem,end="\n")
    def add(self,item):
        """链表头部添加元素"""
        node = Node(item)
        if self.is_empty():
            self.__head = node
            node.next = node
            return
        else:
            cur = self.__head
            while cur.next != self.__head:
                cur = cur.next
            # self.__head记录了它指向的下一个元素的地址,就是把__head后面元素的值给了新节点
            node.next = self.__head
            self.__head = node
            cur.next = self.__head

    def append(self,item):
        """链表尾部添加元素"""
        node = Node(item)  #建立节点
        #链表是否为空链表
        if self.is_empty():
            self.__head = node
            node.next = node
        else:
            cur = self.__head   #设置指向头结点的游标
            while cur.next != self.__head:
                cur = cur.next
            #添加节点到尾部
            node.next = self.__head
            cur.next = node

    def insert(self,pos,item):
        """在指定位置添加元素"""
        #指向不是头部元素,self.__head的地址
        # 为下一个元素,所以pre为下一个元素
        if pos <= 0:
            #认为是头插法
            self.add(item)
        elif pos > (self.length()-1):
            self.append(item)
        else:
            pre = self.__head
            count = 0
            while count < (pos-1):
                count+=1
                pre = pre.next
            node = Node(item)
            node.next = pre.next
            pre.next = node

    def remove(self,item):
        """删除元素"""
        if self.is_empty():
            return
        cur = self.__head
        pre = None
        while cur.next != self.__head:
            if cur.elem == item:
                #判断此节点是否为头节点
                #是头节点
                if cur == self.__head:
                    #设置尾节点并找到尾部
                    rear = self.__head
                    while rear.next != self.__head:
                        rear = rear.next
                    #到这里是rear指向尾部
                    self.__head = cur.next  #删除第一个元素
                    rear.next = self.__head #尾部指向第二个元素
                    # self.__head = cur.next
                else:
                    #中间节点
                    pre.next = cur.next
                return
            else:
                #cur,pre游标都向下移动
                pre = cur
                cur = cur.next
        #删除尾节点
        if cur.elem == item:
            if cur == self.__head:
                self.__head = None
            else:
                pre.next = cur.next
    def search(self,item):
        """查找结点是否存在"""
        #如果是一个空链表
        if self.is_empty():
            return False
        cur = self.__head
        while cur.next != self.__head:
            if cur.elem == item:
                return True
            else:
                cur = cur.next
        return False
if __name__ == '__main__':
    ll = SingLinkList()
    print(ll.is_empty())
    print(ll.length())

    ll.append(1)
    print(ll.is_empty())
    print(ll.length())

    ll.append(2)
    ll.append(3)
    ll.append(4)
    ll.append(5)
    ll.travel()
    ll.insert(-1,20)
    ll.travel()
    ll.insert(2,30)
    ll.travel()
    ll.insert(10,200)
    ll.travel()

    ll.remove(20)
    ll.travel()
    ll.remove(200)
    ll.travel()

 

你可能感兴趣的:(数据结构与算法)