page104.html

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

HTML
47
字号
<HTML><HEAD><TITLE>prepend Method</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="tex2html2412" HREF="page105.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2410" HREF="page97.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2404" HREF="page103.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html2414" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION004370000000000000000"><tt>prepend</tt> Method</A></H2><P>To <em>prepend</em><A NAME=3913>&#160;</A> an element to a linked listis to insert that element in front of the first element of the list.The prepended list element becomes the new head of the list.Program&nbsp;<A HREF="page104.html#proglinkedListf"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> gives the algorithm for the<tt>prepend</tt> method of the <tt>LinkedList</tt> class.<P><P><A NAME="4154">&#160;</A><A NAME="proglinkedListf">&#160;</A> <IMG WIDTH=575 HEIGHT=180 ALIGN=BOTTOM ALT="program3917" SRC="img633.gif"  ><BR><STRONG>Program:</STRONG> <tt>LinkedList</tt> class <tt>prepend</tt> method.<BR><P><P>The <tt>prepend</tt> method first creates a new <tt>LinkedList.Element</tt>.Its <tt>_datum</tt> instance attribute is initialized with the value to be prependedto the list, <tt>item</tt>; andthe <tt>_next</tt> instance attribute refers to thefirst element of the existing list by initializing it withthe current value of <tt>_head</tt>.If the list is initially empty,both <tt>_head</tt> and <tt>_tail</tt> refer to the new element.Otherwise, just <tt>_head</tt> needs to be updated.<P>Note, the <tt>LinkedList.Element</tt> instance is initializedby calling its <tt>__init__</tt> method.In Section&nbsp;<A HREF="page99.html#secfdslistelem"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> the running time of the <tt>__init__</tt> methodwas determined to be <I>O</I>(1).And since the body of the <tt>prepend</tt> method addsonly a constant amount of work,the running time of the <tt>prepend</tt> method is also <I>O</I>(1).<P><HR><A NAME="tex2html2412" HREF="page105.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2410" HREF="page97.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2404" HREF="page103.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html2414" 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 + -
显示快捷键?