Before reading this blog it is recommended to check out the video on the B-sitepython data structures and algorithms course seriesThe course does not implement the operation of bidirectional circular chain table, so I have implemented the operation of bidirectional circular chain table according to the chain table idea of the video, welcome to read and exchange, if there is any infringement, please contact the blogger!
The code is attached below:
class Node: def __init__(self, elem): = elem = None = None class DoubleCycleLinkList: def __init__(self, node=None): self.__head = node def is_empty(self): """Judgement of emptiness.""" if self.__head is None: return True return False def length(self): """Length of chain table""" if self.is_empty(): return 0 cur = self.__head count = 1 while is not self.__head: count += 1 cur = return count def travel(self): """Traversing a chained table.""" if self.is_empty(): return cur = self.__head while is not self.__head: print(, end=" ") cur = print(, end=" ") print("") def add(self, elem): """Head-in-the-sand.""" node = Node(elem) if self.is_empty(): self.__head = node = node = node else: self.__head. = node = self.__head.prev = self.__head self.__head.prev = node self.__head = node def append(self, elem): """Tail insertion.""" node = Node(elem) if self.is_empty(): self.__head = node = node = node else: = self.__head = self.__head.prev self.__head. = node self.__head.prev = node def insert(self, pos, elem): """Insert at any position (pos), counting from zero.""" if pos <= 0: (elem) elif pos > (() - 1): (elem) else: count = 0 cur = self.__head node = Node(elem) while count < (pos - 1): count += 1 cur = = = cur = node = node def remove(self, elem): """Delete a node, if there is more than one eligible node, just delete the first one""" if self.is_empty(): return cur = self.__head while is not self.__head: if == elem: if cur is self.__head: self.__head = = = else: = = break cur = if == elem: = self.__head = def search(self, elem): """Find a node.""" if self.is_empty(): return False cur = self.__head while is not self.__head: if == elem: return True cur = # while can't handle the tail node, so do the last tail node judgment if == elem: return True return False
to this article on the python data structure to achieve the two-way circular chain table operation of the example of the article is introduced to this, more related python two-way circular chain table operation content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!