page98.html

来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 56 行

HTML
56
字号
<HTML><HEAD><TITLE>An Implementation</TITLE></HEAD><BODY bgcolor="#FFFFFF"> <a href="../index.html" target="_top"><img src="../icons/usins.gif" alt="Logo" align=right></a><b>Data Structures and Algorithms with Object-Oriented Design Patterns in Python</b><br><A NAME="tex2html2346" HREF="page99.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2344" HREF="page97.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2338" HREF="page97.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html2348" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION004310000000000000000">An Implementation</A></H2><P>Figure&nbsp;<A HREF="page98.html#figlinklist3"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> illustrates thethe singly-linked list scheme we have chosen to implement.Two related structures are used.The elements of the list are represented using instancesof the <tt>Element</tt> class which comprises three instance attributes,<tt>_list</tt>, <tt>_datum</tt> and <tt>_next</tt>.The main structure is an instanceof the <tt>LinkedList</tt> class which also comprises two instance attributes,<tt>_head</tt> and <tt>_tail</tt>,which refer to the first and last list elements, respectively.The <tt>_list</tt> instance attribute of every <tt>Element</tt>contains a reference to the <tt>LinkedList</tt> instancewith which it is associated.The <tt>_datum</tt> instance attribute is used to refer to the objects in the list andthe <tt>_next</tt> instance attribute refers to the next list element.<P><P><A NAME="3771">&#160;</A><A NAME="figlinklist3">&#160;</A> <IMG WIDTH=575 HEIGHT=371 ALIGN=BOTTOM ALT="figure3600" SRC="img626.gif"  ><BR><STRONG>Figure:</STRONG> Memory representation of a linked list.<BR><P><P>Program&nbsp;<A HREF="page98.html#proglinkedLista"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the <tt>LinkedList.Element</tt> class.It is used to represent the elements of a linked list.It has three instance attributes, <tt>_list</tt>, <tt>_datum</tt> and <tt>_next</tt>,an <tt>__init__</tt> method and two properties, <tt>datum</tt> and <tt>next</tt>.<P><P><A NAME="4126">&#160;</A><A NAME="proglinkedLista">&#160;</A> <IMG WIDTH=575 HEIGHT=428 ALIGN=BOTTOM ALT="program3782" SRC="img627.gif"  ><BR><STRONG>Program:</STRONG> <tt>LinkedList.Element</tt> class.<BR><P><P>We can calculate the total storage required, <I>S</I>(<I>n</I>),to hold a linked list of <I>n</I> itemsfrom the class definitions given in Program&nbsp;<A HREF="page98.html#proglinkedLista"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>as follows:<P> <IMG WIDTH=500 HEIGHT=40 ALIGN=BOTTOM ALT="eqnarray3806" SRC="img628.gif"  ><P>In Python object identifiers (addresses) occupy a constant amount of space.Therefore, <I>S</I>(<I>n</I>)=<I>O</I>(<I>n</I>).<P><HR><A NAME="tex2html2346" HREF="page99.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2344" HREF="page97.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2338" HREF="page97.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html2348" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <P><ADDRESS><img src="../icons/bruno.gif" alt="Bruno" align=right><a href="../copyright.html">Copyright &#169; 2003</a> by <a href="../signature.html">Bruno R. Preiss, P.Eng.</a>  All rights reserved.</ADDRESS></BODY></HTML>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?