📄 page155.html
字号:
<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="tex2html2992" HREF="page156.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2990" HREF="page152.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2986" HREF="page154.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2994" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION006223000000000000000"><tt>enqueue</tt>, <tt>dequeue</tt> and <tt>getHead</tt> Methods</A></H3><P>The <tt>enqueue</tt>, <tt>dequeue</tt> and <tt>getHead</tt> methodsof the <tt>QueueAsLinkedList</tt> class aregiven in Program <A HREF="page155.html#progqueueAsLinkedListb"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.<P><P><A NAME="6795"> </A><A NAME="progqueueAsLinkedListb"> </A> <IMG WIDTH=575 HEIGHT=390 ALIGN=BOTTOM ALT="program6707" SRC="img697.gif" ><BR><STRONG>Program:</STRONG> <tt>QueueAsLinkedList</tt> class <tt>enqueue</tt>, <tt>dequeue</tt> and <tt>getHead</tt> methods.<BR><P><P>The <tt>getHead</tt> methodreturns the object at the head of the queue.The head of the queue is in the first element of the linked list.In Chapter <A HREF="page81.html#chapfds"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> we saw that the running time of<tt>LinkedList.first</tt> is a constant,Therefore, the normal running time for the <tt>head</tt> method is <I>O</I>(1).<P>The <tt>enqueue</tt> method takes a single argument--the object to be added to the tail of the queue.The method simply calls the <tt>LinkedList.append</tt> method.Since the running time for <tt>append</tt> is <I>O</I>(1),the running time of <tt>enqueue</tt> is also <I>O</I>(1).<P>The <tt>dequeue</tt> method removes an object from the headof the queue and returns that object.First, it verifies that the queue is not emptyand throws an exception when it is.If the queue is not empty,<tt>dequeue</tt> saves the first item in the linked listin the local variable <tt>result</tt>.Then that item is extracted from the list.Using the <tt>LinkedList</tt> class from Chapter <A HREF="page81.html#chapfds"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>,the time required to extract the first item from a listis <I>O</I>(1) regardless of the number of items in the list.As a result,the running time of <tt>dequeue</tt> is also <I>O</I>(1).<P><HR><A NAME="tex2html2992" HREF="page156.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2990" HREF="page152.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2986" HREF="page154.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2994" 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 + -