Home

Operations on an Array-Based List

 

Adding an Item

Creating a collection consists of adding items to it. Items are usually added one at a time. The easiest way to do this is to add an item at the end of the existing collection.

To add an item to the collection, you first check whether the list is already full. For an array-based list, the collection is full if its count of items is equal to or higher than the maximum number you had set. If the collection is not empty, you can add an item at the end and increase the count by one. Here is how this can be done:

using namespace System;

int MaxCount = 20;

public ref class CCollection
{
private:
    // This collection will be a list of decimal numbers
    array<double> ^ Item;

private:
    // This is the size of the collection
    int size;
public:
    // This represents the number of items in the collection
    property int Count
    {
        int get() { return size; }
    }
    
    bool Add(double item);

public:
    CCollection();
};

CCollection::CCollection()
{
    Item = gcnew array<double>(MaxCount);
    size = 0;
}

// Adds a new item to the list if the list is not full
// Increases the number of items in the list
// Returns true if the item was added, otherwise returns false
bool CCollection::Add(double item)
{
    // Make sure the list is not yet full
    if (size < 20)
    {
        // Since the list is not full, add the "item" at the end
        Item[size] = item;
        // Increase the count and return the new position
        size++;

        // Indicate that the item was successfully added
        return true;
    }

    // If the item was not added, return false;
    return false;
}

Once you have a means of adding items to the list, you can effectively create a list of items. Here is an example:

int main()
{
    CCollection ^ list = gcnew CCollection;

    Console::WriteLine(L"Number of Items: {0}", list->Count);

    list->Add(224.52);
    list->Add(60.48);
    list->Add(1250.64);
    list->Add(8.86);
    list->Add(1005.36);

    Console::WriteLine(L"Number of Items: {0}", list->Count);
    return 0;
}

This would produce:

Number of Items: 0
Number of Items: 5
Press any key to continue . . .

Getting an Item From a List

After adding items to a collection, you can retrieve them to do what you intended the list for. To retrieve an item, you can locate it by its position, the same way you would do for an array. Having this index, you can check whether the position specified is negative or higher than the current count of items. If it is, there is nothing much to do since the index would be wrong. If the index is in the right range, you can retrieve its corresponding item. The method to do this can be implemented as follows:

using namespace System;

int MaxCount = 20;

public ref class CCollection
{
private:
    // This collection will be a list of decimal numbers
    array<double> ^ Item;

private:
    // This is the size of the collection
    int size;

public:
    // This represents the number of items in the collection
    property int Count
    {
        int get() { return size; }
    }
    
    bool Add(double item);
    double Retrieve(int pos);

public:
    CCollection();
};

CCollection::CCollection()
{
    Item = gcnew array<double>(MaxCount);
    size = 0;
}

// Adds a new item to the list if the list is not full
// Increases the number of items in the list
// Returns true if the item was added, otherwise returns false
bool CCollection::Add(double item)
{
    // Make sure the list is not yet full
    if (size < 20)
    {
        // Since the list is not full, add the "item" at the end
        Item[size] = item;
        // Increase the count and return the new position
        size++;

        // Indicate that the item was successfully added
        return true;
    }

    // If the item was not added, return false;
    return false;
}

// Retrieves an item from the list based on the specified index
double CCollection::Retrieve(int pos)
{
    // Make sure the index is in the range
    if( (pos >= 0) && (pos <= size) )
        return this->Item[pos];

    // If the index was wrong, return 0
    return 0;
}

int main()
{
    CCollection ^ list = gcnew CCollection;

    Console::WriteLine(L"Number of Items: {0}\n", list->Count);

    list->Add(224.52);
    list->Add(60.48);
    list->Add(1250.64);
    list->Add(8.86);
    list->Add(1005.36);

     for(int i = 0; i < list->Count; i++)
         Console::WriteLine(L"Item {0}: {1}", i + 1, list->Retrieve(i));

    Console::WriteLine(L"\nNumber of Items: {0}", list->Count);

    return 0;
}

This would produce:

Number of Items: 0
Item 1: 224.52
Item 2: 60.48
Item 3: 1250.64
Item 4: 8.86
Item 5: 1005.36
Number of Items: 5

Press any key to continue . . .

Inserting an Item in the List

Inserting a new item in the collection allows you to add one at a position of your choice. To insert an item in the list, you must provide the new item and the desired position. Before performing this operation, you must check two things. First, the list must not be empty. Second, the specified position must be in the allowed range.

The method can be implemented as follows:

using namespace System;

int MaxCount = 20;

public ref class CCollection
{
private:
    // This collection will be a list of decimal numbers
    array<double> ^ Item;

private:
    // This is the size of the collection
    int size;

public:
    // This represents the number of items in the collection
    property int Count
    {
        int get() { return size; }
    }
    
    bool Add(double item);
    double Retrieve(int pos);
    bool Insert(double itm, int pos);

public:
    CCollection();
};

CCollection::CCollection()
{
    Item = gcnew array<double>(MaxCount);
    size = 0;
}

// Adds a new item to the list if the list is not full
// Increases the number of items in the list
// Returns true if the item was added, otherwise returns false
bool CCollection::Add(double item)
{
    // Make sure the list is not yet full
    if (size < 20)
    {
        // Since the list is not full, add the "item" at the end
        Item[size] = item;
        // Increase the count and return the new position
        size++;

        // Indicate that the item was successfully added
        return true;
    }

    // If the item was not added, return false;
    return false;
}

// Retrieves an item from the list based on the specified index
double CCollection::Retrieve(int pos)
{
    // Make sure the index is in the range
    if( (pos >= 0) && (pos <= size) )
        return this->Item[pos];

    // If the index was wrong, return 0
    return 0;
}

// Before performing this operation, check that
// 1. The list is not full
// 2. The specified position is in an allowable range
// Inserts a new item at a specified position in the list
// After the new item is inserted, the count is increased
bool CCollection::Insert(double itm, int pos)
{
    // Check that the item can be added to the list
    if( (size < 20) && (pos >= 0) && (pos <= size) )
    {
        // Since there is room,
        // starting from the end of the list to the new position,
        // push each item to the next or up
        // to create room for the new item
        for(int i = size; i > pos - 1; i--)
            this->Item[i + 1] = this->Item[i];

        // Now that we have room, put the new item in the position created
        Item[pos] = itm;

        // Since we have added a new item, increase the count
        size++;

        // Indicate that the operation was successful
        return true;
    }

    // Since the item could not be added, return false
    return false;
}

int main()
{
    CCollection ^ list = gcnew CCollection;

    Console::WriteLine(L"Number of Items: {0}\n", list->Count);

    list->Add(224.52);
    list->Add(60.48);
    list->Add(1250.64);
    list->Add(8.86);
    list->Add(1005.36);

    for(int i = 0; i < list->Count; i++)
        Console::WriteLine(L"Item {0}: {1}", i + 1, list->Retrieve(i));

    Console::WriteLine(L"\nNumber of Items: {0}\n", list->Count);

    list->Insert(-707.16, 2);

    for(int i = 0; i < list->Count; i++)
        Console::WriteLine(L"Item {0}: {1}", i + 1, list->Retrieve(i));

    Console::WriteLine(L"\nNumber of Items: {0}\n", list->Count);

    return 0;
}

This would produce:

Number of Items: 0
Item 1: 224.52
Item 2: 60.48
Item 3: 1250.64
Item 4: 8.86
Item 5: 1005.36

Number of Items: 5

Item 1: 224.52
Item 2: 60.48
Item 3: -707.16
Item 4: 1250.64
Item 5: 8.86
Item 6: 1005.36
Number of Items: 6

Press any key to continue . . .

Removing an Item From the List

Another operation you can perform on a collection consists of deleting an item. This is also referred to as removing the item. To delete an item from the collection, you can provide its position. Before performing the operation, you can first check that the specified position is valid. The method to perform this operation can be implemented as follows:

using namespace System;

int MaxCount = 20;

public ref class CCollection
{
private:
    // This collection will be a list of decimal numbers
    array<double> ^ Item;

private:
    // This is the size of the collection
    int size;

public:
    // This represents the number of items in the collection
    property int Count
    {
        int get() { return size; }
    }
    
    bool Add(double item);
    double Retrieve(int pos);
    bool Insert(double itm, int pos);
    bool Delete(int pos);

public:
    CCollection();
};

CCollection::CCollection()
{
    Item = gcnew array<double>(MaxCount);
    size = 0;
}

// Adds a new item to the list if the list is not full
// Increases the number of items in the list
// Returns true if the item was added, otherwise returns false
bool CCollection::Add(double item)
{
    // Make sure the list is not yet full
    if (size < 20)
    {
        // Since the list is not full, add the "item" at the end
        Item[size] = item;
        // Increase the count and return the new position
        size++;

        // Indicate that the item was successfully added
        return true;
    }

    // If the item was not added, return false;
    return false;
}

// Retrieves an item from the list based on the specified index
double CCollection::Retrieve(int pos)
{
    // Make sure the index is in the range
    if( (pos >= 0) && (pos <= size) )
        return this->Item[pos];

    // If the index was wrong, return 0
    return 0;
}

// Before performing this operation, check that
// 1. The list is not full
// 2. The specified position is in an allowable range
// Inserts a new item at a specified position in the list
// After the new item is inserted, the count is increased
bool CCollection::Insert(double itm, int pos)
{
    // Check that the item can be added to the list
    if( (size < 20) && (pos >= 0) && (pos <= size) )
    {
        // Since there is room,
        // starting from the end of the list to the new position,
        // push each item to the next or up
        // to create room for the new item
        for(int i = size; i > pos - 1; i--)
            this->Item[i + 1] = this->Item[i];

        // Now that we have room, put the new item in the position created
        Item[pos] = itm;

        // Since we have added a new item, increase the count
        size++;

        // Indicate that the operation was successful
        return true;
    }

    // Since the item could not be added, return false
    return false;
}

// Removes an item from the list
// First check that the specified position is valid
// Deletes the item at that position and decreases the count
bool CCollection::Delete(int pos)
{
    // Make sure the position specified is in the range
    if (pos >= 0 && pos <= size)
    {
        // Since there is room, starting at the specified position,
        // Replace each item by the next
        for (int i = pos; i < this->size; i++)
            this->Item[i] = this->Item[i + 1];

        // Since an item has been removed, decrease the count
        this->size--;

        // Indicate that the operation was successful
        return true;
    }

    // Since the position was out of range, return false
    return false;
}

int main()
{
    CCollection ^ list = gcnew CCollection;

    Console::WriteLine(L"Number of Items: {0}\n", list->Count);

    list->Add(224.52);
    list->Add(60.48);
    list->Add(1250.64);
    list->Add(8.86);
    list->Add(1005.36);

    for(int i = 0; i < list->Count; i++)
        Console::WriteLine(L"Item {0}: {1}", i + 1, list->Retrieve(i));

    Console::WriteLine(L"\nNumber of Items: {0}\n", list->Count);

    list->Insert(-707.16, 2);
    list->Insert(-369952.274, 4);

    for(int i = 0; i < list->Count; i++)
        Console::WriteLine(L"Item {0}: {1}", i + 1, list->Retrieve(i));

    Console::WriteLine(L"\nNumber of Items: {0}\n", list->Count);

    list->Delete(5);
    list->Delete(3);

    for(int i = 0; i < list->Count; i++)
        Console::WriteLine(L"Item {0}: {1}", i + 1, list->Retrieve(i));
    Console::WriteLine(L"\nNumber of Items: {0}\n", list->Count);

    return 0;
}

This would produce:

Number of Items: 0

Item 1: 224.52
Item 2: 60.48
Item 3: 1250.64
Item 4: 8.86
Item 5: 1005.36

Number of Items: 5

Item 1: 224.52
Item 2: 60.48
Item 3: -707.16
Item 4: 1250.64
Item 5: -369952.274
Item 6: 8.86
Item 7: 1005.36

Number of Items: 7

Item 1: 224.52
Item 2: 60.48
Item 3: -707.16
Item 4: -369952.274
Item 5: 1005.36

Number of Items: 5

Press any key to continue . . .
 

Previous Copyright © 2007-2013, FunctionX Home