page151.html
来自「wqeqwvrw rkjqhwrjwq jkhrjqwhrwq jkhrwq」· HTML 代码 · 共 60 行
HTML
60 行
<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="tex2html3776" HREF="page152.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page152.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="tex2html3774" HREF="page148.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page148.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="tex2html3770" HREF="page150.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page150.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="tex2html3778" 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="tex2html3779" 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="SECTION007213000000000000000"><tt>Head</tt>, <tt>Enqueue</tt>, and <tt>Dequeue</tt> Member Functions</A></H3>
<P>
Program <A HREF="page151.html#progqueue2c" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page151.html#progqueue2c"><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> defines the <tt>Head</tt>,
<tt>Enqueue</tt> and <tt>Dequeue</tt> member functions
of the <tt>QueueAsArray</tt> class.
<P>
<P><A NAME="7435"> </A><A NAME="progqueue2c"> </A> <IMG WIDTH=575 HEIGHT=525 ALIGN=BOTTOM ALT="program7272" SRC="img744.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img744.gif" ><BR>
<STRONG>Program:</STRONG> <tt>QueueAsArray</tt> Class <tt>Head</tt>, <tt>Enqueue</tt> and <tt>Dequeue</tt> Member Function Definitions<BR>
<P>
<P>
The <tt>Head</tt> member function
simply returns a reference to 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 throws a <tt>domainerror</tt> exception.
Under normal circumstances, we expect that the queue will not be empty.
Therefore, the normal running time of this function is <I>O</I>(1).
<P>
The <tt>Enqueue</tt> function takes a single argument which is a
reference to an object to be added to the tail of the queue.
The <tt>Enqueue</tt> function first checks that the queue is not full--a <tt>domainerror</tt> exception is thrown when the queue is full.
Next, the position at which to insert the new element is determined
by increasing the member variable <tt>tail</tt> by one
modulo the length of the array.
Finally, a pointer to the object to be enqueued is put into the array
at 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> function removes an object from the head
of the queue and returns a reference to that object.
First, it checks that the queue is not empty
and throws an exception when it is.
If the queue is not empty,
the function sets aside a reference to the object at the head
in the local variable <tt>result</tt>;
it increases the <tt>head</tt> member variable by one modulo the length
of the array;
adjusts the <tt>count</tt> accordingly;
and returns <tt>result</tt>.
All this can be done in a constant amount of time
so the running time of <tt>Dequeue</tt> is a constant.
<P>
<HR><A NAME="tex2html3776" HREF="page152.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page152.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="tex2html3774" HREF="page148.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page148.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="tex2html3770" HREF="page150.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page150.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="tex2html3778" 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="tex2html3779" 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 + =
减小字号Ctrl + -
显示快捷键?