Home

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 class Numbers
{
    int size;
    Number Sample;
    public Number Head;

    public Numbers()
    {
        size = 0;
        Head = null;
    }

    public int Count
    {
        get { return size; }
    }

    public int Add(Number NewItem)
    {
        Number Sample = new Number();

        Sample = NewItem;
        Sample.Next = Head;
        Head = Sample;
        return size++;
    }
}
 

Practical LearningPractical Learning: Adding Items to a Collection

  1. Change the FlowerInventory.cs file as follows:
     
    using System;
    
    namespace FlowerShop4
    {
        public abstract class AFlower
        {
            protected int items;
    
            public AFlower()
            {
                items = 0;
            }
    
            public int Count
            {
                get { return items; }
            }
    
            public abstract int Add(Flower obj);
        }
    
        public class FlowerInventory : AFlower
        {
            public Flower Head;
            public Flower Inventory;
    
            public FlowerInventory()
            {
                Head = null;
            }
    
            public override int Add(Flower NewFlower)
            {
                Flower Sample = new Flower();
    
                Sample = NewFlower;
                Sample.Next = Head;
                Head = Sample;
                return items++;
            }
        }
    }
  2. Access the Program.cs and change it as follows:
     
    using System;
    
    namespace FlowerShop4
    {
        public class Program
        {
            static int Main(string[] args)
            {
                FlowerInventory flowers = new FlowerInventory();
                Flower nice;
    
                nice = new Flower();
                nice.Type = FlowerType.Lilies;
                nice.Color = FlowerColor.White;
                nice.Arrangement = FlowerArrangement.Bouquet;
                nice.UnitPrice = 39.95M;
                flowers.Add(nice);
    
                nice = new Flower();
                nice.Type = FlowerType.Daisies;
                nice.Color = FlowerColor.Mixed;
                nice.Arrangement = FlowerArrangement.Bouquet;
                nice.UnitPrice = 40.50M;
                flowers.Add(nice);
    
                nice = new Flower();
                nice.Type = FlowerType.Carnations;
                nice.Color = FlowerColor.Lavender;
                nice.Arrangement = FlowerArrangement.Any;
                nice.UnitPrice = 34.85M;
                flowers.Add(nice);
    
                nice = new Flower();
                nice.Type = FlowerType.Roses;
                nice.Color = FlowerColor.Pink;
                nice.Arrangement = FlowerArrangement.Bouquet;
                nice.UnitPrice = 29.95M;
                flowers.Add(nice);
    
                nice = new Flower();
                nice.Type = FlowerType.Daisies;
                nice.Color = FlowerColor.Yellow;
                nice.Arrangement = FlowerArrangement.Vase;
                nice.UnitPrice = 29.95M;
                flowers.Add(nice);
    
                return 0;
            }
        }
    }
     

     


    Lilies
    Daisies
    Carnation
    Roses
    Daisies

     

     

     

     

  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 null or 0. If the number is in the range, the method can return the item at that position. Here is an example:

public class Numbers
{
    int size;
    Number Sample;
    public Number Head;

    public Numbers()
    {
        size = 0;
        Head = null;
    }

    public int Count
    {
        get { return size; }
    }

    public int Add(Number NewItem)
    {
        Number Sample = new Number();

        Sample = NewItem;
        Sample.Next = Head;
        Head = Sample;
        return size++;
    }

    public Number Retrieve(int Position)
    {
        Number Current = Head;

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

Practical LearningPractical Learning: Retrieving the Items of a Collection

  1. Access the FlowerInventory.cs file and add the following method:
     
    using System;
    
    namespace FlowerShop4
    {
        public abstract class AFlower
        {
            protected int items;
    
            public AFlower()
            {
                items = 0;
            }
    
            public int Count
            {
                get { return items; }
            }
    
            public abstract int Add(Flower obj);
            public abstract Flower Get(int index);
        }
    
        public class FlowerInventory : AFlower
        {
            public Flower Head;
            public Flower Inventory;
    
            public FlowerInventory()
            {
                Head = null;
            }
    
            public override int Add(Flower NewFlower)
            {
                Flower Sample = new Flower();
    
                Sample = NewFlower;
                Sample.Next = Head;
                Head = Sample;
                return items++;
            }
    
            public override Flower Get(int index)
            {
                Flower Current = Head;
    
                for(int i = Count - 1;
                    i > index && Current != null;
                    i--)
                    Current = Current.Next;
                return Current;
            }
        }
    }
  2. Access the Program.cs file and change it as follows:
     
    using System;
    
    namespace FlowerShop4
    {
        public class Program
        {
            static int Main(string[] args)
            {
                FlowerInventory flowers = new FlowerInventory();
                Flower nice;
    
                nice = new Flower();
                nice.Type = FlowerType.Lilies;
                nice.Color = FlowerColor.White;
                nice.Arrangement = FlowerArrangement.Bouquet;
                nice.UnitPrice = 39.95M;
                flowers.Add(nice);
    
                nice = new Flower();
                nice.Type = FlowerType.Daisies;
                nice.Color = FlowerColor.Mixed;
                nice.Arrangement = FlowerArrangement.Bouquet;
                nice.UnitPrice = 40.50M;
                flowers.Add(nice);
    
                nice = new Flower();
                nice.Type = FlowerType.Carnations;
                nice.Color = FlowerColor.Lavender;
                nice.Arrangement = FlowerArrangement.Any;
                nice.UnitPrice = 34.85M;
                flowers.Add(nice);
    
                nice = new Flower();
                nice.Type = FlowerType.Roses;
                nice.Color = FlowerColor.Pink;
                nice.Arrangement = FlowerArrangement.Bouquet;
                nice.UnitPrice = 29.95M;
                flowers.Add(nice);
    
                nice = new Flower();
                nice.Type = FlowerType.Daisies;
                nice.Color = FlowerColor.Yellow;
                nice.Arrangement = FlowerArrangement.Vase;
                nice.UnitPrice = 42.75M;
                flowers.Add(nice);
    
              Console.WriteLine("//=//=//=//=//=//=//=//=//=//=//=//=//=//=//");
              Console.WriteLine("Total: {0} flower items in current inventory",
                    flowers.Count);
              Console.WriteLine("--------------------------------------------");
                Console.WriteLine("Inventory Summary");
                for (int i = 0; i < flowers.Count; i++)
                {
                    Console.WriteLine("------------------------");
                    Console.WriteLine("Flower Information");
                    Console.WriteLine("Type:        {0}", flowers.Get(i).Type);
                    Console.WriteLine("Color:       {0}", flowers.Get(i).Color);
                    Console.WriteLine("Arrangement: {0}",
    			 flowers.Get(i).Arrangement);
                    Console.WriteLine("Unit Price:  {0:F}",
    			 flowers.Get(i).UnitPrice);
                }
              Console.WriteLine("//=//=//=//=//=//=//=//=//=//=//=//=//=//=//");
    
                return 0;
            }
        }
    }
  3. 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:       Mixed
    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:  42.75
    //=//=//=//=//=//=//=//=//=//=//=//=//=//=//
    Press any key to continue . . .
  4. 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:

public class Numbers
{
    . . . No Change

    public bool Delete()
    {
        if (Head == null)
        {
            Console.WriteLine("The list is empty");
            return false;
        }

        Number Current;

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

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 null, depending on how you create it.

 
 

Practical LearningPractical Learning: Retrieving the Items of a Collection

  1. Access the FlowerInventory.cs file and add the following method:
     
    using System;
    
    namespace FlowerShop4
    {
        public abstract class AFlower
        {
            protected int items;
    
            public AFlower()
            {
                items = 0;
            }
    
            public int Count
            {
                get { return items; }
            }
    
            public abstract int Add(Flower obj);
            public abstract Flower Get(int index);
            public abstract bool Delete();
        }
    
        public class FlowerInventory : AFlower
        {
            public Flower Head;
            public Flower Inventory;
    
            public FlowerInventory()
            {
                Head = null;
            }
    
            public override int Add(Flower NewFlower)
            {
                Flower Sample = new Flower();
    
                Sample = NewFlower;
                Sample.Next = Head;
                Head = Sample;
                return items++;
            }
    
            public override Flower Get(int index)
            {
                Flower Current = Head;
    
                for(int i = Count - 1;
                    i > index && Current != null;
                    i--)
                    Current = Current.Next;
                return Current;
            }
    
            public override bool Delete()
            {
                if (Head == null)
                {
                    Console.WriteLine("The inventory is empty");
                    return false;
                }
    
                Flower Current;
    
                Current = Head.Next;
                Head.Next = Current.Next;
                items--;
                return true;
            }
        }
    }
  2. Access the Program.cs file and change it as follows:
     
    using System;
    
    namespace FlowerShop4
    {
        public class Program
        {
            static int Main(string[] args)
            {
                FlowerInventory flowers = new FlowerInventory();
                Flower nice;
    
                nice = new Flower();
                nice.Type = FlowerType.Lilies;
                nice.Color = FlowerColor.White;
                nice.Arrangement = FlowerArrangement.Bouquet;
                nice.UnitPrice = 39.95M;
                flowers.Add(nice);
    
                nice = new Flower();
                nice.Type = FlowerType.Daisies;
                nice.Color = FlowerColor.Mixed;
                nice.Arrangement = FlowerArrangement.Bouquet;
                nice.UnitPrice = 40.50M;
                flowers.Add(nice);
    
                nice = new Flower();
                nice.Type = FlowerType.Carnations;
                nice.Color = FlowerColor.Lavender;
                nice.Arrangement = FlowerArrangement.Any;
                nice.UnitPrice = 34.85M;
                flowers.Add(nice);
    
                nice = new Flower();
                nice.Type = FlowerType.Roses;
                nice.Color = FlowerColor.Pink;
                nice.Arrangement = FlowerArrangement.Bouquet;
                nice.UnitPrice = 29.95M;
                flowers.Add(nice);
    
                nice = new Flower();
                nice.Type = FlowerType.Daisies;
                nice.Color = FlowerColor.Yellow;
                nice.Arrangement = FlowerArrangement.Vase;
                nice.UnitPrice = 42.75M;
                flowers.Add(nice);
    
                Console.WriteLine("//=//=//=//=//=//=//=//=//=//=//=//=//=//=//");
                Console.WriteLine("Total: {0} flower items in current inventory",
                    flowers.Count);
                Console.WriteLine("--------------------------------------------");
                Console.WriteLine("Inventory Summary");
                for (int i = 0; i < flowers.Count; i++)
                {
                    Console.WriteLine("------------------------");
                    Console.WriteLine("Flower Information");
                    Console.WriteLine("Type:        {0}",
                                      flowers.Get(i).Type);
                    Console.WriteLine("Color:       {0}",
                                      flowers.Get(i).Color);
                    Console.WriteLine("Arrangement: {0}",
                 flowers.Get(i).Arrangement);
                    Console.WriteLine("Unit Price:  {0:F}",
                 flowers.Get(i).UnitPrice);
                }
                Console.WriteLine("//=//=//=//=//=//=//=//=//=//=//=//=//=//=//");
    
                flowers.Delete();
                flowers.Delete();
    
                Console.WriteLine("//=//=//=//=//=//=//=//=//=//=//=//=//=//=//");
                Console.WriteLine("Total: {0} flower items in current inventory",
                    flowers.Count);
                Console.WriteLine("--------------------------------------------");
                Console.WriteLine("Inventory Summary");
                for (int i = 0; i < flowers.Count; i++)
                {
                    Console.WriteLine("------------------------");
                    Console.WriteLine("Flower Information");
                    Console.WriteLine("Type:        {0}",
                                      flowers.Get(i).Type);
                    Console.WriteLine("Color:       {0}",
                                      flowers.Get(i).Color);
                    Console.WriteLine("Arrangement: {0}",
                 flowers.Get(i).Arrangement);
                    Console.WriteLine("Unit Price:  {0:F}",
                 flowers.Get(i).UnitPrice);
                }
                Console.WriteLine("//=//=//=//=//=//=//=//=//=//=//=//=//=//=//");
    
                return 0;
            }
        }
    }
  3. 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:       Mixed
    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:  42.75
    //=//=//=//=//=//=//=//=//=//=//=//=//=//=//
    //=//=//=//=//=//=//=//=//=//=//=//=//=//=//
    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:       Mixed
    Arrangement: Bouquet
    Unit Price:  40.50
    ------------------------
    Flower Information
    Type:        Daisies
    Color:       Yellow
    Arrangement: Vase
    Unit Price:  42.75
    //=//=//=//=//=//=//=//=//=//=//=//=//=//=//
    Press any key to continue . . .
  4. 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 System;

public class Number
{
    public double Item;
    public Number Next;
}

public class Numbers
{
    int size;
    Number Sample;
    public Number Head;

    public Numbers()
    {
        size = 0;
        Head = null;
    }

    public int Count
    {
        get { return size; }
    }

    public int Add(Number NewItem)
    {
        Number Sample = new Number();

        Sample = NewItem;
        Sample.Next = Head;
        Head = Sample;
        return size++;
    }

    public Number Retrieve(int Position)
    {
        Number Current = Head;

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

    public bool Delete()
    {
        if (Head == null)
        {
            Console.WriteLine("The list is empty");
            return false;
        }

        Number Current;

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

    public bool Find(Number toFind)
    {
        Number Current = new Number();

        if (toFind == null)
            return false;

        for (Current = Head; Current != null; Current = Current.Next)
        {
            if( Current.Item  == toFind.Item )
                return true;
        }

        return false;
    }
}

public class Program
{
    static int Main(string[] args)
    {
        Number  real;
        Numbers reals = new Numbers();

        real = new Number();
        real.Item = 2974.03;
        reals.Add(real);

        real = new Number();
        real.Item = 748.25;
        reals.Add(real);

        real = new Number();
        real.Item = 50883.82;
        reals.Add(real);

        real = new Number();
        real.Item = 29.24;
        reals.Add(real);

        real = new Number();
        real.Item = 772.85;
        reals.Add(real);

        real = new Number();
        real.Item = 106628.06;
        reals.Add(real);

        Console.WriteLine("Number of items: {0}", reals.Count);

        for (int i = 0; i < reals.Count; i++)
        {
            Number nbr = reals.Retrieve(i);
            Console.WriteLine("Number[{0}] = {1}", i, nbr.Item);
        }

        reals.Delete();

        Console.WriteLine("\nNumber of items: {0}", reals.Count);

        for (int i = 0; i < reals.Count; i++)
        {
            Number nbr = reals.Retrieve(i);
            Console.WriteLine("Number[{0}] = {1}", i, nbr.Item);
        }

        Number nbrToFind = new Number();
        nbrToFind.Item = 26486.56;

        bool Found = reals.Find(nbrToFind);
        if (Found == true)
            Console.WriteLine("\nThe number {0} was found in the list", nbrToFind.Item);
        else
            Console.WriteLine("\nThe number {0} was NOT found in the list", nbrToFind.Item);

        nbrToFind = new Number();
        nbrToFind.Item = 50883.82;

        Found = reals.Find(nbrToFind);
        if (Found == true)
            Console.WriteLine("The number {0} was found in the list\n", nbrToFind.Item);
        else
            Console.WriteLine("The number {0} was NOT found in the list\n", nbrToFind.Item);

        return 0;
    }
}

This would produce:

Number of items: 6
Number[0] = 2974.03
Number[1] = 748.25
Number[2] = 50883.82
Number[3] = 29.24
Number[4] = 772.85
Number[5] = 106628.06

Number of items: 5
Number[0] = 2974.03
Number[1] = 748.25
Number[2] = 50883.82
Number[3] = 29.24
Number[4] = 106628.06

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

Press any key to continue . . .

 

 

Previous Copyright © 2006-2007 FunctionX, Inc. Next