单向循环链表:单向循环链表相对于单链表,区别在于链表中最后一个节点的next区域不在指向None,而是指向链表的头节点。
常用操作:
- is_empty() :判断链表是否为空,空则返回True,否则返回False。
- length(): 返回链表的长度。
- print():遍历链表并打印其中的元素。
- add(item):往链表头部添加元素。
- append(item):往链表尾部添加元素。
- insert(pos, item):往指定位置添加元素。
- remove(item):删除指定的某个元素,删除成功则返回该元素,否则返回None。
- search(item):查找某个元素是否存在,存在则返回其索引,否则返回-1。
Python代码实现:
# -*- coding: utf-8 -*-
"""
--------------------------------------------------------
# @Version : python3.7
# @Author : wangTongGen
# @File : single_cycle_list.py
# @Software: PyCharm
# @Time : 2019/5/13 11:29
--------------------------------------------------------
"""
class Node(object):
def __init__(self, item=None):
self.val = item
self.next = None
if item:
self.next = item
class SingleCycleList(object):
"""单向循环链表"""
def __init__(self, node=None):
self.__head = node
def is_empty(self):
"""判断是否为空"""
return self.__head==None
def length(self):
"""返回链表长度"""
if self.is_empty():
return 0
count = 1
# cur: 游标 用来移动遍历节点
cur = self.__head
while cur.next != self.__head:
count += 1
cur = cur.next
return count
def print(self):
"""遍历整个链表"""
if self.is_empty():
return
cur = self.__head
while cur.next != self.__head:
print(cur.val, end=" ")
cur = cur.next
print(cur.val)
def add(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
self.__head = node
cur.next = node
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):
"""在指定位置添加元素"""
if pos < 0:
self.add(item)
elif pos > self.length()-1:
self.append(item)
else:
node = Node(item)
cur = self.__head
count = 0
while count < pos-1:
count += 1
cur = cur.next
node.next = cur.next
cur.next = node
def remove(self, item):
"""删除某个元素"""
if self.is_empty():
return
pre = None
cur = self.__head
while cur.next != self.__head:
if cur.val == item:
# 先判断此节点是否是头节点
if cur == self.__head:
# 若是头节点,得先找到尾节点
rear = self.__head
while rear.next != self.__head:
rear = rear.next
self.__head = cur.next
rear.next = cur.next
else:
pre.next = cur.next
return item
else:
pre = cur
cur = cur.next
# 退出循环,指向尾节点
if cur.val == item:
if cur.next == self.__head:
# 链表只有一个节点
self.__head = None
else:
pre.next = self.__head
return item
return None
def search(self, item):
"""查找节点是否存在"""
if self.is_empty():
return -1
count = 0
cur = self.__head
while cur.next != self.__head:
if cur.val == item:
return count
cur = cur.next
count += 1
if cur.val == item:
return count
return -1
if __name__ == '__main__':
single_cycle = SingleCycleList()
print(single_cycle.is_empty())
print(single_cycle.length())
single_cycle.add(99)
print(single_cycle.length())
single_cycle.add(1)
single_cycle.append(100)
single_cycle.insert(1,11)
single_cycle.print()
print(single_cycle.remove(99))
single_cycle.print()
print(single_cycle.search(100))
来源:CSDN
作者:偶尔也吃鸡
链接:https://blog.csdn.net/qq_41689620/article/details/90176066