单项循环链表single_cycle_link_list(python)

#coding=utf-8

class Node(object):
    #节点
    def __init__(self.elem):
        self.next=None
        self.elem=elem

class SingleListLink(object):
    """单项单链表"""
    def __init__(self,node=None):
        self.head=node
        if node:
            node.next=node

    def is_empty(self):
        return self.head==None

    def lenth(self):
        """链表长度"""
        #cur是一个游标,用来移动遍历节点
        cur=self.head
        count=0
        while cur!=None:
            count+=1
            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
            # 退出循环,cur指向尾节点,但尾节点的元素未打印
        print(cur.elem)
    def add(self,item):
        """链表头部添加元素"""
        node=Node(item)
        if self.is_empty():
        	node.next=self.head
            self.head=node
        else:
            cur=self.head
            while cur.next!=self.head:
                cur=cur.next
            node.next=self.head
            self.head=node
            cur.next=self.head
    def append(self,item):
        """链表尾部添加元素"""
        node=Node(item)
        cur=self.head
        if self.is_empty:
            node.next=self.head
            self.head=node
        else:
            while cur.next!=self.head:
                cur=cur.next
            cur.next=node
            node.next=self.head

    def insert(self,pos,item):
        """在指定位置添加元素"""
        #pos 从0开始
        if pos<0:
            self.add(item)
        elif pos>(self.lenth-1):
            self.append(item)
        else:
            node=Node(item)
            pre=self.head
            count=0
            while count<(pos-1):
                count+=1
                pre=pre.next
            node.next=pre.next
            pre.next=node

    def remove(self,item):
        node=Node(item)
        if self.is_empty():
            return
        cur=self.head
        pre=None
        while cur.next!=None:
            if cur.elem==item:
                if cur==self.head:
                    rear=self.head
                    while rear.next!=self.headad:
                        rear=rear.next
                    self.head=cur.next
                    rear.next=self.head
                else:
                    pre.next=cur.next
                return
            else:
                pre=cur
                cur=cur.next
        # 退出循环,cur指向尾节点
        if cur.elem==item:
            if cur==self.head:
                #链表只有一个节点
                self.head=None
            else:
                pre.next=self.head
    def search(self,item):
        if self.is_empty():
            return False
        cur=self.head
        while cur.next!=self.head:
            if cur.elem==item:
                return True
            cur=cur.next
        if cur.elem==item:
            return True
            return False
            cur=cur.next




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值