Home

C++/CLI Collections: Operations on a Collection

 

Adding an Item

The primary operation you can perform on a list is to add a new item to it, since a list is fundamentally empty when it starts. In order to indicate that you want to add an item to the list, you can create a method that receives an item as argument. For the return type, you have two main options. Because the main job of this method is to add a new item, which it hardly fails to do if you implement it right, it can be defined as void. Alternatively, you can make it return the position of the new item in the list. Here is an example:

public ref class CNumbers
{
private:
    int size;
    CNumber ^ Sample;

public:
    CNumber ^ Head;

    CNumbers()
    {
        size = 0;
        Head = nullptr;
    }

    property int Count
    {
        int get() { return size; }
    }

    int Add(CNumber ^ NewItem)
    {
        CNumber ^ Sample = gcnew CNumber;

        Sample = NewItem;
        Sample->Next = Head;
        Head = Sample;
        return size++;
    }
};

int main()
{
    CNumbers ^ nbrs = gcnew CNumbers;

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

    CNumber ^ one = gcnew CNumber;
    one->Item = 2937.45;
    nbrs->Add(one);

    one = gcnew CNumber;
    one->Item = 329.459;
    nbrs->Add(one);

    one = gcnew CNumber;
    one->Item = 98.23456;
    nbrs->Add(one);

    one = gcnew CNumber;
    one->Item = 32465.645;
    nbrs->Add(one);

    return 0;
}

Practical LearningPractical Learning: Adding Items to a Collection

  1. Change the FlowerInventory.h header file as follows:
     
    #pragma once
    
    #include "Flower.h"
    
    public ref class CFlowerInventory : CFlower
    { 
    public:
        CFlower ^ Inventory;
    
        CFlowerInventory(void);
        CFlower ^ Head;
    	
        int Add(CFlower ^ obj);
    };
  2. Access the FlowerInventory.cpp source file and change it as follows:
     
    #include "FlowerInventory.h"
    
    CFlowerInventory::CFlowerInventory(void)
    {
        Head = nullptr;
        Inventory = gcnew CFlower;
    }
    
    int CFlowerInventory::Add(CFlower ^ NewFlower)
    {
        CFlower ^ Sample = gcnew CFlower;
    
        Sample = NewFlower;
        Sample->Next = Head;
        Head = Sample;
        return items++;
    }
  3. Save all

Retrieving an Item

Once a list exists, the user can explore it. One of the operations performed on items is to locate and retrieve one. To do this, you can create a method that takes as argument an index. The method would examine the argument with regards to the number of items in the list to make sure the argument's value is in the range of the current items of the list. If the number is too low or too high, the method can return nullptr. If the number is in the range, the method can return the item at that position. Here is an example:

using namespace System;

public ref class CNumber
{
public:
    double Item;
    CNumber ^ Next;
};

public ref class CNumbers
{
private:
    int size;
    CNumber ^ Sample;

public:
    CNumber ^ Head;

    CNumbers()
    {
        size = 0;
        Head = nullptr;
    }

    property int Count
    {
        int get() { return size; }
    }

    int Add(CNumber ^ NewItem)
    {
        CNumber ^ Sample = gcnew CNumber;

        Sample = NewItem;
        Sample->Next = Head;
        Head = Sample;
        return size++;
    }

    CNumber ^ Retrieve(int Position)
    {
        CNumber ^ Current = Head;

        for (int i = Count - 1; i > Position && Current != nullptr; i--)
            Current = Current->Next;
        return Current;
    }
};

int main()
{
    CNumbers ^ nbrs = gcnew CNumbers;

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

    CNumber ^ one = gcnew CNumber;
    one->Item = 2937.45;
    nbrs->Add(one);one = gcnew CNumber;
    one->Item = 329.459;
    nbrs->Add(one);one = gcnew CNumber;
    one->Item = 98.23456;
    nbrs->Add(one);one = gcnew CNumber;
    one->Item = 32465.645;
    nbrs->Add(one);

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

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

    Console::WriteLine();
    return 0;
}

This would produce:

Number of Items: 0


Number of Items: 4

Number: 2937.45
Number: 329.459
Number: 98.23456
Number: 32465.645

Press any key to continue . . .

Practical LearningPractical Learning: Retrieving the Items of a Collection

  1. Access the FlowerInventory.h header file and add the following method:
     
    #pragma once
    
    #include "Flower.h"
    
    public ref class CFlowerInventory : CFlower
    { 
    public:
        CFlower ^ Inventory;
    
    	CFlowerInventory(void);
        CFlower ^ Head;
    	
        int Add(CFlower ^ obj);
        CFlower ^ Get(int index);
    };
  2. Access the FlowerInventory.cpp source file and add the following method:
     
    #include "FlowerInventory.h"
    
    CFlowerInventory::CFlowerInventory(void)
    {
        Head = nullptr;
        Inventory = gcnew CFlower;
    }
    
    int CFlowerInventory::Add(CFlower ^ NewFlower)
    {
        CFlower ^ Sample = gcnew CFlower;
    
        Sample = NewFlower;
        Sample->Next = Head;
        Head = Sample;
        return items++;
    }
    
    CFlower ^ CFlowerInventory::Get(int index)
    {
        CFlower ^ Current = Head;
    
        for(int i = Count - 1;
            i > index && Current != nullptr;
            i--)
            Current = Current->Next;
        return Current;
    }
  3. Access the Exercise.cpp source file and change it as follows:
     
    #include "OrderProcessing.h"
    #include "FlowerInventory.h"
    
    using namespace System;
    
    int main()
    {
        CFlowerInventory ^ flowers = gcnew CFlowerInventory;
        CFlower ^ nice;
    
        nice = gcnew CFlower;
        nice->Type  = Lilies;
        nice->Color = White;
        nice->Arrangement = Bouquet;
        nice->UnitPrice = 39.95;
        flowers->Add(nice);
    
        nice = gcnew CFlower;
        nice->Type = Daisies;
        nice->Color = Various;
        nice->Arrangement = Bouquet;
        nice->UnitPrice = 40.50;
        flowers->Add(nice);
    
        nice = gcnew CFlower;
        nice->Type = Carnations;
        nice->Color = Lavender;
        nice->Arrangement = Any;
        nice->UnitPrice = 34.85;
        flowers->Add(nice);
    
        nice = gcnew CFlower;
        nice->Type = Roses;
        nice->Color = Pink;
        nice->Arrangement = Bouquet;
        nice->UnitPrice = 29.95;
        flowers->Add(nice);
    
        nice = gcnew CFlower;
        nice->Type = Daisies;
        nice->Color = Yellow;
        nice->Arrangement = Vase;
        nice->UnitPrice = 29.95;
        flowers->Add(nice);

     

    Lilies
    Daisies
    Carnation
    Roses
    Daisies

     

     

        Console::WriteLine(L"//=//=//=//=//=//=//=//=//=//=//=//=//=//=//");
        Console::WriteLine(L"Total: {0} flower items in current inventory",
        flowers->Count);
        Console::WriteLine(L"--------------------------------------------");
        Console::WriteLine(L"Inventory Summary");
        for(int i = 0; i < flowers->Count; i++)
        {
            Console::WriteLine(L"------------------------");
            Console::WriteLine(L"Flower Information");
            Console::Write(L"Type:        ");
            switch(flowers->Get(i)->Type)
            {
            case 1:
                Console::WriteLine(L"Roses");
                break;
            case 2:
                Console::WriteLine(L"Lilies");
                break;
            case 3:
                Console::WriteLine(L"Daisies");
                break;
            case 4:
                Console::WriteLine(L"Carnations");
                break;
            case 5:
                Console::WriteLine(L"Live Plant");
                break;
            default:
                Console::WriteLine(L"Mixed");
            }
       
            Console::Write(L"Color:       ");
            switch(flowers->Get(i)->Color)
            {
            case 1:
                Console::WriteLine(L"Red");
                break;
            case 2:
                Console::WriteLine(L"White");
                break;
            case 3:
                Console::WriteLine(L"Yellow");
                break;
            case 4:
                Console::WriteLine(L"Pink");
                break;
            case 5:
                Console::WriteLine(L"Orange");
                break;
            case 6:
                Console::WriteLine(L"Blue");
                break;
            case 7:
                Console::WriteLine(L"Lavender");
                break;
            default:
                Console::WriteLine(L"Various");
            }
    
            Console::Write(L"Arrangement: ");
            switch(flowers->Get(i)->Arrangement)
            {
            case 1:
                Console::WriteLine(L"Bouquet");
                break;
            case 2:
                Console::WriteLine(L"Vase");
                break;
            case 3:
                Console::WriteLine(L"Basket");
                break;
            default:
                Console::WriteLine(L"Any");
            }
    
            Console::WriteLine(L"Unit Price:  {0:F}",
    	    flowers->Get(i)->UnitPrice);
        }
        Console::WriteLine(L"//=//=//=//=//=//=//=//=//=//=//=//=//=//=//");
    
        Console::WriteLine();
        return 0;
    }
  4. Execute the application to view the result:
     
    //=//=//=//=//=//=//=//=//=//=//=//=//=//=//
    Total: 5 flower items in current inventory
    --------------------------------------------
    Inventory Summary
    ------------------------
    Flower Information
    Type:        Lilies
    Color:       White
    Arrangement: Bouquet
    Unit Price:  39.95
    ------------------------
    Flower Information
    Type:        Daisies
    Color:       Various
    Arrangement: Bouquet
    Unit Price:  40.50
    ------------------------
    Flower Information
    Type:        Carnations
    Color:       Lavender
    Arrangement: Any
    Unit Price:  34.85
    ------------------------
    Flower Information
    Type:        Roses
    Color:       Pink
    Arrangement: Bouquet
    Unit Price:  29.95
    ------------------------
    Flower Information
    Type:        Daisies
    Color:       Yellow
    Arrangement: Vase
    Unit Price:  29.95
    //=//=//=//=//=//=//=//=//=//=//=//=//=//=//
    
    Press any key to continue . . .
  5. Close the DOS window

Removing an Item

Deleting an item consists of removing it from the list. There are two main approaches you can use. You can simply ask the class to delete an item. In this case, it is usually the item at the end that gets deleted. If you do this, make sure you perform other routines operations such as decrementing the count of items in the list. Here is an example:

using namespace System;

public ref class CNumber
{
public:
    double Item;
    CNumber ^ Next;
};

public ref class CNumbers
{
private:
    int size;
    CNumber ^ Sample;

public:
    CNumber ^ Head;

    CNumbers()
    {
        size = 0;
        Head = nullptr;
    }

    property int Count
    {
        int get() { return size; }
    }

    int Add(CNumber ^ NewItem)
    {
        CNumber ^ Sample = gcnew CNumber;

        Sample = NewItem;
        Sample->Next = Head;
        Head = Sample;
        return size++;
    }

    CNumber ^ Retrieve(int Position)
    {
        CNumber ^ Current = Head;

        for (int i = Count - 1; i > Position && Current != nullptr; i--)
            Current = Current->Next;
        return Current;
    }

    bool DeleteFirst()
    {
        if (Head == nullptr)
        {
            Console::WriteLine(L"The list is empty");
            return false;
        }

        CNumber ^ Current;

        Current = Head->Next;
        size--;
        return true;
    }
};

int main()
{
    CNumbers ^ nbrs = gcnew CNumbers;

    CNumber ^ one = gcnew CNumber;
    one->Item = 2937.45;
    nbrs->Add(one);one = gcnew CNumber;
    one->Item = 329.459;
    nbrs->Add(one);one = gcnew CNumber;
    one->Item = 98.23456;
    nbrs->Add(one);one = gcnew CNumber;
    one->Item = 32465.645;
    nbrs->Add(one);

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

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

    nbrs->DeleteFirst();

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

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

This would produce:

Number: 2937.45
Number: 329.459
Number: 98.23456
Number: 32465.645

Number of Items: 4

Number: 329.459
Number: 98.23456
Number: 32465.645

Number of Items: 3

Press any key to continue . . .

Another technique used to delete an item consists of specifying the position of the item to be deleted. To do this, you can pass an argument as the desired position. The method would check the range of values of the current list. If the specified position is beyond the appropriate range, the method can return false, 0, or nullptr, depending on how you create it.

Practical LearningPractical Learning: Retrieving the Items of a Collection

  1. Access the FlowerInventory.h header file and add the following method:
     
    #pragma once
    
    #include "Flower.h"
    
    public ref class CFlowerInventory : CFlower
    { 
    public:
        CFlower ^ Inventory;
    
        CFlowerInventory(void);
        CFlower ^ Head;
    	
        int Add(CFlower ^ obj);
        CFlower ^ Get(int index);
        bool Delete();
    };
  2. Access the FlowerInventory.cpp source file and change it as follows:
     
    #include "FlowerInventory.h"
    
    CFlowerInventory::CFlowerInventory(void)
    {
        Head = nullptr;
        Inventory = gcnew CFlower;
    }
    
    int CFlowerInventory::Add(CFlower ^ NewFlower)
    {
        CFlower ^ Sample = gcnew CFlower;
    
        Sample = NewFlower;
        Sample->Next = Head;
        Head = Sample;
        return items++;
    }
    
    CFlower ^ CFlowerInventory::Get(int index)
    {
        CFlower ^ Current = Head;
    
        for(int i = Count - 1;
            i > index && Current != nullptr;
            i--)
            Current = Current->Next;
        return Current;
    }
    
    bool CFlowerInventory::Delete()
    {
        if (Head == nullptr)
        {
            Console::WriteLine(L"The inventory is empty");
            return false;
        }
    
        CFlower ^ Current;
    
        Current = Head->Next;
        Head->Next = Current->Next;
        items--;
        return true;    
    }
  3. Access the Exercise.cpp file and change it as follows:
     
    #include "OrderProcessing.h"
    #include "FlowerInventory.h"
    
    using namespace System;
    
    CFlowerInventory ^ CreateFlowers()
    {
        CFlowerInventory ^ NewFlowers = gcnew CFlowerInventory();
        CFlower ^ nice;
    
        nice = gcnew CFlower;
        nice->Type  = Lilies;
        nice->Color = White;
        nice->Arrangement = Bouquet;
        nice->UnitPrice = 39.95;
        NewFlowers->Add(nice);
    
        nice = gcnew CFlower;
        nice->Type = Daisies;
        nice->Color = Various;
        nice->Arrangement = Bouquet;
        nice->UnitPrice = 40.50;
        NewFlowers->Add(nice);
    
        nice = gcnew CFlower;
        nice->Type = Carnations;
        nice->Color = Lavender;
        nice->Arrangement = Any;
        nice->UnitPrice = 34.85;
        NewFlowers->Add(nice);
    
        nice = gcnew CFlower;
        nice->Type = Roses;
        nice->Color = Pink;
        nice->Arrangement = Bouquet;
        nice->UnitPrice = 29.95;
        NewFlowers->Add(nice);
    
        nice = gcnew CFlower;
        nice->Type = Daisies;
        nice->Color = Yellow;
        nice->Arrangement = Vase;
        nice->UnitPrice = 29.95;
        NewFlowers->Add(nice);
    
        return NewFlowers;
    }
    
    void ShowInventory(CFlowerInventory ^ FlowersList)
    {
        Console::WriteLine(L"//=//=//=//=//=//=//=//=//=//=//=//=//=//=//");
        Console::WriteLine(L"Total: {0} flower items in current inventory",
        FlowersList->Count);
        Console::WriteLine(L"--------------------------------------------");
        Console::WriteLine(L"Inventory Summary");
        for(int i = 0; i < FlowersList->Count; i++)
        {
            Console::WriteLine(L"------------------------");
            Console::WriteLine(L"Flower Information");
            Console::Write(L"Type:        ");
            switch(FlowersList->Get(i)->Type)
            {
            case 1:
                Console::WriteLine(L"Roses");
                break;
            case 2:
                Console::WriteLine(L"Lilies");
                break;
            case 3:
                Console::WriteLine(L"Daisies");
                break;
            case 4:
                Console::WriteLine(L"Carnations");
                break;
            case 5:
                Console::WriteLine(L"Live Plant");
                break;
            default:
                Console::WriteLine(L"Mixed");
            }
       
            Console::Write(L"Color:       ");
            switch(FlowersList->Get(i)->Color)
            {
            case 1:
                Console::WriteLine(L"Red");
                break;
            case 2:
                Console::WriteLine(L"White");
                break;
            case 3:
                Console::WriteLine(L"Yellow");
                break;
            case 4:
                Console::WriteLine(L"Pink");
                break;
            case 5:
                Console::WriteLine(L"Orange");
                break;
            case 6:
                Console::WriteLine(L"Blue");
                break;
            case 7:
                Console::WriteLine(L"Lavender");
                break;
            default:
                Console::WriteLine(L"Various");
            }
    
            Console::Write(L"Arrangement: ");
            switch(FlowersList->Get(i)->Arrangement)
            {
            case 1:
                Console::WriteLine(L"Bouquet");
                break;
            case 2:
                Console::WriteLine(L"Vase");
                break;
            case 3:
                Console::WriteLine(L"Basket");
                break;
            default:
                Console::WriteLine(L"Any");
            }
    
            Console::WriteLine(L"Unit Price:  {0:F}",
    	    FlowersList->Get(i)->UnitPrice);
        }
        Console::WriteLine(L"//=//=//=//=//=//=//=//=//=//=//=//=//=//=//");
    }
    int main()
    {
        CFlowerInventory ^ flowers = CreateFlowers();
    
        ShowInventory(flowers);
    
        flowers->Delete();
        flowers->Delete();
    
        Console::Write(L"Press Enter to see the new inventory...");
        Console::ReadLine();
    
        ShowInventory(flowers);
    
        Console::WriteLine();
        return 0;
    }
  4. Execute the application to view the result:
     
    //=//=//=//=//=//=//=//=//=//=//=//=//=//=//
    Total: 5 flower items in current inventory
    --------------------------------------------
    Inventory Summary
    ------------------------
    Flower Information
    Type:        Lilies
    Color:       White
    Arrangement: Bouquet
    Unit Price:  39.95
    ------------------------
    Flower Information
    Type:        Daisies
    Color:       Various
    Arrangement: Bouquet
    Unit Price:  40.50
    ------------------------
    Flower Information
    Type:        Carnations
    Color:       Lavender
    Arrangement: Any
    Unit Price:  34.85
    ------------------------
    Flower Information
    Type:        Roses
    Color:       Pink
    Arrangement: Bouquet
    Unit Price:  29.95
    ------------------------
    Flower Information
    Type:        Daisies
    Color:       Yellow
    Arrangement: Vase
    Unit Price:  29.95
    //=//=//=//=//=//=//=//=//=//=//=//=//=//=//
    Press Enter to see the new inventory...
    //=//=//=//=//=//=//=//=//=//=//=//=//=//=//
    Total: 3 flower items in current inventory
    --------------------------------------------
    Inventory Summary
    ------------------------
    Flower Information
    Type:        Lilies
    Color:       White
    Arrangement: Bouquet
    Unit Price:  39.95
    ------------------------
    Flower Information
    Type:        Daisies
    Color:       Various
    Arrangement: Bouquet
    Unit Price:  40.50
    ------------------------
    Flower Information
    Type:        Daisies
    Color:       Yellow
    Arrangement: Vase
    Unit Price:  29.95
    //=//=//=//=//=//=//=//=//=//=//=//=//=//=//
    
    Press any key to continue . . .
  5. Close the DOS window

Locating an Item

One of the operations hardly performed on a list is to find an item. This is because if you ask a list to locate a particular item, you must provide as much information as possible. Probably the most expedient way you can do this is to completely define an item and pass it to the list. Only if the item is found in the list would it be recognized.

Here is an example:

using namespace System;

public ref class CNumber
{
public:
    double Item;
    CNumber ^ Next;
};

public ref class CNumbers
{
private:
    int size;
    CNumber ^ Sample;

public:
    CNumber ^ Head;

    CNumbers()
    {
        size = 0;
        Head = nullptr;
    }

    property int Count
    {
        int get() { return size; }
    }

    int Add(CNumber ^ NewItem)
    {
        CNumber ^ Sample = gcnew CNumber;

        Sample = NewItem;
        Sample->Next = Head;
        Head = Sample;
        return size++;
    }

    CNumber ^ Retrieve(int Position)
    {
        CNumber ^ Current = Head;

        for (int i = Count - 1; i > Position && Current != nullptr; i--)
            Current = Current->Next;
        return Current;
    }

    bool DeleteFirst()
    {
        if (Head == nullptr)
        {
            Console::WriteLine(L"The list is empty");
            return false;
        }

        CNumber ^ Current;

        Current = Head->Next;
        size--;
        return true;
    }

    bool Find(CNumber ^ toFind)
    {
        CNumber ^ Current = gcnew CNumber;

        if (toFind == nullptr)
            return false;

        for (Current = Head; Current != nullptr; Current = Current->Next)
        {
            if( Current->Item  == toFind->Item )
                return true;
        }

        return false;
    }
};

int main()
{
    CNumbers ^ nbrs = gcnew CNumbers;

    CNumber ^ one = gcnew CNumber;
    one->Item = 2937.45;
    nbrs->Add(one);
	
    one = gcnew CNumber;
    one->Item = 329.459;
    nbrs->Add(one);

    one = gcnew CNumber;
    one->Item = 98.23456;
    nbrs->Add(one);

    one = gcnew CNumber;
    one->Item = 26486.56;
    nbrs->Add(one);

    one = gcnew CNumber;
    one->Item = 32465.645;
    nbrs->Add(one);

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

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

    CNumber ^ nbrToFind = gcnew CNumber;
    nbrToFind->Item = 26486.56;

    bool Found = nbrs->Find(nbrToFind);

    if( Found == true )
        Console::WriteLine(L"The number {0} was found in the list",
		nbrToFind->Item);
    else
        Console::WriteLine(L"The number {0} was NOT found in the list",
		nbrToFind->Item);

    nbrToFind = gcnew CNumber;
    nbrToFind->Item = 50883.82;

    Found = nbrs->Find(nbrToFind);

    if( Found == true )
        Console::WriteLine(L"The number {0} was found in the list\n",
		nbrToFind->Item);
    else
        Console::WriteLine(L"The number {0} was NOT found in the list\n",
		nbrToFind->Item);

    return 0;
}

This would produce:

Number: 2937.45
Number: 329.459
Number: 98.23456
Number: 26486.56
Number: 32465.645

Number of Items: 5

The number 26486.56 was found in the list
The number 50883.82 was NOT found in the list

Press any key to continue . . .
 

Previous Copyright © 2007-2013, FunctionX Home