📄 page162.html
字号:
<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="tex2html3907" HREF="page163.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page163.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="tex2html3905" HREF="page161.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page161.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="tex2html3901" HREF="page161.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page161.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="tex2html3909" 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="tex2html3910" 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="SECTION007321000000000000000"><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="8664"> </A><A NAME="progdeque5c"> </A> <IMG WIDTH=575 HEIGHT=429 ALIGN=BOTTOM ALT="program8562" SRC="img759.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img759.gif" ><BR>
<STRONG>Program:</STRONG> <tt>DequeAsLinkedList</tt> Class <tt>Tail</tt>, <tt>EnqueueTail</tt> and <tt>DequeueTail</tt> Member Function Definitions<BR>
<P>
<P>
The <tt>Tail</tt> member function
returns a <tt>const</tt> reference to the object at the tail of the deque.
The tail of the deque is in the last 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>::Last</tt> is a constant,
Therefore, the normal running time for the <tt>Tail</tt> function is <I>O</I>(1).
<P>
The <tt>EnqueueHead</tt> function takes a single argument--a reference to the object to be added to the head of the deque.
The function simply calls the <tt>LinkedList<T>::Prepend</tt> function.
Since the running time for <tt>Prepend</tt> is <I>O</I>(1),
the running time of <tt>EnqueueHead</tt> is also <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 verifies that the deque is not empty
and throws an exception when it is.
If the deque is not empty,
<tt>DequeueTail</tt> saves a reference to the last 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 last item from a list is <I>O</I>(<I>n</I>),
where <IMG WIDTH=72 HEIGHT=9 ALIGN=BOTTOM ALT="tex2html_wrap_inline61308" SRC="img709.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img709.gif" > is the number of items in the list.
As a result,
the running time of <tt>DequeueTail</tt> is <I>O</I>(<I>n</I>).
<P>
<HR><A NAME="tex2html3907" HREF="page163.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page163.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="tex2html3905" HREF="page161.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page161.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="tex2html3901" HREF="page161.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page161.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="tex2html3909" 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="tex2html3910" 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 + -