page150.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 41 行
HTML
41 行
<HTML><HEAD><TITLE>__init__ and purge 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="tex2html2938" HREF="page151.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2936" HREF="page148.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2930" HREF="page149.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2940" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION006212000000000000000"><tt>__init__</tt> and <tt>purge</tt> Methods</A></H3><P>The definitions of the <tt>QueueAsArray</tt> class <tt>__init__</tt>and <tt>purge</tt> methods are given in Program <A HREF="page148.html#progqueueAsArraya"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.In addition to <tt>self</tt>,the <tt>__init__</tt> takes a single parameter, <tt>size</tt>,which specifies the maximum number of items that can be stored in the queue.The <tt>__init__</tt> method initializes the instance attributes as follows:The <tt>_array</tt> instance attribute is initialized to an array of length <tt>_size</tt>and the <tt>_head</tt> and <tt>_tail</tt> instance attributes,are initialized to represent the empty queue.The total running time for the <tt>QueueAsArray</tt><tt>__init__</tt> method is <I>O</I>(<I>n</I>), where <IMG WIDTH=64 HEIGHT=10 ALIGN=BOTTOM ALT="tex2html_wrap_inline60687" SRC="img662.gif" >.<P>The purpose of the <tt>purge</tt> method is to remove all the contentsof a container.In this case, the objects in the queue occupy contiguousarray positions between <tt>_head</tt> and <tt>_tail</tt>.To empty the queue,the <tt>purge</tt> method walks through the occupied array positionsassigning to each one the value <tt>None</tt> as it goes.Clearly, the running time for the <tt>purge</tt> method is <I>O</I>(<I>n</I>),where <IMG WIDTH=72 HEIGHT=9 ALIGN=BOTTOM ALT="tex2html_wrap_inline60691" SRC="img663.gif" >.<P><HR><A NAME="tex2html2938" HREF="page151.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2936" HREF="page148.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2930" HREF="page149.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2940" 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 + -
显示快捷键?