1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| class Node(): """节点类""" def __init__(self, item, per=None, next=None): self.per = per self.item = item self.next = next
class LinkList(): """循环链表"""
def __init__(self, node=None): self.head = node
def is_empty(self): """判断链表是否为空""" return True if self.head else False
def length(self): """返回链表长度""" num = 0 node = self.head if node == None: return num num += 1 while node.next!=self.head: node = node.next num += 1 return num
def add(self, item): """链表头部添加""" self.head = Node(item, per=self.head.per, next=self.head)
def travel(self): """遍历整个链表""" node = self.head if node == None: return print(node.item) while node.next!=self.head: node = node.next print(node.item)
def addend(self, item): """链表尾部添加元素""" node = self.head if node == None: node = Node(item) node.per=node node.next=node self.head = node return while node.next: node = node.next node.next = Node(item, node, node.next)
def insert(self, index, item): """指定位置添加""" if index == 0: self.add(item) return node = self.head try: for i in range(index): node = node.next node.next = None(item, node, node.next) except: assert "索引超出范围"
def remove(self, index): """删除指定节点""" if index == 0: self.head = self.head.next return node = self.head try: for i in range(index-1): node = node.next node.next = node.next.next node.next.per=node except: assert "索引超出范围"
def search(self, item): """查找节点是否存在""" index = 0 node = self.head while node.next: if node.item==item: return index index += 1 node = node.next return "该元素不存在"
|