📄 page108.html
字号:
<HTML><HEAD><TITLE>insertAfter and insertBefore Methods</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="tex2html2454" HREF="page109.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2452" HREF="page97.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2448" HREF="page107.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2456" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION0043110000000000000000"><tt>insertAfter</tt> and <tt>insertBefore</tt> Methods</A></H2><P>Consider the methods <tt>insertAfter</tt> and <tt>insertBefore</tt>of the <tt>LinkedList.Element</tt> class shown in Program <A HREF="page108.html#proglinkedListj"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.Both methods take a single argument that specifiesan item to be inserted into the list.The given item is inserted either in front ofor immediately following this list element.<P><P><A NAME="4166"> </A><A NAME="proglinkedListj"> </A> <IMG WIDTH=575 HEIGHT=428 ALIGN=BOTTOM ALT="program4015" SRC="img637.gif" ><BR><STRONG>Program:</STRONG> <tt>LinkedList.Element</tt> class <tt>insertAfter</tt> and <tt>insertBefore</tt> methods.<BR><P><P>The <tt>insertAfter</tt> method is almost identical to <tt>append</tt>.Whereas <tt>append</tt> inserts an item after the tail,<tt>insertAfter</tt> inserts an item after an arbitrary list element.Nevertheless, the running time of <tt>insertAfter</tt> is identical tothat of <tt>append</tt>, i.e., it is <I>O</I>(1).<P>To insert a new item <em>before</em> a given list element,it is necessary to traverse the linked list starting from the headto locate the list element that precedes the given list element.In the worst case, the given element is the at the tail of the listand the entire list needs to be traversed.Therefore, the running time of the <tt>insertBefore</tt> method is <I>O</I>(<I>n</I>).<P><HR><A NAME="tex2html2454" HREF="page109.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2452" HREF="page97.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2448" HREF="page107.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2456" 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -