page186.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 42 行
HTML
42 行
<HTML><HEAD><TITLE>Inserting an Item at an Arbitrary Position</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="tex2html3351" HREF="page187.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html3349" HREF="page179.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html3343" HREF="page185.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html3353" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION007127000000000000000">Inserting an Item at an Arbitrary Position</A></H3><P>Having determined the position of an item in an ordered list,we can make use of that position to insert items into the middle of the list.Two methods are specifically provided for this purpose--<tt>insertAfter</tt> and <tt>insertBefore</tt>.In addition to <tt>self</tt>,both of these take a single argument--the object to be inserted into the list.<P><P><A NAME="9898"> </A><A NAME="progorderedListAsLinkedListf"> </A> <IMG WIDTH=575 HEIGHT=218 ALIGN=BOTTOM ALT="program9521" SRC="img758.gif" ><BR><STRONG>Program:</STRONG> <tt>OrderedListAsLinkedList.Cursor</tt> class <tt>insertAfter</tt> method.<BR><P><P>Program <A HREF="page186.html#progorderedListAsLinkedListf"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> gives the implementation for the<tt>insertAfter</tt> method of the <tt>OrderedListAsLinkedList.Cursor</tt> class.This method simply calls the <tt>insertAfter</tt> methodprovided by the <tt>LinkedList</tt> class.Assuming no exceptions are thrown,the running time for this method is <I>O</I>(1).<P>The implementation of <tt>insertBefore</tt> is not shown--its similarity with <tt>insertAfter</tt> should be obvious.Since it must call the <tt>insertBefore</tt> methodprovided by the <tt>LinkedList</tt> class,we expect the worst case running time to be <I>O</I>(<I>n</I>),where <IMG WIDTH=78 HEIGHT=9 ALIGN=BOTTOM ALT="tex2html_wrap_inline61037" SRC="img743.gif" >.<P><HR><A NAME="tex2html3351" HREF="page187.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html3349" HREF="page179.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html3343" HREF="page185.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html3353" 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 © 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 + -
显示快捷键?