📄 page155.html
字号:
<HTML>
<HEAD>
<TITLE>Head, Enqueue and Dequeue Member Functions</TITLE>
</HEAD>
<BODY bgcolor="#FFFFFF">
<img src="cover75.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cover75.gif" alt="Logo" align=right>
<b>Data Structures and Algorithms
with Object-Oriented Design Patterns in C++</b><br>
<A NAME="tex2html3825" HREF="page156.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page156.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="next_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/next_motif.gif"></A> <A NAME="tex2html3823" HREF="page152.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page152.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="up_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/up_motif.gif"></A> <A NAME="tex2html3819" HREF="page154.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page154.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/previous_motif.gif"></A> <A NAME="tex2html3827" HREF="page9.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page9.html"><IMG WIDTH=65 HEIGHT=24 ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/contents_motif.gif"></A> <A NAME="tex2html3828" HREF="page620.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page620.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="index_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/index_motif.gif"></A> <BR><HR>
<H3><A NAME="SECTION007223000000000000000"><tt>Head</tt>, <tt>Enqueue</tt> and <tt>Dequeue</tt> Member Functions</A></H3>
<P>
The <tt>Head</tt>, <tt>Enqueue</tt> and <tt>Dequeue</tt>
member functions of the <tt>QueueAsLinkedList</tt> class are
given in Program <A HREF="page155.html#progqueue4c" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page155.html#progqueue4c"><IMG ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A>.
<P>
<P><A NAME="7448"> </A><A NAME="progqueue4c"> </A> <IMG WIDTH=575 HEIGHT=429 ALIGN=BOTTOM ALT="program7353" SRC="img747.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img747.gif" ><BR>
<STRONG>Program:</STRONG> <tt>QueueAsLinkedList</tt> Class <tt>Enqueue</tt>, <tt>Head</tt> and <tt>Dequeue</tt> Member Function Definitions<BR>
<P>
<P>
The <tt>Head</tt> member function
returns a <tt>const</tt> reference to 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="page79.html#chapfds" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page79.html#chapfds"><IMG ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A> we saw that the running time of
<tt>LinkedList<T>::First</tt> is a constant,
Therefore, the normal running time for the <tt>Head</tt> function is <I>O</I>(1).
<P>
The <tt>Enqueue</tt> function takes a single argument--a reference to the object to be added to the tail of the queue.
The function simply calls the <tt>LinkedList<T>::Append</tt> function.
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> function removes an object from the head
of the queue and returns a reference to that object.
First, it verifies that the queue is not empty
and throws an exception when it is.
If the queue is not empty,
<tt>Dequeue</tt> saves a reference to the first item in the linked list
in the local variable <tt>result</tt>.
Then the item of the linked list is removed from the list.
When using the <tt>LinkedList<T></tt> class from Chapter <A HREF="page79.html#chapfds" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page79.html#chapfds"><IMG ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A>,
the time required to delete the first item from a list
is <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="tex2html3825" HREF="page156.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page156.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="next_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/next_motif.gif"></A> <A NAME="tex2html3823" HREF="page152.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page152.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="up_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/up_motif.gif"></A> <A NAME="tex2html3819" HREF="page154.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page154.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/previous_motif.gif"></A> <A NAME="tex2html3827" HREF="page9.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page9.html"><IMG WIDTH=65 HEIGHT=24 ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/contents_motif.gif"></A> <A NAME="tex2html3828" HREF="page620.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page620.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="index_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/index_motif.gif"></A> <P><ADDRESS>
<img src="bruno.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/bruno.gif" alt="Bruno" align=right>
<a href="javascript:if(confirm('http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html'" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html">Copyright © 1997</a> by <a href="javascript:if(confirm('http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html'" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html">Bruno R. Preiss, P.Eng.</a> All rights reserved.
</ADDRESS>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -