Showing posts with label Introduction to c# Collections. Show all posts
Showing posts with label Introduction to c# Collections. Show all posts

Thursday, 16 February 2012

Operations on c # Linked Lists

Common Routines of a Linked List

 
Introduction


To enhance the functionality of a linked list, you will need to keep track of what item is the first and which one is the last. This is because some operations must be performed in the starting point of a collection and some others will need to reference the last item. To keep track of these two special nodes, you should (must) create a variable for each:
Linked List
public class Node<T>
{
    public T Value { get; set; }
    public Node<T> Next { get; set; }
}

public class LinkedList<T>
{
    private Node<T> begin;
    private Node<T> end;
}
Of course, when the collection starts, the first and the last items have no value. Therefore, you should initialize them to null or their default value if you are using a primitive type. Here is an example:
public class Node<T>
{
    public T Value { get; set; }
    public Node<T> Next { get; set; }
}

public class LinkedList<T>
{
    private Node<T> begin;
    private Node<T> end;

    public LinkedList()
    {
        begin = null;
        end = null;
    }
}
Other than that, in previous lessons, we saw that you should usually (if not always) provide a way to get the current number of items in a collection. We also saw that you provide a mechanism to get to an item inside the collection. You can use a method or a read-only indexed property for the same purpose. You should also provide a Boolean read-only property that checks whether the list is currently empty or not. Here is an example:
public class Node<T>
{
    public T Value { get; set; }
    public Node<T> Next { get; set; }
}

public class LinkedList<T>
{
    private Node<T> begin;
    private Node<T> end;
    private int size;

    public LinkedList()
    {
        begin = null;
        end = null;
        size = 0;
    }

    public int Count
    {
        get { return size; }
    }
    
    public bool IsEmpty
    {
        get
        {
            if (size == 0)
                return true;
            return false;
        }
    }

    public Node<T> this[int index]
    {
        get
        {
            Node<T> current = begin;

            for (int i = size - 1; (i > index) && (current != null); i--)
                current = current.Next;
            return current;
        }
    }

    public override string ToString()
    {
        string result = "- ";
        Node<T> current = end;

        if (end == null)
        {
            return "The list is currently empty";
            // throw new NullReferenceException("The list is currently empty");
        }
        else
        {
            for (int i = 0; i < size; i++)
            {
                result = result + current.Value + " - ";
                current = current.Next;
            }
        }

        return result;
    }
}
Adding a First Item

The first item of a collection is the last item that was added to the list:
First Item
Still, if you want, you can create a new item and add it as the first in the collection. To do this, create a method that receives the intended item as argument:
  • If the list is currently empty, assign the argument to both the first and the last item
  • If the list already contains at least one item:
    1. Get a reference to the first item
    2. Get the next item to the first and assign it to the next item of the argument
    3. Replace the next item of the first with the argument
  • Increase the number of items in the collection
Here is an example:
using System;

public class Node<T>
{
    public T Value { get; set; }
    public Node<T> Next { get; set; }
}

public class LinkedList<T>
{
    private Node<T> begin;
    private Node<T> end;
    private int size;

    public LinkedList()
    {
        begin = null;
        end = null;
        size = 0;
    }

    public int Count
    {
        get { return size; }
    }

    public bool IsEmpty
    {
        get
        {
            if (size == 0)
                return true;
            return false;
        }
    }

    public void AddFirst(Node<T> item)
    {
        // Get a reference to the node we want to add
        Node<T> current = item;

        // If the list is currently empty, specify that the first
        // and the last are the same and they will receive the argument
        if (begin == null)
            begin = end = current;
        else // If there is already at least one item in the collection
        {
            // Get a reference to the current first item
            Node<T> first = this[0];
            // Get the next item to the first and assign it
            // to the next item of the argument
            current.Next = first.Next;
            // Replace the next item of the first with the argument
            first.Next = current;
        }

        // Increase the count of items
        size++;
    }

    public Node<T> this[int index]
    {
        get
        {
            Node<T> current = end;

            for (int i = size - 1; (i > index) && (current != null); i--)
                current = current.Next;
            return current;
        }
    }
}

public class Exercise
{
    static int Main(string[] args)
    {
        Node<string> name = null;
        LinkedList<string> employees = new LinkedList<string>();

        name = new Node<string>();
        name.Value = "Alan Baxter";
        employees.AddFirst(name);

        name = new Node<string>();
        name.Value = "Joan Bramble";
        employees.AddFirst(name);

        name = new Node<string>();
        name.Value = "Lionel Berg";
        employees.AddFirst(name);

        name = new Node<string>();
        name.Value = "Flora Bruze";
        employees.AddFirst(name);

        name = new Node<string>();
        name.Value = "Christopher Fox";
        employees.AddFirst(name);

        name = new Node<string>();
        name.Value = "Holly Madsen";
        employees.AddFirst(name);

        for (int i = 0; i < employees.Count; i++)
            Console.WriteLine("Name {0}: {1}", i + 1, employees[i].Value);

        return 0;
    }
}
This would produce:
Name 1: Holly Madsen
Name 2: Christopher Fox
Name 3: Flora Bruze
Name 4: Lionel Berg
Name 5: Joan Bramble
Name 6: Alan Baxter
Press any key to continue . . .
Using the First Item

If you use an indexed list to identify the members of a collection, the first item of the list is the one with the index of 0. You can use that feature to get the first member. Here is an example:
public class Exercise
{
    static int Main(string[] args)
    {
        Node<string> name = null;
        LinkedList<string> employees = new LinkedList<string>();

        . . .

        Console.WriteLine("---------------------------------");
        Console.WriteLine("First Item: {0}", employees[0].Value);

        return 0;
    }
}
This would produce:
Name 1: Holly Madsen
Name 2: Christopher Fox
Name 3: Flora Bruze
Name 4: Paul Bertrand Yamaguchi
Name 5: Lionel Berg
Name 6: Joan Bramble
Name 7: Alan Baxter
---------------------------------
First Item: Holly Madsen
Press any key to continue . . .
Remember that our collection class has a field that represents the first item of the list. You can create a method or a read-only property that simply returns that field. Here is an example:
using System;

public class Node<T>
{
    public T Value { get; set; }
    public Node<T> Next { get; set; }

    public Node<T> Nullify()
    {
        return default(Node<T>);
    }
}

public class LinkedList<T>
{
    private Node<T> begin;
    private Node<T> end;
    private int size;

    . . . No Change
    
    public Node First
    {
        get
        {
            // If the list has no item, throw an exception
            if( begin == null ) 
                throw new NullReferenceException(
                 "There is no item in the collection");
            else
            {
                // This variable will be used for looping
                Node temp = null;
                // Get a reference to the first item of the collection
                Node current = begin;

                // Loop through the collection, from one item to the next.
                // This allows you to get to the first item
                while (current != null)
                {
                    temp = current;
                    current = current.Next;
                }

                // Return that first item
                return temp;
            }
        }
    }

}

public class Exercise
{
    static int Main(string[] args)
    {
        Node<string> name = null;
        LinkedList<string> employees = new LinkedList<string>();

        name = new Node<string>();
        name.Value = "Alan Baxter";
        employees.AddFirst(name);

        name = new Node<string>();
        name.Value = "Joan Bramble";
        employees.AddFirst(name);

        name = new Node<string>();
        name.Value = "Lionel Berg";
        employees.AddFirst(name);

        name = new Node<string>();
        name.Value = "Flora Bruze";
        employees.AddFirst(name);

        name = new Node<string>();
        name.Value = "Christopher Fox";
        employees.AddFirst(name);

        name = new Node<string>();
        name.Value = "Holly Madsen";
        employees.AddFirst(name);

        for (int i = 0; i < employees.Count; i++)
            Console.WriteLine("Name {0}: {1}", i + 1, employees[i].Value);

        Console.WriteLine("----------------------------------");
        Console.WriteLine("First Item: {0}", employees.First.Value); // employees[0].Value);

        return 0;
    }
}
This would produce:
Name 1: Holly Madsen
Name 2: Christopher Fox
Name 3: Flora Bruze
Name 4: Lionel Berg
Name 5: Joan Bramble
Name 6: Alan Baxter
----------------------------------
First Item: Holly Madsen
Press any key to continue . . .
Another way you can use the first position is to replace the item in that position with another item.
Adding the Last Item

The technique we saw in the previous lesson about adding a new item to a linked list actually adds the item to the end of the list. This means that we can just name that method AddLast and the result would be the same. Here is an example:
using System;

public class Node<T>
{
    public T Value { get; set; }
    public Node<T> Next { get; set; }
}

public class LinkedList<T>
{
    . . . No Change

    public void AddLast(Node<T> item)
    {
        // Get a reference to the new item that will be added
        // and assign the argument to that reference
        Node<T> current = item;

        // Indicate that the first node will become next to the new item
        current.Next = begin;
        // Indicate that the new item becomes the first of the collection
        begin = current;

        // Increase the count of items
        size++;
    }
}
Using the Last Item

In some cases, you may want to get the last item of a collection. Here is an example of how to access it:
using System;

public class Node<T>
{
    public T Value { get; set; }
    public Node<T> Next { get; set; }

    public Node<T> Nullify()
    {
        return default(Node<T>);
    }
}

public class LinkedList<T>
{
    private Node<T> begin;
    private Node<T> end;
    private int size;

    public LinkedList()
    {
        begin = null;
        end = null;
        size = 0;
    }

    public int Count
    {
        get { return size; }
    }

    public Node<T> First
    {
        get
        {
            // If the list has no item, throw an exception
            if( begin == null ) 
                throw new NullReferenceException(
                 "There is no item in the collection");
            else
            {
                // This variable will be used for looping
                Node<T> temp = null;
                // Get a reference to the first item of the collection
                Node<T> current = begin;

                // Loop through the collection, from one item to the next.
                // This allows you to get to the first item
                while (current != null)
                {
                    temp = current;
                    current = current.Next;
                }

                // Return that first item
                return temp;
            }
        }
    }

    public Node<T> Last
    {
        get
        {
            return begin;
        }
    }

    public void AddLast(Node<T> item)
    {
        // Get a reference to the new item that will be added
        // and assign the argument to that reference
        Node<T> current = item;

        // Indicate that the first node will become next to the new item
        current.Next = begin;
        // Indicate that the new item becomes the first of the collection
        begin = current;

        // Increase the count of items
        size++;
    }

    public Node<T> this[int index]
    {
        get
        {
            if( (index == 0) && (begin == null) )
                throw new NullReferenceException(
                 "There is no item in the collection");
            else
            {
                Node<T> current = begin;

                for (int i = size - 1; (i > index) && (current != null); i--)
                    current = current.Next;
                return current;
            }
        }
    }
}

public class Exercise
{
    static int Main(string[] args)
    {
        Node<string> name = null;
        LinkedList<string> employees = new LinkedList<string>();

        name = new Node<string>();
        name.Value = "Alan Baxter";
        employees.AddLast(name);

        name = new Node<string>();
        name.Value = "Joan Bramble";
        employees.AddLast(name);

        name = new Node<string>();
        name.Value = "Paul Bertrand Yamaguchi";
        employees.AddLast(name);

        name = new Node<string>();
        name.Value = "Lionel Berg";
        employees.AddLast(name);

        name = new Node<string>();
        name.Value = "Flora Bruze";
        employees.AddLast(name);

        name = new Node<string>();
        name.Value = "Christopher Fox";
        employees.AddLast(name);

        name = new Node<string>();
        name.Value = "Holly Madsen";
        employees.AddLast(name);

        for (int i = 0; i < employees.Count; i++)
            Console.WriteLine("Name {0}: {1}", i + 1, employees[i].Value);

        Console.WriteLine("----------------------------------");
        Console.WriteLine("First Item: {0}", employees.First.Value);
        Console.WriteLine("Last Item:  {0}", employees.Last.Value);
        Console.WriteLine("----------------------------------");

        return 0;
    }
}
This would produce:
Name 1: Alan Baxter
Name 2: Joan Bramble
Name 3: Paul Bertrand Yamaguchi
Name 4: Lionel Berg
Name 5: Flora Bruze
Name 6: Christopher Fox
Name 7: Holly Madsen
----------------------------------
First Item: Alan Baxter
Last Item:  Holly Madsen
----------------------------------
Press any key to continue . . .
. Here is an example:
public
Inserting an Item Using an Index

As you should know already, inserting an item consists of putting one between two existing items. One way you can do this consists of passing the new item and an index. When implementing the method, first identify the index. If the user passes an index of 0, this is equivalent to adding the first item. If the user passes an index that is equal to the size of the list, add the item as the last. If the user passes an index that is larger than the size of the list, you can either throw an exception or add the item as the last. If the user passes an index that is positive but less than the total size of the list:
  1. Get a reference to the item passed as argument
  2. Get the item located at the specified index
  3. Get the item next to the one at the index and assign it to the item next to the argument
  4. Assign the argument to the item next to the one at the index
  5. Increase the number of items
Here is an example:
public class Node<T>
{
    public T Value { get; set; }
    public Node<T> Next { get; set; }
}

public class LinkedList<T>
{
    private Node<T> First;
    private Node<T> end;
    private int size;

    . . . No Change

    public void Insert(Node<T> item, int index)
    {
        if (index == 0)
            AddFirst(item);
        else if (index >= size)
            AddLast(item);
        else
        {
            Node<T> current = item;

            Node<T> itemAtIndex = this[index];
            current.Next = itemAtIndex.Next;
            itemAtIndex.Next = current;
            
            size++;
        }
    }
}
Here is an example of testing the method:
public class Exercise
{
    static int Main(string[] args)
    {
        Node<string> name = null;
        LinkedList<string> employees = new LinkedList<string>();

        name = new Node<string>();
        name.Value = "Mathew Jeremy Williams";
        employees.Insert(name, 3);

        name = new Node<string>();
        name.Value = "Alan Baxter";
        employees.AddLast(name);

        name = new Node<string>();
        name.Value = "Joan Bramble";
        employees.AddLast(name);

        name = new Node<string>();
        name.Value = "Lionel Berg";
        employees.AddLast(name);

        name = new Node<string>();
        name.Value = "Paul Bertrand Yamaguchi";
        employees.AddLast(name);

        name = new Node<string>();
        name.Value = "Flora Bruze";
        employees.AddLast(name);

        name = new Node<string>();
        name.Value = "Christopher Fox";
        employees.AddLast(name);

        name = new Node<string>();
        name.Value = "Holly Madsen";
        employees.AddLast(name);

        for (int i = 0; i < employees.Count; i++)
            Console.WriteLine("Name {0}: {1}", i + 1, employees[i].Value);

        return 0;
    }
}
This would produce:
Name 1: Mathew Jeremy Williams
Name 2: Alan Baxter
Name 3: Joan Bramble
Name 4: Lionel Berg
Name 5: Paul Bertrand Yamaguchi
Name 6: Flora Bruze
Name 7: Christopher Fox
Name 8: Holly Madsen
Press any key to continue . . .
Here is another test:
public class Exercise
{
    static int Main(string[] args)
    {
        Node<string> name = null;
        LinkedList<string> employees = new LinkedList<string>();

        name = new Node<string>();
        name.Value = "Alan Baxter";
        employees.AddLast(name);

        name = new Node<string>();
        name.Value = "Joan Bramble";
        employees.AddLast(name);

        name = new Node<string>();
        name.Value = "Mathew Jeremy Williams";
        employees.Insert(name, 3);

        name = new Node<string>();
        name.Value = "Lionel Berg";
        employees.AddLast(name);

        name = new Node<string>();
        name.Value = "Paul Bertrand Yamaguchi";
        employees.AddLast(name);

        name = new Node<string>();
        name.Value = "Flora Bruze";
        employees.AddLast(name);

        name = new Node<string>();
        name.Value = "Christopher Fox";
        employees.AddLast(name);

        name = new Node<string>();
        name.Value = "Holly Madsen";
        employees.AddLast(name);

        for (int i = 0; i < employees.Count; i++)
            Console.WriteLine("Name {0}: {1}", i + 1, employees[i].Value);

        return 0;
    }
}
This would produce:
Name 1: Alan Baxter
Name 2: Joan Bramble
Name 3: Mathew Jeremy Williams
Name 4: Lionel Berg
Name 5: Paul Bertrand Yamaguchi
Name 6: Flora Bruze
Name 7: Christopher Fox
Name 8: Holly Madsen
Press any key to continue . . .
Here is one more test:
public class Exercise
{
    static int Main(string[] args)
    {
        Node<string> name = null;
        LinkedList<string> employees = new LinkedList<string>();

        name = new Node<string>();
        name.Value = "Alan Baxter";
        employees.AddLast(name);

        name = new Node<string>();
        name.Value = "Joan Bramble";
        employees.AddLast(name);

        name = new Node<string>();
        name.Value = "Lionel Berg";
        employees.AddLast(name);

        name = new Node<string>();
        name.Value = "Paul Bertrand Yamaguchi";
        employees.AddLast(name);

        name = new Node<string>();
        name.Value = "Flora Bruze";
        employees.AddLast(name);

        name = new Node<string>();
        name.Value = "Christopher Fox";
        employees.AddLast(name);

        name = new Node<string>();
        name.Value = "Mathew Jeremy Williams";
        employees.Insert(name, 3);

        name = new Node<string>();
        name.Value = "Holly Madsen";
        employees.AddLast(name);

        for (int i = 0; i < employees.Count; i++)
            Console.WriteLine("Name {0}: {1}", i + 1, employees[i].Value);

        return 0;
    }
}
This would produce:
Name 1: Alan Baxter
Name 2: Joan Bramble
Name 3: Lionel Berg
Name 4: Mathew Jeremy Williams
Name 5: Paul Bertrand Yamaguchi
Name 6: Flora Bruze
Name 7: Christopher Fox
Name 8: Holly Madsen
Press any key to continue . . .
Deleting an Item

 
Deleting the First Item

To delete the first item of a linked list, get a reference to the next item of the starting positio, assign that reference to the next item to the first. Here is an example:
public void DeleteFirst()
{
        if (begin == null)
            return;

        Node<T> current = begin.Next;

        begin.Next = current;
        size--;
}
Deleting the Last Item

This would produce:
Number of Items: 0
Press any key to continue . . .

C# Collections: Introduction to Collections

Introduction to c# Collections


Array-Based Lists

 
Introduction





A collection, also called a list, is many items of the same kind grouped into one entity. The collection can be based on simple numbers, basic symbols, dates, strings, times. The collection can also be made of objects that each is made of internal values. This means that a collection can be made of such objects as houses, people, cars, countries, electronic devices.

The primary rule to observe is that all items included in a list must be described using the same characteristics. Based on this, if the collection is made of cars, it should contain only cars, not cars and countries.

There are various types of lists or collections. An array-based list is one whose number of items is known in advance. For example, an array-based collection of 10 cars contains 10 cars, may-be less but not more (in reality, the Array class of the .NET Framework provides a method used to expand an array).

Setting Up a Collection


To use a collection as an entity, you can create a class for it. This class would be used to add items in the collection, to remove items in the list, or to perform other necessary operations. You can start a simple class as follows:

class Collection
{
    public Collection()
    {
    }
}

After creating the class for a collection, the collection becomes an object and its class can be used like any other. This means that, before using the collection, you can declare a variable for it. Here is an example:

using System;

class Collection
{
    public Collection()
    {
    }
}

public class Exercise
{
    static int Main(string[] args)
    {
        var list = new Collection();

        return 0;
    }
}

The Number of Items in a Collection


For an array-based list, because you must specify the number of items that the list will contain, you can declare an array as a field. Here is an example:

class Collection
{
    // This collection will be a list of decimal numbers
    private double[] Item = new double[20];

    // Our default constructor, used to initialize the collection
    public Collection()
    {
    }
}

Although you can initialize an array in a C# class, remember that you can also use a constructor to initialize it. Here is an example:

class Collection
{
    public int MaxCount = 20;

    // This collection will be a list of decimal numbers
    private double[] Item;

    #region This section is used to set up the collection
    // Our default constructor, used to initialize the collection
    public Collection()
    {
        this.Item = new double[MaxCount];
    }
    #endregion
}

A primary accessory you need when using a list is a count of the number of items in the list when/as they are added or deleted. This accessory is primarily a private field as a simple natural number. When the class is declared, this member variable should be set to 0 to indicate that the list is primarily empty:

class Collection
{
    public int MaxCount = 20;

    // This collection will be a list of decimal numbers
    private double[] Item;
    // This is the size of the collection
    private int size;

    #region This section is used to set up the collection
    // Our default constructor, used to initialize the collection
    public Collection()
    {
        this.Item = new double[MaxCount];
 this.size = 0;
    }
    #endregion
}

Since the size field was declared private, if you plan to get the count of items in the list from outside the class, you should (must) provide a property to take care of this. This can simply be done as follows:

using System;

class Collection
{
    public int MaxCount = 20;

    // This collection will be a list of decimal numbers
    private double[] Item;
    // This is the size of the collection
    private int size;

    #region This section is used to set up the collection
    // Our default constructor, used to initialize the collection
    public Collection()
    {
        this.Item = new double[MaxCount];
        this.size = 0;
    }

    // This represents the number of items in the collection
    public int Count
    {
        get { return this.size; }
    }
    #endregion
}

public class Exercise
{
    static int Main(string[] args)
    {
        var list = new Collection();

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

This would produce:

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

Routine Operations on an Array-Based List

 
Adding a Value to a Collection


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, because an array has (or is supposed to have) a fixed number of items, you should 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 the maximum number of items it can hold. If the collection can still receive at least one item, you can add the item at the end of the list and increase the count by one. Here is how this can be done:

class Collection
{
    public int MaxCount = 20;

    // This collection will be a list of decimal numbers
    private double[] Item;
    // This is the size of the collection
    private int size;

    #region This section is used to set up the collection
    // Our default constructor, used to initialize the collection
    public Collection()
    {
        this.Item = new double[MaxCount];
        this.size = 0;
    }

    // This represents the number of items in the collection
    public int Count
    {
        get { return this.size; }
    }
    #endregion

    #region Operations on the collection
    // 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
    public bool Add(double item)
    {
        // Make sure the list is not yet full
        if (this.size < 20)
        {
            // Since the list is not full, add the item at the end
            this.Item[this.size] = item;
            // Increase the count and return the new position
            this.size++;

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

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

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

public class Exercise
{
    static int Main(string[] args)
    {
        var list = new Collection();

        Console.WriteLine("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("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 Collection


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 System;

class Collection
{
    public int MaxCount = 20;

    // This collection will be a list of decimal numbers
    private double[] Item;
    // This is the size of the collection
    private int size;

    #region This section is used to set up the collection
    // Our default constructor, used to initialize the collection
    public Collection()
    {
        this.Item = new double[MaxCount];
        this.size = 0;
    }

    // This represents the number of items in the collection
    public int Count
    {
        get { return this.size; }
    }
    #endregion

    #region Operations on the collection
    // 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
    public bool Add(double item)
    {
        // Make sure the list is not yet full
        if (size < MaxCount)
        {
            // Since the list is not full, add the "item" at the end
            this.Item[this.size] = item;
            // Increase the count and return the new position
            this.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
    public double 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;
    }
    #endregion
}

public class Exercise
{
    static int Main(string[] args)
    {
        var list = new Collection();

        Console.WriteLine("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);

        for (int i = 0; i < list.Count; i++)
            Console.WriteLine("Item {0}: {1}\n", i + 1, list.Retrieve(i));
        Console.WriteLine("Number 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 Collection


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 because if it is, then there is no way to insert an item (you insert something between two things, not something between nothing). Second, the specified position must be in the allowed range.

The method can be implemented as follows:

using System;

class Collection
{
    public int MaxCount = 20;

    // This collection will be a list of decimal numbers
    private double[] Item;
    // This is the size of the collection
    private int size;

    #region This section is used to set up the collection
    // Our default constructor, used to initialize the collection
    public Collection()
    {
        this.Item = new double[MaxCount];
        this.size = 0;
    }

    // This represents the number of items in the collection
    public int Count
    {
        get { return this.size; }
    }
    #endregion

    #region Operations on the collection
    // 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
    public bool Add(double item)
    {
        // Make sure the list is not yet full
        if (size < MaxCount)
        {
            // Since the list is not full, add the "item" at the end
            this.Item[this.size] = item;
            // Increase the count and return the new position
            this.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
    public double 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
    // Insert a new item at a specified position in the list.
    // After the new item is inserted, the count is increased
    public bool 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
            this.Item[pos] = itm;

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

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

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

public class Exercise
{
    static int Main(string[] args)
    {
        var list = new Collection();

        Console.WriteLine("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);


        for (var i = 0; i < list.Count; i++)
            Console.WriteLine("Item {0}: {1}", i + 1, list.Retrieve(i));
        Console.WriteLine("Number of Items: {0}\n", list.Count);

        list.Insert(-707.16, 2);

        for (var i = 0; i < list.Count; i++)
            Console.WriteLine("Item {0}: {1}", i + 1, list.Retrieve(i));
        Console.WriteLine("Number 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 (should/must) first check that the specified position is valid. The method to perform this operation can be implemented as follows:

using System;

class Collection
{
    public int MaxCount = 20;

    // This collection will be a list of decimal numbers
    private double[] Item;
    // This is the size of the collection
    private int size;

    #region This section is used to set up the collection
    // Our default constructor, used to initialize the collection
    public Collection()
    {
        this.Item = new double[MaxCount];
        this.size = 0;
    }

    // This represents the number of items in the collection
    public int Count
    {
        get { return this.size; }
    }
    #endregion

    #region Operations on the collection
    // 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
    public bool Add(double item)
    {
        // Make sure the list is not yet full
        if (size < MaxCount)
        {
            // Since the list is not full, add the "item" at the end
            this.Item[this.size] = item;
            // Increase the count and return the new position
            this.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
    public double 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
    public bool 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
            this.Item[pos] = itm;

            // Since we have added a new item, increase the count
            this.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
    //-- Delete the item at that position and decrease the count --//
    public bool 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;
    }
    #endregion
}

public class Exercise
{
    static int Main(string[] args)
    {
        var list = new Collection();

        Console.WriteLine("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);


        for (var i = 0; i < list.Count; i++)
            Console.WriteLine("Item {0}: {1}", i + 1, list.Retrieve(i));
        Console.WriteLine("Number of Items: {0}\n", list.Count);

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

        for (var i = 0; i < list.Count; i++)
            Console.WriteLine("Item {0}: {1}", i + 1, list.Retrieve(i));
        Console.WriteLine("Number of Items: {0}\n", list.Count);

        list.Delete(5);
        list.Delete(3);

        for (var i = 0; i < list.Count; i++)
            Console.WriteLine("Item {0}: {1}", i + 1, list.Retrieve(i));
        Console.WriteLine("Number 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 . . .

A Collection of Items

 
Introduction


We have learned how to create an array as a list of items. Like an array, a collection is a series of items of the same type. The particularity with creating an array is that you must know in advance the number of items that will make up the list (in reality, in the .NET Framework, you just have to specify an initial count, such as 5; then, before adding a new item, you can check if there is room, if there is no room, you can provide room first, then add the new item). There are times when you don't know, you can't know, or you can't predict the number of items of the list. For this reason, you may want to create the list for which you don't specify the maximum number of items but you allow the user of the list to add, locate, or remove items at will.

Before creating a list, you probably should first decide or define what the list would be made of. As different as collections are, one list can be made of numeric values, such as a list that will be made of numbers. You may want a list that is made of names. Such a list can be created from a class that includes a string member variable. Another type of list can contain complex objects.

Implementing a Collection


After deciding what each item of the list would be made of, you can create a class that would manage the list. This class would be responsible for all operations that can be performed on the list. If the list will be made of primitive values, you can directly create a field of the desired type. Here is an example:

using System;

public class Numbers
{
    public double Number;
}

public class Exercise
{
    static int Main(string[] args)
    {
        Numbers nbrs = new Numbers();

        return 0;
    }
}

If the list will be made of objects, you can first create a class that specifies those types of items and then declare its variable in the list class. Here is an example of a simple class that holds a double-precision value:

using System;

public class Number
{
    public double Item;
}

public class Numbers
{
    Number Sample;
}

public class Exercise
{
    static int Main(string[] args)
    {
        Numbers nbrs = new Numbers();

        return 0;
    }
}

When creating a list, one of the aspects you should pay attention to is to keep track of the number of items in the list. To do this, you can create a property that holds the number. The value of this property would increase as the items are added to the list and the value would decrease as the items are removed from the list. Here is how this can be done:

using System;

public class Number
{
    public double Item;
}

public class Numbers
{
    int size;
    Number Sample;

    public Numbers()
    {
        size = 0;
    }

    public int Count
    {
        get { return size; }
    }
}

public class Exercise
{
    static int Main(string[] args)
    {
        var nbrs = new Numbers();

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

        return 0;
    }
}

This would produce:

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

The Beginning of a Collection


A good collection is a list that can grow or shrink as the user wishes. When creating the list, you don't need to predict the maximum number of items that will be added to the list. When a list starts, it is empty or at least it must be considered like that, before any item is added to it. To specify this, you should declare a primary member variable. Although you can call it anything, it is usually called Head:

Collection

The head member can be made private if you don't intend to access it outside of the class. If you want clients of the class to access it, you can make it public. Here is an example:

public class Numbers
{
    int listSize;
    Number Sample;

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

    public int Count
    {
        get { return listSize; }
    }

    public Number Head;
}

Linking the Items of a Collection


We saw that when using an array, each member could be accessed using its index. If the items of a collection are not indexed, you must provide a mechanism to locate an item. To do this, you can use a starting point that determines the beginning of the list. Then, to locate an item, you can check whether another item follows that starting point:

Collection

If no item follows an item, either you are at the end of the list or the list is empty.

To be able to scan a list from one item to another, you can declare a field. Although this member variable can be called anything, for the sake of clarify, you should call it Next. The field is the same type as its class. Here is an example:

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

Operations on a Collection

 
Adding an Item


Since a list is fundamentally empty when it starts, the primary operation you can perform on a list is to add a new item to it. 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++;
    }
}

Retrieving an Item


Once a list exists, the user can explore it. One of the operations performed on a collection is to locate and retrieve an item. 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;
    }
}

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 routine 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.

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 (exact) 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 Exercise
{
    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 . . .