⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 page401.html

📁 wqeqwvrw rkjqhwrjwq jkhrjqwhrwq jkhrwq
💻 HTML
字号:
<HTML>
<HEAD>
<TITLE>Linked List Implementation</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="tex2html6879" HREF="page402.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page402.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="tex2html6877" HREF="page397.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page397.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="tex2html6873" HREF="page400.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page400.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="tex2html6881" 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="tex2html6882" 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>
<H2><A NAME="SECTION0013320000000000000000">Linked List Implementation</A></H2>
<P>
The array implementation of multisets is really only practical
if the number of items in the universe, <I>N</I>=|<I>U</I>|,
is not too large.
If <I>N</I> is large, then it is impractical,
or at least extremely inefficient,
to use an array of <I>N</I> counters to represent the multiset.
This is especially so if the number of elements in the multisets 
is significantly less than <I>N</I>.
<P>
If we use a linked list of elements to represent a multiset <I>S</I>,
the space required is proportional to the size of the multiset, |<I>S</I>|.
When the size of the multiset is significantly less than
the size of the universe,  <IMG WIDTH=62 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline67534" SRC="img1640.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1640.gif"  >,
it is more efficient in terms of both time and space to use a linked list.
<P>
Program&nbsp;<A HREF="page401.html#progmultiset3h" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page401.html#progmultiset3h"><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 declaration
of the <tt>MultisetAsLinkedList</tt> class.
The <tt>MultisetAsLinkedList</tt> class is a concrete class derived
from the abstract base class <tt>Multiset</tt>.
In this case a linked list of <tt>unsigned int</tt>s
is used to record the contents of the multiset.
<P>
How should the elements of the multiset be stored in the list?
Perhaps the simplest way is to store the elements in the list
in no particular order.
Doing so makes the <tt>Insert</tt> operation efficient--it can be done in constant time.
Furthermore, the <tt>IsMember</tt> and <tt>Withdraw</tt> operations
both take <I>O</I>(<I>n</I>) time,
where <I>n</I> is the number of items in the multiset,
<EM>regardless of the order of the items in the linked list</EM>.
<P>
Consider now the union, intersection, and difference of two multisets,
say <I>S</I> and <I>T</I>.
If the linked list is unordered,
the worst case running time for the union operation is <I>O</I>(<I>m</I>+<I>n</I>),
where <I>m</I>=|<I>S</I>| and <I>n</I>=|<I>T</I>|.
Unfortunately, intersection and difference are both <I>O</I>(<I>mn</I>).
<P>
If, on the other hand,
we use an <em>ordered</em> linked list,
union, intersection, and difference can all be done in <I>O</I>(<I>m</I>+<I>n</I>) time.
The trade-off is that the insertion becomes an <I>O</I>(<I>n</I>) operation
rather than a <I>O</I>(1).
The <tt>MultisetAsLinkedList</tt> implementation presented in this
section records the elements of the multiset
in an <em>ordered</em> linked list.
<P>
<P><A NAME="28776">&#160;</A><A NAME="progmultiset3h">&#160;</A> <IMG WIDTH=575 HEIGHT=143 ALIGN=BOTTOM ALT="program28584" SRC="img1641.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1641.gif"  ><BR>
<STRONG>Program:</STRONG> <tt>MultisetAsLinkedList</tt> Class Definition<BR>
<P><BR> <HR>
<UL> 
<LI> <A NAME="tex2html6883" HREF="page402.html#SECTION0013321000000000000000" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page402.html#SECTION0013321000000000000000">Union</A>
<LI> <A NAME="tex2html6884" HREF="page403.html#SECTION0013322000000000000000" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page403.html#SECTION0013322000000000000000">Intersection</A>
</UL>
<HR><A NAME="tex2html6879" HREF="page402.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page402.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="tex2html6877" HREF="page397.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page397.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="tex2html6873" HREF="page400.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page400.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="tex2html6881" 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="tex2html6882" 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -