数据结构之单向循环链表

单向循环列表,即链表中最后一个节点的next不再指向None,而是指向链表的头节点。

单向循环列表的操作以及代码实现:
操作
is_empty() 判断链表是否为空
length() 返回链表的长度
travel() 遍历
add(item) 在头部添加一个节点
append(item) 在尾部添加一个节点
insert(pos, item) 在指定位置pos添加节点
remove(item) 删除一个节点
search(item) 查找节点是否存在

代码实现

# -*- coding: utf-8 -*-
"""
Created on Mon Jul  8 15:19:41 2019

@author: Omega_Sendoh
"""

class Node(object):
    def __init__(self,item):
        self.item = item
        self.next = None
        
class SingleCycLink(object):
    def __init__(self):
        self.head = None
    
    def is_empty(self):
        return self.head == None
    
    def __len__(self):
        if self.is_empty():
            return 0
        cur = self.head
        count = 1 
        while cur.next != self.head:
            count+=1
            cur = cur.next
        return count
    
    def trvel(self):
        if not self.is_empty():
            cur = self.head
            while cur.next!=self.head:
                print(cur.item,end='')
                cur = cur.next
            print(cur.item)
        else:
            print('链表为空')
            
    def add(self,item):
        #在头部添加
        node = Node(item)
        if self.is_empty():
            self.head = node
            node.next = self.head
        else:
            cur = self.head
            while cur.next != 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
        else:
            cur = self.head
            while cur.next!=self.head:
                cur = cur.next
            cur.next = node
            node.next = self.head
            
    def insert(self,index,item):
        #指定位置添加元素
        #0,1,2,3,4,5
        if index<=0:
            self.add(item)
        elif index>=len(self):
            self.append(item)
        else:
            node = Node(item)
            count = 0
            cur = self.head
            while count<index-1:
                count+=1
                cur = cur.next
            node.next = cur.next
            cur.next = node
            
    def remove(self,item):
        #删除指定元素
        #若第一个元素就是指定元素,最后一个元素指向第二个元素,第二个元素为self.head
        #若指定元素不为第一个,判断当前元素的下一个元素是否为指定元素,若是,当前元素指向下下一个元素,即cur.next = cur.next.next
        if self.head ==None:
            print('空链表')
        else:
            cur = self.head
            pre = self.head
            if cur.item == item:
                while cur.next !=self.head:
                    cur = cur.next
                cur.next = pre.next
                self.head = pre.next
            else:
                while cur.next.item!=item:
                    cur=cur.next
                cur.next = cur.next.next
                
    def search(self,item):
        #判断查找的元素在节点中是否存在,返回bool类型
        cur = self.head
        while cur.next is not None:
            if cur.item == item:
                return True
            else:
                cur = cur.next
        else:
            return False
            
    

       
            
link  = SingleCycLink()
print('链表长度:',len(link))

link.trvel()

print(link.is_empty())

link.append(4)
link.add(1)
link.insert(1,'hello')
link.append(2)
link.append(1)

print('链表长度:',len(link))
link.trvel()
          
link.add(5) 
link.remove(4)
link.trvel()    

运行结果如图所示:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值