Data Structures and Algorithms with Object-Oriented Design Patterns in C#
next up previous contents index

Removing Arbitrary Items by Position

The final method of the OrderedListAsArray.MyCursor class that we will consider is the Withdraw method. The desired effect of this method is to remove from the ordered list the item at the position specified by the cursor.

Figure gif shows the way in which to delete an item from an ordered list which implemented with an array. All of the items remaining in the list to the right of the deleted item need to be shifted to the left in the array by one position.

   figure9192
Figure: Withdrawing an item from an ordered list implemented as an array.

Program gif gives the implementation of the Withdraw method. After checking the validity of the position, all of the items following the item to be withdraw are moved one position to the left in the array.

   program9455
Program: OrderedListAsArray.MyCursor class Withdraw method.

The running time of the Withdraw method depends on the position in the array of the item being deleted and on the number of items in the ordered lists. In the worst case, the item to be deleted is in the first position. In this case, the work required to move the remaining items left is O(n), where tex2html_wrap_inline60465.


next up previous contents index

Bruno Copyright © 2001 by Bruno R. Preiss, P.Eng. All rights reserved.