page227.html

来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 53 行

HTML
53
字号
<HTML><HEAD><TITLE>Inserting and Removing Items</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="tex2html3819" HREF="page228.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html3817" HREF="page224.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html3811" HREF="page226.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html3821" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION008413000000000000000">Inserting and Removing Items</A></H3><P>Program&nbsp;<A HREF="page227.html#progchainedHashTableb"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> gives the code forinserting and removing items from a <tt>ChainedHashTable</tt>.<P><P><A NAME="11450">&#160;</A><A NAME="progchainedHashTableb">&#160;</A> <IMG WIDTH=575 HEIGHT=199 ALIGN=BOTTOM ALT="program11322" SRC="img929.gif"  ><BR><STRONG>Program:</STRONG> <tt>ChainedHashTable</tt> class <tt>insert</tt> and <tt>withdraw</tt> methods.<BR><P><P>The implementations of the <tt>insert</tt>and <tt>withdraw</tt> methods are remarkably simple.For example,the <tt>insert</tt> method first calls the hash method <tt>h</tt>to compute an array index which is used to select one of the linked lists.The <tt>append</tt> method provided by the <tt>LinkedList</tt> classis used to add the object to the selected linked list.The total running time for the <tt>insert</tt> operation is  <IMG WIDTH=100 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline62203" SRC="img930.gif"  >,where  <IMG WIDTH=48 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline62205" SRC="img931.gif"  > is the running time of the <tt>hash</tt> function.Notice that if the hash function runs in constant time,then so too does hash table insertion operation!<P>The <tt>withdraw</tt> method is almost identical to the <tt>insert</tt> method.Instead of calling the <tt>append</tt>,it calls the linked list <tt>extract</tt> method to remove thespecified object from the appropriate linked list.The running time of <tt>withdraw</tt> is determined by the timeof the <tt>extract</tt> operation.In Chapter&nbsp;<A HREF="page81.html#chapfds"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> this was shown to be <I>O</I>(<I>n</I>)where <I>n</I> is the number of items in the linked list.In the worst case, all of the items in the <tt>ChainedHashTable</tt>have collided with each other and ended up in the same list.That is, in the worst case if there are <I>n</I> items in the container,all <I>n</I> of them are in a single linked list.In this case, the running time of the <tt>withdraw</tt> operationis  <IMG WIDTH=101 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline62215" SRC="img932.gif"  >.<P><HR><A NAME="tex2html3819" HREF="page228.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html3817" HREF="page224.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html3811" HREF="page226.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html3821" 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 &#169; 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 + -
显示快捷键?