page195.html

来自「wqeqwvrw rkjqhwrjwq jkhrjqwhrwq jkhrwq」· HTML 代码 · 共 55 行

HTML
55
字号
<HTML>
<HEAD>
<TITLE>Inserting Items in a Sorted 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="tex2html4325" HREF="page196.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page196.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="tex2html4323" HREF="page194.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page194.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="tex2html4317" HREF="page194.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page194.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="tex2html4327" 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="tex2html4328" 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="SECTION008221000000000000000">Inserting Items in a Sorted List</A></H3>
<P>
Program&nbsp;<A HREF="page195.html#progsorted5c" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page195.html#progsorted5c"><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> gives the implementation of the <tt>Insert</tt>
member function of the <tt>SortedListAsLinkedList</tt> class.
This function takes a single argument:
a reference to an <tt>Object</tt> to be inserted into the sorted list.
The algorithm used for the insertion is as follows:
First, the existing sorted, linked list is traversed
in order to find the linked list element which is greater than or equal
to the object to be inserted into the list.
The traversal is done using two pointers--<tt>prevPtr</tt> and <tt>ptr</tt>.
During the traversal,
the latter keeps track of the current element
and the former keeps track of the previous element.
<P>
By keeping track of the previous element,
it is possible to efficiently insert the new item into the sorted list
by calling the <tt>InsertAfter</tt> member function of the
<tt>LinkedList&lt;T&gt;</tt> class.
In Chapter&nbsp;<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 <tt>InsertAfter</tt> function was shown to be <I>O</I>(1).
<P>
In the event that the item to be inserted is smaller than the first item
in the sorted list,
then rather than using the <tt>InsertAfter</tt> function,
the <tt>Prepend</tt> function is used.
The <tt>Prepend</tt> function was also shown to be <I>O</I>(1).
<P>
In the worst case, the object to be inserted into the linked list
is larger than all of the objects already present in the list.
In this case, the entire list needs to be traversed
before doing the insertion.
Consequently, the total running time for the <tt>Insert</tt>
operation of the <tt>SortedListAsLinkedList</tt> class 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"  >.
<P>
<P><A NAME="11060">&#160;</A><A NAME="progsorted5c">&#160;</A> <IMG WIDTH=575 HEIGHT=315 ALIGN=BOTTOM ALT="program10863" SRC="img860.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img860.gif"  ><BR>
<STRONG>Program:</STRONG> <tt>SortedListAsLinkedList</tt> Class 	<tt>Insert</tt> Member Function Definition<BR>
<P><HR><A NAME="tex2html4325" HREF="page196.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page196.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="tex2html4323" HREF="page194.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page194.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="tex2html4317" HREF="page194.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page194.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="tex2html4327" 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="tex2html4328" 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 &#169; 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 + -
显示快捷键?