📄 page180.html
字号:
<HTML>
<HEAD>
<TITLE>Finding Items in a List</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="tex2html4141" HREF="page181.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page181.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="tex2html4139" HREF="page177.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page177.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="tex2html4133" HREF="page179.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page179.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="tex2html4143" 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="tex2html4144" 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="SECTION008123000000000000000">Finding Items in a List</A></H3>
<P>
Program <A HREF="page180.html#proglist8c" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page180.html#proglist8c"><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>IsMember</tt>
and <tt>Find</tt> member functions of the <tt>ListAsLinkedList</tt> class.
The implementations of these functions are almost identical.
However, they differ in two key aspects--the comparison used and the return value.
<P>
<P><A NAME="10552"> </A><A NAME="proglist8c"> </A> <IMG WIDTH=575 HEIGHT=372 ALIGN=BOTTOM ALT="program10173" SRC="img805.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img805.gif" ><BR>
<STRONG>Program:</STRONG> <tt>ListAsLinkedList</tt> Class <tt>IsMember</tt> and <tt>Find</tt> Member Function Definitions<BR>
<P>
<P>
The <tt>IsMember</tt> function tests whether
a particular object instance is contained in the ordered list.
It returns a Boolean value indicating whether the object is present.
The running time of this function is clearly <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" >,
the number of items in the ordered list.
<P>
The <tt>Find</tt> member function locates an object
which matches a given object.
The match is determined by using <tt>operator==</tt>.
<tt>Find</tt> returns a reference to the matching object if one is found.
Otherwise, it returns a reference to the <tt>NullObject</tt> instance.
The running time for this function,
is <IMG WIDTH=172 HEIGHT=26 ALIGN=MIDDLE ALT="tex2html_wrap_inline61650" SRC="img790.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img790.gif" >,
where <IMG WIDTH=89 HEIGHT=26 ALIGN=MIDDLE ALT="tex2html_wrap_inline61648" SRC="img789.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img789.gif" > is the time required to do the comparison,
and <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 ordered list.
This simplifies to <I>O</I>(<I>n</I>) when the comparison can be done in constant time.
<P>
<HR><A NAME="tex2html4141" HREF="page181.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page181.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="tex2html4139" HREF="page177.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page177.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="tex2html4133" HREF="page179.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page179.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="tex2html4143" 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="tex2html4144" 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 + -