page203.html

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

HTML
81
字号
<HTML><HEAD><TITLE>Exercises</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="tex2html3538" HREF="page204.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html3536" HREF="page168.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html3530" HREF="page202.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html3540" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H1><A NAME="SECTION007300000000000000000">Exercises</A></H1><P><OL><LI>	Devise an algorithm to reverse the contents of an ordered list.	Determine the running time of your algorithm.<LI> <A NAME="exerciselistsappend">&#160;</A>	Devise an algorithm to append the contents of one ordered list	to the end of another.	Assume that both lists are represented using arrays.	What is the running time of your algorithm?<LI>	Repeat Exercise&nbsp;<A HREF="page203.html#exerciselistsappend"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>,	but this time assume that both lists are represented using linked lists.	What is the running time of your algorithm?<LI> <A NAME="exerciselistsmerge">&#160;</A>	Devise an algorithm to merge the contents of two sorted lists.	Assume that both lists are represented using arrays.	What is the running time of your algorithm?<LI>	Repeat Exercise&nbsp;<A HREF="page203.html#exerciselistsmerge"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>,	but this time assume that both lists are represented using linked lists.	What is the running time of your algorithm?<LI>	The <tt>withdraw</tt> method can be used to	remove items from a list one at a time.	Suppose we want to provide an additional a method,	<tt>withdrawAll</tt>,	that takes one argument and withdraws all the items in a list	that <em>match</em> the given argument.	We can provide an implementation of the <tt>withdrawAll</tt>	method in the <tt>SearchableContainer</tt> class like this:<PRE>class SearchableContainer(Container):    def withdrawAll(self, obj):	while true:	    match = self.find(obj)	    if match is None:		break	    self.withdraw(match)    # ...</PRE>	Determine the worst-case running time of this method	for each of the following cases:	<OL><LI> an array-based implementation of an ordered list,<LI> a linked-list implementation of an ordered list,<LI> an array-based implementation of a sorted list, and<LI> a linked-list implementation of a sorted list.	</OL><LI> <A NAME="exerciselistswithdrawall">&#160;</A>	Devise an <I>O</I>(<I>n</I>) algorithm,	to remove from an ordered list	all the items that match a given item.	Assume the list is represented using an array.<LI>	Repeat Exercise&nbsp;<A HREF="page203.html#exerciselistswithdrawall"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>,	but this time assume the ordered list	is represented using a linked list.<LI>	Consider an implementation of the <tt>OrderedList</tt> interface that uses	a doubly-linked list such as the one shown in Figure&nbsp;<A HREF="page165.html#figdeque2"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>&nbsp;(a).	Compare the running times of the operations for this implementation	with those given in Table&nbsp;<A HREF="page188.html#tbllists"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.<LI>	Derive an expression for the amount of space used to represent	an ordered list of <I>n</I> elements using	a doubly-linked list such as the one shown in Figure&nbsp;<A HREF="page165.html#figdeque2"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>&nbsp;(a).	Compare this with the space used by the array-based implementation.	Assume that integers and pointers each occupy four bytes.<LI>	Consider an implementation of the <tt>SortedList</tt> interface that uses	a doubly-linked list such as the one shown in Figure&nbsp;<A HREF="page165.html#figdeque2"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>&nbsp;(a).	Compare the running times of the operations for this implementation	with those given in Table&nbsp;<A HREF="page199.html#tblsortedlists"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.<LI> <A NAME="exerciselistsproof">&#160;</A>	Verify that Program&nbsp;<A HREF="page201.html#progpolynomialAsSortedLista"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> correctly computes	the sum of two polynomials.<LI>	Write an algorithm to multiply a polynomial by a scalar.	<b>Hint</b>: Use a visitor.</OL><HR><A NAME="tex2html3538" HREF="page204.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html3536" HREF="page168.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html3530" HREF="page202.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html3540" 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 + -
显示快捷键?