page402.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 47 行
HTML
47 行
<HTML><HEAD><TITLE>Intersection</TITLE></HEAD><BODY bgcolor="#FFFFFF"> <a href="../index.html" target="_top"><img src="../icons/usins.gif" alt="Logo" align=right></a><b>Data Structures and Algorithms with Object-Oriented Design Patterns in Python</b><br><A NAME="tex2html5810" HREF="page403.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html5808" HREF="page400.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html5804" HREF="page401.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html5812" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION0012322000000000000000">Intersection</A></H3><P>The implementation of the intersection operator for the<tt>MultisetAsLinkedList</tt> classis similar to that of union.However, instead of merging of two ordered, linked liststo construct a third,we compare the elements of two listsand append an item to the third only when it appears in both of the input lists.The <tt>Intersection</tt> method is shown in Program <A HREF="page402.html#progmultisetAsLinkedListc"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.<P><P><A NAME="28254"> </A><A NAME="progmultisetAsLinkedListc"> </A> <IMG WIDTH=575 HEIGHT=371 ALIGN=BOTTOM ALT="program28088" SRC="img1598.gif" ><BR><STRONG>Program:</STRONG> <tt>MultisetAsLinkedList</tt> class <tt>__and__</tt> method.<BR><P><P>The main loop of the program traverses the linked listsof both input operands at once using two variables (lines 9-16).If the next element in each list is the same,that element is appended to the result and both variables are advanced.Otherwise, only one of the variables is advanced--the one pointing to the smaller element.<P>The number of iterations of the main loop actually donedepends on the contents of the respective linked lists.The best case occurs when both lists are identical.In this case, the number of iterations is <I>m</I>,where <IMG WIDTH=132 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline66949" SRC="img1599.gif" >.In the worst case case, the number of iterations done is <I>m</I>+<I>n</I>.Therefore, the running time of the <tt>Intersection</tt> method is <I>O</I>(<I>m</I>+<I>n</I>).<P><HR><A NAME="tex2html5810" HREF="page403.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html5808" HREF="page400.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html5804" HREF="page401.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html5812" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <P><ADDRESS><img src="../icons/bruno.gif" alt="Bruno" align=right><a href="../copyright.html">Copyright © 2003</a> by <a href="../signature.html">Bruno R. Preiss, P.Eng.</a> All rights reserved.</ADDRESS></BODY></HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?