Class Node (object): Def __init__ (self, data): Self.data = Data Self.next = None Self.pre = Nonecla SS Dlinklist (Object): Def __init__ (self, node=none): self.__head = Node Self.cur = Self.__head def is_ Empty (self): "" "The list is empty: return:" "" return self.__head = = None def __len__ (self): "" "List Length: return:" "Count = 0 cur = self.__head # While the loop condition is different, count should never With the beginning. Here the conditions can have two # cur none cur.next none, using cur none is because count=0 just satisfies the situation of the empty list while cur! = None:count + = 1 cur = cur.next return count def append (self, data): "" "Trailing add:p Aram data: : Return: "" "Node = node (data) if Self.is_empty (): Self.__head = node sel F.cur = self.__head return cur = self.__head while cur.next! = None:cur = Cur.next # when exiting the while loop, At this point the position of the current node cur is the last node Cur.next = node Node.pre = cur def add (self, data): "" "header added: return: "" "Node = node (data) if Self.is_empty (): Self.__head = node return Node.next = Self.__head self.__head.pre = node Self.__head = Node Self.cur = Self.__head def __iter__ (self): return self def __next__ (self): if self.cur! = None:data = Self.cur.data Self.cur = self.cur.next return Data else:raise stopiteration def pop (self): "" "POPs the element from the end: return:" "" pre = None cur = self.__head while cur.next! = None: Pre = cur cur = Cur.next pre.next = None def remove (self, data): "" "Delete specified value element Vegetarian:p Aram Data:: Return: "" "cur = self.__head pre = none while cur! = none: if cur.data = = DaTA: # If the head node if cur = = Self.__head:self.__head = Cur.next Cur.next.pre = None self.cur = self.__head # Intermediate node and tail node else: Pre.next = Cur.next Cur.next.pre = Pre Break else: Pre = cur cur = cur.next def insert (self, POS, data): "" "insert element at specified position, POS starting from 0:p ar Am POS::p Aram Data:: Return: "" "if pos = = 0:self.add (data) elif POS >= Len (self): self.append (data) Else:count = 0 node = node (data) cur = s Elf.__head pre = None while count < Pos:pre = Cur cur = cur.next Count + = 1 pre.next = node Node.pre = Pre Node.next = cur cur.pr E = Nodeif __name__ = = '__main__ ': L = dlinklist () l.append (1) l.append (2) L.add (3) L.insert (1,77) L.pop () for I in L: Print (i)
Summarize:
- For some logic judgments can be more complex, so you should first follow the normal logic to deal with the general situation, and then on the basis of the previous written code to consider the special situation
- While the condition of the while loop is related to the outside counter, the understanding of the while loop must be in place, and the value that the variable is in when the while loop exits should be the critical condition, the same asynchronous relationship of what condition goes into the loop, the loop body and the loop condition
- The object-oriented idea is reflected in this aspect, shielding the outside operation, some operations are done inside, encapsulation
- The nature of the Python variable, a=5, A is not 5 of this memory address alias, but open two space, a piece is 5, a piece of the address of 5
- Distinguish the use and reference of a variable, put it on the right side of the equals sign using this variable
Python implements a doubly linked list