page160.html
来自「wqeqwvrw rkjqhwrjwq jkhrjqwhrwq jkhrwq」· HTML 代码 · 共 62 行
HTML
62 行
<HTML>
<HEAD>
<TITLE>Tail, EnqueueHead,
and DequeueTail 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="tex2html3884" HREF="page161.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page161.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="tex2html3882" HREF="page159.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page159.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="tex2html3878" HREF="page159.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page159.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="tex2html3886" 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="tex2html3887" 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="SECTION007311000000000000000"><tt>Tail</tt>, <tt>EnqueueHead</tt>,
and <tt>DequeueTail</tt> Member Functions</A></H3>
<P>
Program <A HREF="page160.html#progdeque3c" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page160.html#progdeque3c"><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>Tail</tt>,
<tt>EnqueueHead</tt> and <tt>DequeueTail</tt> member functions
of the <tt>DequeAsArray</tt> class.
<P>
<P><A NAME="8652"> </A><A NAME="progdeque3c"> </A> <IMG WIDTH=575 HEIGHT=544 ALIGN=BOTTOM ALT="program8487" SRC="img756.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img756.gif" ><BR>
<STRONG>Program:</STRONG> <tt>DequeAsArray</tt> Class <tt>Tail</tt> <tt>EnqueueTail</tt> and <tt>DequeueTail</tt> Member Function Definitions<BR>
<P>
<P>
The <tt>Tail</tt> member function
simply returns a reference to the object found
at the tail of the deque,
having first checked to see that the deque is not empty.
If the deque is empty, it throws a <tt>domainerror</tt> exception.
Under normal circumstances, we expect that the deque will not be empty.
Therefore, the normal running time of this function is <I>O</I>(1).
<P>
The <tt>EnqueueHead</tt> function takes a single argument which is a
reference to an object to be added to the head of the deque.
The <tt>EnqueueHead</tt> function first checks that the deque is not full--a <tt>domainerror</tt> exception is thrown when the deque is full.
Next, the position at which to insert the new element is determined
by decreasing the member variable <tt>head</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>EnqueueHead</tt> is <I>O</I>(1).
<P>
The <tt>DequeueTail</tt> function removes an object from the tail
of the deque and returns a reference to that object.
First, it checks that the deque is not empty
and throws an exception when it is.
If the deque is not empty,
the function sets aside a reference to the object at the tail
in the local variable <tt>result</tt>;
it decreases the <tt>tail</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>DequeueTail</tt> is a constant.
<P>
<HR><A NAME="tex2html3884" HREF="page161.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page161.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="tex2html3882" HREF="page159.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page159.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="tex2html3878" HREF="page159.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page159.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="tex2html3886" 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="tex2html3887" 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 + -
显示快捷键?