Home > other >  For help, the Data structure and algorithms in python, P279 - positional list ADT
For help, the Data structure and algorithms in python, P279 - positional list ADT

Time:11-22

The class _DoubleLinkedbase:
The class _Node:
__slots__="_element _next", ""," _prev
"Def __init__ (self, element, next, prev) :
Self. _element=element
Self. _next=next
Self. _prev=prev

Def __init__ (self) :
Self. _header=self. _Node (None, None, None)
Self. _tailer=self. _Node (None, None, None)
Self. _size=0
Self. _header. _next=self. _tailer
Self. _tailer. _prev=self. _header

Def __len__ (self) :
Return the self. _size

Def utilities like is_empty that (self) :
Return the self. The _size==0

Def _insert_between (self, e, the predecessor and successor) :
Newest=self. _Node (e, predecessor and successor)
Predecessor. _next=newest
Successor. _prev=newest
Self. _size +=1
Return newest

Def _delete_node (self, node) :
Predecessor=node. _prev
Successor=node. _next
Predecessor. _next=successor
Successor. _prev=predecessor
Self. _size -=1
Element=node. _element
Node. _prev=node. _next=node. _element=None
The return element


The class PositionList (_DoubleLinkedbase) :
The class Position:
"" "the an abstraction representing the location of a single element. "" ""

Def __init__ (self, container, node) :
Self _container=# container container is used for?
Self. _node=node

Def element (self) :
Return the self. _node. _element

Def __eq__ (self, other) :
The return type (other) is a type (self) and other. _node is self. _node

Def __ne__ (self, other) :
Return the not (self==other)

Def _validate (self, p) :
If not isinstance (p, the self. The Position) :
Raise TypeError (" p must be proper Position type ")
If p. _container is not self:
Raise ValueError (" p does not belong to this container ")
If p. _node. _next is None:
Raise ValueError (" p is no longer valid ")
Return p. _node

Def _make_position (self, node) :
If the node is the self. _header or node is self. _tailer:
Return None
The else:
Return the self. The Position (self, node) # is only one parameter node??

Def first (self) :
Return the self. _make_position (self) _header) _next)

Def last (self) :
Return the self. _make_position (self) _tailer) _prev)

Def before (self, p) :
The node=self. _validate (p)
Return the self. _make_position (node. _prev)

Def after (self, p) :
The node=self. _validate (p)
Return the self. _make_position (node. _next)

Def __iter__ (self) :
Cursor=self. The first ()
While the cursor is not None:
Yield cursor. Element ()
Cursor=self. After (cursor)

Def _insert_between (self, e, prenode, sucnode) :
The node=super (). _insert_between (e, prenode, sucnode)
Return the self. _make_position (node)

Def addfirst (self, e) :
Return the self. _insert_between (e, self. _header, self. _header. _next)

Def addlast (self, e) :
Return the self. _insert_between (e, self. _tailer. _prev, self. _tailer)

Def addbefore (self, p, e) :
The original=self. _validate (p)
Return the self. _insert_between (e, the original _prev, the original)

Def addafter (self, p, e) :
The original=self. _validate (p)
Return the self. _insert_between (e, the original, the original _next)

Def the delete (self, p) :
The original=self. _validate (p)
Return the self. _delete_node (the original)

Def the replace (self, p, e) :
The original=self. _validate (p)
T=what. _element
Original_element=e
Return t

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
The above for the definition of positional list are in the book, don't know much about the Position is how to obtain values corresponding to the location of the class, what role the Position inside the container?
Bosses, please help to explain, it is best to give a test code, thank you!!!!!
  • Related