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

Creating a List and Inserting Items

Program gif gives the definitions of the constructor and the Insert methods of the OrderedListAsArray class. The constructor takes a single argument which specifies the length of array to use in the representation of the ordered list. Thus if we use an array-based implementation, we need to know when a list is declared what will be the maximum number of items in that list. The constructor initializes the array variable as an array with the specified length. The running time of the constructor is clearly O(n), where tex2html_wrap_inline60461.

   program8796
Program: OrderedListAsArray class constructor and Insert methods.

The Insert method is part of the interface of all searchable containers. Its purpose is to put an object into the container. The obvious question which arises is, where should the inserted item be placed in the ordered list? The simple answer is, at the end.

In Program gif we see that the Insert method simply adds the new item to the end of the list, provided there is still room in the array. Normally, the array will not be full, so the running time of this method is O(1).


next up previous contents index

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