Wednesday, February 17, 2016

CS 2420 - Assignment 4 - Part 2

So I have now finished the first part of this assignment, and I have now moved on to creating a List class using the IList interface and Generics.

I am going to be honest here, I had no idea how to start this part of the assignment. At first I thought I was building another Linked List class, but after talking to a friend of mine he got me started on the right direction. He told me that I had to use an array and I had to manually keep track of the items in the array.

I can't just insert items into the array at any index, I have to keep it all together and I have to manage the size of the array as well or use any built in functions. Basically, this is going to be the hardest assignment I've done so far. Here is what I have so far:

using System;
using System.Collections;
using System.Collections.Generic;

class MyList : IList
{
    public T[] underlyingArray;
    private int element_count;

    public MyList(int initial_size)
    {
        this.underlyingArray = new T[initial_size];
        this.element_count = 0;
    }

    // TODO Implement: Indexer
    public T this[int index]
    {
        get
        {
            throw new NotImplementedException();
        }

        set
        {
            throw new NotImplementedException();
        }
    }

    public int Count
    {
        get
        {
            return this.element_count;
        }
    }

    public bool IsReadOnly
    {
        get
        {
            return this.underlyingArray.IsReadOnly;
        }
    }

    // TODO Handle Exception
    public void Add(T item)
    {
        if (element_count < this.underlyingArray.Length)
        {
            this.underlyingArray[element_count] = item;
            this.element_count++;
        }

        else
            throw new IndexOutOfRangeException();
    }

    public void Clear()
    {
        this.element_count = 0;
    }

    public bool Contains(T item)
    {
        for (int index = 0; index < Count; index++)
        {
            if (EqualityComparer.Default.Equals(underlyingArray[index], item));
                return true;
        }
        return false;
    }

    // TODO Implement: Copy
    public void CopyTo(T[] array, int arrayIndex)
    {
        throw new NotImplementedException();
    }

    // TODO Implement: Get Enumerator
    public IEnumerator GetEnumerator()
    {
        throw new NotImplementedException();
    }

    // TODO Implement: Index of
    public int IndexOf(T item)
    {
        throw new NotImplementedException();
    }

    // TODO Implement: Insert
    public void Insert(int index, T item)
    {
        throw new NotImplementedException();
    }

    // TODO Implement: Remove
    public bool Remove(T item)
    {
        throw new NotImplementedException();
    }

    // TODO Implement: Remove at
    public void RemoveAt(int index)
    {
        throw new NotImplementedException();
    }

    // TODO Implement: Get IEnumerable
    IEnumerator IEnumerable.GetEnumerator()
    {
        throw new NotImplementedException();
    }
}

So far I have only worked on a couple of functions, most importantly the Add function. This function will append an item to the end of the list, but I still haven't figured out how to manage the size of the array on my own.

That's all for now, thanks for reading!

No comments:

Post a Comment