page172.html

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

HTML
50
字号
<HTML><HEAD><TITLE>Creating a List and Inserting Items</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="tex2html3191" HREF="page173.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html3189" HREF="page170.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html3183" HREF="page171.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html3193" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION007112000000000000000">Creating a List and Inserting Items</A></H3><P>Program&nbsp;<A HREF="page170.html#progorderedListAsArraya"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the <tt>__init__</tt> methodof the <tt>OrderedListAsArray</tt> class.In addition to <tt>self</tt>,the <tt>__init__</tt> method takes a single argument which specifiesthe length of array to use in the representation of the ordered list.Thus if we use an array-based implementation,we need to know when a list is declaredwhat will be the maximum number of items in that list.The <tt>__init__</tt> method initializes the <tt>_array</tt> variable asan array with the specified length.The running time of the <tt>__init__</tt> method is clearly <I>O</I>(<I>n</I>),where  <IMG WIDTH=64 HEIGHT=10 ALIGN=BOTTOM ALT="tex2html_wrap_inline60687" SRC="img662.gif"  >.<P>Program&nbsp;<A HREF="page172.html#progorderedListAsArrayb"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the <tt>insert</tt> methodof the <tt>OrderedListAsArray</tt> class.The <tt>insert</tt> method is implemented by all searchable containers.Its purpose is to put an object into the container.The obvious question which arises is,where should the inserted item be placed in the ordered list?The simple answer is, at the end.<P><P><A NAME="8854">&#160;</A><A NAME="progorderedListAsArrayb">&#160;</A> <IMG WIDTH=575 HEIGHT=180 ALIGN=BOTTOM ALT="program8675" SRC="img735.gif"  ><BR><STRONG>Program:</STRONG> <tt>OrderedListAsArray</tt> class <tt>insert</tt> method.<BR><P><P>In Program&nbsp;<A HREF="page172.html#progorderedListAsArrayb"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> we see that the <tt>insert</tt>method simply adds the new item to the end of the list,provided there is still room in the array.Normally, the array will not be full,so the running time of this method is <I>O</I>(1).<P><HR><A NAME="tex2html3191" HREF="page173.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html3189" HREF="page170.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html3183" HREF="page171.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html3193" 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 + -
显示快捷键?