1 # dual chain
2 class the Node (Object): . 3 # precursors subsequent data
. 4 DEF the __init__ (Self, Data): . 5 self.pre = None . 6 # precursor
. 7 self.data = self.data . 8 # data
. 9 self.next = None 10 # successor
. 11
12 is class DoubleLianBiao (Object): 13 is # define a doubly-linked list node connecting
14 DEF the __init__ (Self, node = None): 15 # Points to node if not transmitted using None
16 self._head = Node . 17 # node definition head node, the transmission point to point to an object instance of the class
18 is
. 19 DEF is_empty (Self): 20 is '' ' list is empty ' ''
21 is return self._head iS None 22 is
23 is DEF length (Self): 24 '' ' queries the list length ' ''
25 CUR = self._head 26 is # CUR pointer to the current
27 COUNT = 0 28 # record length
29 the while CUR ! =None: 30 # current point is not none
31 is COUNT +. 1 =
32 # Number plus. 1
33 is CUR = cur.next 34 is # shall mean for the mobile node
35 # If the first node is None, still returns 0
36 return COUNT 37 # returns the number of nodes
38 is
39
40 DEF Travel (Self): 41 is '' ' to traverse the entire list ' ''
42 is CUR = self._head 43 is the while CUR =! None: 44 is # if not empty then the print data
45 Print (cur.data, End = " " ) 46 is # print data
47 CUR = cur.next 48 # traversed downward
49
50
51 is DEF the Add (Self, Data): 52 is '' ' list header additive element ' ''
53 = node the node (Data) 54 is # node points to the head
55 node.next = self._head 56 is # head node as the next element
57 is self._head = node 58 # the node as the primary node
59 node.next. pre =node 60 # after pull the right hand so that node node pointing to node
61 is
62 is DEF the append (Self, Data): 63 is '' ' list tail additive element ' ''
64 node = the Node (Data) 65 # Create a node
66
67 # special cases The first node is empty
68 IF self.is_empty (): 69 self._head = node 70 # head node node
71 is the else : 72 CUR = self._head 73 is the while cur.next =! None: 74 = CUR cur.next 75 # cur.next.data node.data =
76 # No data is
77 cur.next = Node 78 # add a node
79 node.pre = CUR 80 # connection left hands
81
82 DEF INSERT (Self, POS, Data): 83 '' ' designated position additional element ' ''
84 # If zero position
85 IF POS <= 0: 86 self.add (Data) 87 # add nodes
88 elifPOS> (self.length () -. 1 ): 89 # to the last element
90 self.append (Data) 91 is # add a node
92 the else : 93 Node = the Node (Data) 94 index = 0 95 CUR = self._head 96 the while index < pos: 97 # traverse to a position before pos
98 index. 1 = +
99 CUR = cur.next 100 # continued downward movement
101 node.next =CUR 102 # right: node connected to the node currently pointed to
103 node.pre = cur.pre 104 # left: before a node node to the previous node of the current position
105 cur.pre.next = node 106 # left: the current position next node before the node is a node the node
107 cur.pre = node 108 # right: a node before the current node position is node
109
110 DEF remove (Self, Data): 111 '' ' delete nodes ' ''
112 CUR = self._head 113 # before setting cursor indicates a cursor
114 the while CUR! =None: 115 IF cur.data == Data: 1 16 # If cur point node to be deleted is the node
117 IF cur == self._head: 1 18 # if the data for the first data node
119 self._head = cur.next 120 # skip CUR
121 IF cur.next =! None: 122 # If only one node, None no pre attribute
123 cur.next.pre = None 124 # delete node is the head node behind None
125 the else : 126 # , if not the first Node
127 = cur.pre.next cur.next 128 # left: before the current node of a node after a node after a node of the current node
129 IF ! cur.next = None: 130 # to see whether it is the last node, None no pre attribute
131 is cur.next.pre = cur.pre 132 # right: before the current node to the next node before the node is a current node
133 BREAK
134 # to find the data out of the loop
135 the else : 136 # If not found data
137 CUR = cur.next 138 # moves downward
139
140 DEF Search (Self, data): 141 '' ' to find whether there is a node ' ''
142 CUR = self._head 143 # point to the head node
144 the while cur.next =! None: 145 # If the next node is not null
146 IF cur.data == Data: 147 # If the data is found
148 return True 149 the else : 150 CUR = cur.next 151 # continue to look down
152 return False 153 # does not find the data
2020-04-14