单循环链表——python

class Node(object):
    "单循环链表节点"
    def __init__(self,item):
        self.item=item
        self.next=None

class singlecirclelist(object):
    """单循环链表"""
    def __init__(self):
        self._head=None

    # is_empty() 判断链表是否为空
    def is_empty(self):
        cur=self._head
        return cur is None

    #链表长度
    def length(self):
        cur=self._head
        if cur==None:#为空
            count=0
            return count
        count=1
        while cur.next!=self._head:#不为空
            cur=cur.next
            count+=1
        return count

    # 遍历输出
    def printlist(self):
        cur=self._head
        if cur==None:
            return
        else:
            while cur.next!=self._head:
                print(cur.item)
                cur=cur.next
            print(cur.item)
    # add(item) 在头部添加一个节点
    def add(self,item):
        node=Node(item)
        cur=self._head
        if cur==None:
            self._head = node
            node.next = self._head
            return
        else:
            while cur.next!=self._head:
                cur=cur.next
            cur.next=node
            node.next=self._head
            self._head=node

    # append(item) 在尾部添加一个节点
    def append(self,item):
        node = Node(item)
        cur = self._head
        if cur == None:
            self._head = node
            node.next = self._head
            return
        else:
            while cur.next!=self._head:
                cur=cur.next
            cur.next=node
            node.next=self._head

    # 指定位置添加
    def insert(self,pos,item):
        if not isinstance(pos,int):
            raise TypeError
        if pos <= 0:#表头添加
            self.add(item)
        elif pos >= self.length():#表尾添加
            self.append(item)
        else:
            cur=self._head
            node=Node(item)
            count=0
            while count<pos-1:
                count+=1
                cur=cur.next
            node.next=cur.next
            cur.next=node

    # remove(item) 删除一个节点
    def remove(self,item):
        cur = self._head
        if cur==None:
            return
        pre = None
        while cur.next != self._head:
            if cur.item == item:
                if cur == self._head:
                    rear = self._head
                    while rear.next != self._head:
                        rear = rear.next

                    # rear指向的就是尾部节点
                    rear.next = cur.next
                    self._head = cur.next
                    return
                pre.next = cur.next
            pre = cur
            cur = cur.next
        #  cur指向的是尾部节点
        if cur.item == item:
            if cur == self._head:
                self._head = None
                return
            pre.next = cur.next

    # search(item) 查找节点是否存在
    def search(self,item):
        cur=self._head
        if cur==None:
            print(False)
            return
        while cur.next != self._head:
            if cur.item==item:
                print(True)
                return
            cur=cur.next
        if cur.item==item:
            print(True)
            return
        print(False)
        return



if __name__=="__main__":
    s=singlecirclelist()
    s.add(1)
    s.add(2)
    s.add(3)
    s.search(4)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
循环链表是一种特殊的链表,它的最后一个节点的 next 指针指向头节点,形成一个环。 以下是 Python 实现循环链表的代码: ```python class Node: def __init__(self, data): self.data = data self.next = None class CircularLinkedList: def __init__(self): self.head = None def add_node(self, data): new_node = Node(data) if self.head is None: self.head = new_node new_node.next = self.head else: current = self.head while current.next != self.head: current = current.next current.next = new_node new_node.next = self.head def display(self): if self.head is None: print("List is empty") else: current = self.head while current.next != self.head: print(current.data, end=" ") current = current.next print(current.data, end=" ") def remove_node(self, key): if self.head is None: print("List is empty") return if self.head.data == key: current = self.head while current.next != self.head: current = current.next current.next = self.head.next self.head = self.head.next else: current = self.head prev = None while current.next != self.head: prev = current current = current.next if current.data == key: prev.next = current.next current = current.next def search_node(self, key): if self.head is None: print("List is empty") return current = self.head while current.next != self.head: if current.data == key: print("Node found") return current = current.next if current.data == key: print("Node found") return print("Node not found") # Test the CircularLinkedList class clist = CircularLinkedList() clist.add_node(1) clist.add_node(2) clist.add_node(3) clist.add_node(4) print("Original List") clist.display() clist.remove_node(4) print("\nList after removing node with data=4") clist.display() clist.search_node(2) clist.search_node(5) ``` 输出结果: ``` Original List 1 2 3 4 List after removing node with data=4 1 2 3 Node found Node not found ``` 上面的代码实现了循环链表的基本操作,包括添加节点、删除节点、查找节点和显示链表。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值