Você está na página 1de 3

The ADT List

No such thing exists in C++ Metaphor: A shopping list Define Operations Implement the ADT

Create(L)
This creates a new empty list L. Always write a create function, even if it seems unnecessary. The C++ function Create(L) is the constructor for the class listClass. So, to create a list L, instantiate it as a variable of type listClass.

listClass L;

// creates a list L

Length(L)
Tells us how many elements there are in the list L. Always write a function (like this) that permits you to find out whether the data type is empty, or, in some cases, full. The C++ function call is:

Insert(L, i, newitem)
This inserts the new item newitem at position i into L. You always need at least one function that puts something INTO an abstract data type. Sometimes you have several such functions. The C++ function call is:

x = L.Length( );

L.Insert(i, newitem);

Delete(L,i)
We need to specify more about the behavior of Insert. What if we are trying to insert something at position 3 and there is something already at position 3? Then we move the item from 3 to 4, etc. What if we are trying to insert something at 3 and the list is empty? We prohibit insert at any place after length(L)+1.
This deletes the item at position i in the list L. The position i must be smaller or equal than Length(L). The result of the deletion is that the element at position I+1 is moved into the hole left at the position i, etc. The C++ function call is: L.Delete(i);

Retrieve(L, i, Item)
This returns the item at position i in the list L. This operation does not change L. Of course, i must again be in the range between 1 and Length(L). You always need to define one or more operations to get something out of an abstract data type. The C++ function call is: L.Retrieve(i, Item);
In summary, we have operations that - ADD data (includes Create) - DELETE data - ASK for data This is the case for all ADTs.

Array Implementation of List


Even the array implementation is not just an array. We need a way to keep track of how many elements there are in the List. An integer variable would do, but if we have several Lists in one program (VERY POSSIBLE!) we need several such integers. THEREFORE...

We packaged the integer together with the array as private data variables belonging to the instantiated object.

10

const int MAX_LIST = 100; typedef int ItemType; class listClass { public: // constructors, ADT operations private: ItemType Items[MAX_LIST]; int Size; }

Create(L)
listClass::listClass( ) { Size = 0; // init size }

11

12

Destroy(L)
This is required by C++ and not part of our abstraction. listClass::~listClass() { } // This destructor does nothing

Length(L)
int listClass::Length( ) { return Size; }

13

14

Insert(L, i, newitem)
listClass::Insert(int i, ItemType newitem, boolean& success) { int L_pos = Length( ); success = boolean( ( i >= 1) && (i <= L_pos + 1) && (L_pos < MAX_LIST) ); if (success) { // !!!!! Insert newitem (see next page) ++Size; } }

Array Insertion (missing part from last page)


for (int position = L_pos; position >= i; --position) Items[Index(Position+1)] = Items[Index(Position)]; Items[Index(i+1)] = newitem; Note that function Index( ) returns position-1. int listClass::Index(int Position) {return Position-1;)

15

16

void listClass::Delete(int i, boolean& success) { int L_pos = Length( ); success = boolean( (i >= 1) && (i <= L_pos) ); if (success) { for (int From_pos = i+1; From_pos <= L_pos; ++From_pos) Items[Index(From_pos-1)] = Items[Index(From_pos); --Size; } 17 }

Retrieve(L, i, Item)
void listClass::Retrieve(int i, ItemType& Item, boolean& success) { int L_pos = Length( ); success = boolean( ( i >= 1) && (i <= L_pos) ); if (success) Item = Items[Index(i)]; }

18