page151.html

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

HTML
61
字号
<HTML><HEAD><TITLE>enqueue, dequeue and getHead 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="tex2html2947" HREF="page152.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2945" HREF="page148.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2941" HREF="page150.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html2949" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION006213000000000000000"><tt>enqueue</tt>, <tt>dequeue</tt> and <tt>getHead</tt> Methods</A></H3><P>Program&nbsp;<A HREF="page151.html#progqueueAsArrayb"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> definesthe <tt>enqueue</tt>, <tt>Dequeue</tt> and <tt>getHead</tt> methodsof the <tt>QueueAsArray</tt> class.<P><P><A NAME="6785">&#160;</A><A NAME="progqueueAsArrayb">&#160;</A> <IMG WIDTH=575 HEIGHT=543 ALIGN=BOTTOM ALT="program6629" SRC="img695.gif"  ><BR><STRONG>Program:</STRONG> <tt>QueueAsArray</tt> class <tt>enqueue</tt>, <tt>Dequeue</tt>     and <tt>getHead</tt> methods.<BR><P><P>The <tt>getHead</tt> methodreturns the object found at the head of the queue,having first checked to see that the queue is not empty.If the queue is empty,it raises a <tt>ContainerEmpty</tt> exception.Under normal circumstances, we expect that the queue will not be empty.Therefore, the normal running time of this accessor is <I>O</I>(1).<P>In addition to <tt>self</tt>,the <tt>enqueue</tt> method takes a single argument which is athe object to be added to the tail of the queue.The <tt>enqueue</tt> method first checks that the queue is not full--a <tt>ContainerFull</tt> exception is raised when the queue is full.Next, the position at which to insert the new element is determinedby increasing the <tt>_tail</tt> instance attribute by onemodulo the length of the array.Finally, the object to be enqueued is put into the arrayat the correct position and the <tt>_count</tt> is adjusted accordingly.Under normal circumstances (i.e., when the exception is not thrown),the running time of <tt>Enqueue</tt> is <I>O</I>(1).<P>The <tt>dequeue</tt> method removes an object from the headof the queue and returns that object.First, it checks that the queue is not emptyand raises an exception when it is.If the queue is not empty,the method first sets aside the object at the headin the local variable <tt>result</tt>;it increases the <tt>_head</tt> instance attribute by one modulo the lengthof the array;adjusts the <tt>_count</tt> accordingly;and returns <tt>result</tt>.All this can be done in a constant amount of timeso the running time of <tt>dequeue</tt> is a <I>O</I>(1).<P><HR><A NAME="tex2html2947" HREF="page152.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2945" HREF="page148.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2941" HREF="page150.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html2949" 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 + -
显示快捷键?