one-way circular linked list
A variant of the singly linked list is a one-way circular linked list. The next field of the last node in the linked list is no longer None, but points to the head node of the linked list.
operate
- is_empty() determines whether the linked list is empty
- length() returns the length of the linked list
- travel() traverse
- add(item) adds a node to the header
- append(item) adds a node to the end
- insert(pos, item) adds a node at the specified position pos
- remove(item) removes a node
- search(item) finds whether the node exists
accomplish
# node, including: element and address of next node class Node(object): def __init__(self, elem): self.elem = elem self.next = None class SingleLoopLinkList(object): '''One-way circular linked list''' def __init__(self, node=None): self.__head = node def is_empty(self): '''Is the linked list empty''' return self.__head == None def length(self): '''Length of linked list''' # If the list is empty, return 0 if self.is_empty(): return 0 count = 1 cur = self.__head while cur.next != self.__head: count += 1 cur = cur.next return count def travel(self): '''traverse the entire linked list''' # If the linked list is empty, return empty if self.is_empty(): return cur = self.__head while cur.next != self.__head: print(cur.elem, end=' ') cur = cur.next # print the last element print(cur.elem, end=' ') def add(self, item): '''Add an element to the head of the linked list''' node = Node(item) if self.is_empty(): self.__head = node node.next = self.__head else: # Added node points to __head node.next = self.__head # Move to the tail of the linked list and point the next of the tail node to node cur = self.__head while cur.next != self.__head: cur = cur.next cur.next = node # __head points to add node self.__head = node def append(self, item): '''Add an element to the end of the linked list''' node = Node(item) if self.is_empty(): self.__head = node node.next = self.__head # If not empty, find the last node to add else: cur = self.__head while cur.next != self.__head: cur = cur.next # Point the tail node next to node cur.next = node # Point the added node next to the head node node.next = self.__head def insert(self, pos, item): '''Add element at specified position''' # If the position is before the first element, add it to the head if pos <= 0: self.add(item) # If the position is after the last element, add it to the end elif pos > self.length() - 1: self.append(item) else: cur = self.__head count = 0 while count < pos -1: count += 1 cur = cur.next node = Node(item) # First point the next of the new node node to the node at the insertion position node.next = cur.next # Point the next node of the previous node at the insertion position to the new node cur.next = node def remove(self, item): '''Delete node''' if self.is_empty(): return cur = self.__head pre = None while cur.next != self.__head: if cur.elem == item: # if it is the head node if not pre: # Need to change the point of the tail node, so first find the tail node rear = self.__head while rear.next != self.__head: rear = rear.next self.__head = cur.next rear.next = self.__head # If it is an intermediate node else: # Point the previous node at the deletion position next to the next node at the deletion position pre.next = cur.next return else: pre = cur cur = cur.next # exit the loop # if tail node if cur.elem == item: # if there is only one node if cur == self.__head: self.__head = None else: # The previous node next of the tail node points to the head node pre.next = self.__head def search(self, item): '''Find if the node exists''' # If empty, return -1 directly if self.is_empty(): return -1 else: cur = self.__head count = 0 while cur.next != self.__head: if cur.elem == item: return count else: cur = cur.next count += 1 # Determine if the last node is an item if cur.elem == item: return count return -1 # test if __name__ == '__main__': sll = SingleLoopLinkList() # Determine if it is empty print(sll.is_empty()) # Get the length of the linked list print(sll.length()) # Traverse the linked list sll.travel() # header add element sll.add(1) sll.add(100) print(sll.is_empty()) print(sll.length()) sll.travel() print() # add element at the end sll.append(300) sll.append(400) sll.travel() print() # Add the element at the specified location sll.insert(3, 200) sll.insert(6, 500) sll.travel() print() # delete node sll.remove(1) sll.travel() print() # find node print(sll.search(200))