📄 page169.html
字号:
<HTML><HEAD><TITLE>Ordered Lists</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="tex2html3146" HREF="page170.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html3144" HREF="page168.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html3138" HREF="page168.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html3148" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H1><A NAME="SECTION007100000000000000000">Ordered Lists</A></H1><A NAME="seclistsordered"> </A><P>An <em>ordered list</em> is a list in which the order of the items is significant.However, the items in an ordered listsare not necessarily <em>sorted</em>.Consequently, it is possible to <em>change</em> the order of itemsand still have a valid ordered list.<P>Program <A HREF="page169.html#progorderedLista"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the <tt>OrderedList</tt> class.The abstract <tt>OrderedList</tt> class extendsthe abstract <tt>Container</tt> classdefined in Program <A HREF="page126.html#progsearchableContainera"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.Recall that a searchable container is a containerthat supports the following additional operations:<DL ><DT><STRONG><tt>insert</tt></STRONG><DD> used to put objects into a the container; <DT><STRONG><tt>withdraw</tt></STRONG><DD> used to remove objects from the container; <DT><STRONG><tt>find</tt></STRONG><DD> used to locate objects in the container; <DT><STRONG><tt>__contains__</tt></STRONG><DD> used to test whether a given object instance is in the container.<P> </DL><P>The abstract <tt>OrderedList</tt> class adds the following operations:<DL ><DT><STRONG><tt>__getitem__</tt></STRONG><DD> used to access the object at a given position in the ordered list, and <DT><STRONG><tt>findPosition</tt></STRONG><DD> used to find the position of an object in the ordered list.<P> </DL><P><P><A NAME="8840"> </A><A NAME="progorderedLista"> </A> <IMG WIDTH=575 HEIGHT=238 ALIGN=BOTTOM ALT="program8580" SRC="img732.gif" ><BR><STRONG>Program:</STRONG> Abstract <tt>OrderedList</tt> class.<BR><P><P>In addition to <tt>self</tt>,the <tt>findPosition</tt> method of the abstract <tt>List</tt> classtakes an object, <tt>obj</tt>, and searches the listfor an object that matches the given one.The return value is an instance of a class derived from the <tt>Cursor</tt> class.Program <A HREF="page169.html#progcursora"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the abstract <tt>Cursor</tt> class.<P><P><A NAME="8844"> </A><A NAME="progcursora"> </A> <IMG WIDTH=575 HEIGHT=467 ALIGN=BOTTOM ALT="program8598" SRC="img733.gif" ><BR><STRONG>Program:</STRONG> Abstract <tt>Cursor</tt> class.<BR><P><P>A cursor ``remembers'' the position of an item in a list.The abstract Program <A HREF="page169.html#progcursora"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> class given in Program <A HREF="page169.html#progcursora"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>defines the following operations:<P><DL ><DT><STRONG><tt>datum</tt></STRONG><DD> used to access the object in the ordered list at the current cursor position; <DT><STRONG><tt>insertAfter</tt></STRONG><DD> used to insert an object into the ordered list after the current cursor position; <DT><STRONG><tt>insertBefore</tt></STRONG><DD> used to insert an object into the ordered list before the current cursor position; and <DT><STRONG><tt>withdraw</tt></STRONG><DD> used to remove from the ordered list the object at the current cursor position.<P> </DL><P>As we did in the previous chapter with stacks, queues, and deques,we will examine two ordered list implementations--an array-based one and a linked-list one.Section <A HREF="page170.html#seclistslista"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> presents an implementation using the <tt>Array</tt> class;Section <A HREF="page179.html#seclistslistp"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>,an implementation using on the <tt>LinkedList</tt> class.<P><BR> <HR><UL> <LI> <A NAME="tex2html3149" HREF="page170.html#SECTION007110000000000000000">Array Implementation</A><LI> <A NAME="tex2html3150" HREF="page179.html#SECTION007120000000000000000">Linked-List Implementation</A><LI> <A NAME="tex2html3151" HREF="page188.html#SECTION007130000000000000000">Performance Comparison:<tt>OrderedListAsArray</tt> vs. <tt>ListAsLinkedList</tt></A><LI> <A NAME="tex2html3152" HREF="page189.html#SECTION007140000000000000000">Applications</A></UL><HR><A NAME="tex2html3146" HREF="page170.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html3144" HREF="page168.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html3138" HREF="page168.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html3148" 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -