2
\$\begingroup\$
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
class List<T>
{
    private T[] arrayList;
    private int arrayLenght = 5;
    private int arrayIndex = 0;


    public List()
    {
        arrayList = new T[arrayLenght];
    }

    internal string[] toArray()
    {
        string[] arrayListArray = new string[arrayLenght];
        try
        {

            for (int j = 0; j < arrayList.Length; j++)
            {
                if (arrayList[j] == null)
                {
                    arrayListArray[j] = "";
                }
                else
                {
                    arrayListArray[j] = arrayList[j].ToString();
                }

            }
        }
        catch(Exception Ex)
        {
            Console.WriteLine(Ex);
        }
        return arrayListArray;
    }

    private void ArrayOverFlow(T genericParameter)
    {
        try
        {
            arrayLenght = 2 * arrayLenght;
            T[] arrayListNewSize = new T[arrayLenght];
            int arrayListNewSizeIndex = 0;
            Array.Copy(arrayList, arrayListNewSize, arrayList.Length);
            arrayList = new T[arrayLenght];
            Array.Copy(arrayListNewSize, arrayList, arrayListNewSize.Length);
            arrayListNewSize = null;
            arrayList[arrayIndex] = genericParameter;
            arrayIndex++;
        }
        catch (Exception Ex)
        {
            Console.WriteLine(Ex);
        }
    }

    internal void Add(T genericParameter)
    {
        // Console.WriteLine("Parameter type: {0}, value: {1}", typeof(T).ToString(), genericParameter);  
        try
        {
            if (arrayIndex + 1 <= arrayLenght)
            {
                arrayList[arrayIndex] = genericParameter;
                arrayIndex++;
            }
            else
            {
                ArrayOverFlow(genericParameter);
            }
        }
        catch (Exception Ex)
        {
            Console.WriteLine(Ex);
        }
    }

    internal void Insert(int index, T genericParameter)
    {
        try
        {
            arrayList[index] = genericParameter;
        }
        catch (Exception Ex)
        {
            Console.WriteLine(Ex);
        }
    }

    private void RemoveArray(int index)
    {
        try
        {
            for (int i = index; i < arrayList.Length; i++)
            {
                if (i < arrayList.Length - 1)
                {
                    arrayList[i] = arrayList[i + 1];
                }
            }
        }
        catch (Exception Ex) {
            Console.WriteLine(Ex);
        }
    }

    internal void RemoveAt(int index)
    {
        RemoveArray(index);
    }

    internal void Remove(T genericParameter)
    {
        try
        {
            for (int i = 0; i < arrayList.Length; i++)
            {
                if (arrayList[i] != null)
                {
                    if (arrayList[i].Equals(genericParameter))
                    {
                        RemoveArray(i);
                    }
                }
            }
        }
        catch (Exception Ex)
        {
            Console.WriteLine(Ex);
        }
    }
   }
 }

I have also written test cases for the same

using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
[TestFixture]
class Test1
{
    [TestCase]
    public void TestAdd1()
    {
        List<int> st = new List<int>();
        st.Add(10);
        st.Add(20);
        st.Add(30);
        st.Add(40);
        st.Add(50);
        st.Add(60);

        Assert.AreEqual("10,20,30,40,50,60,0,0,0,0", string.Join(",", st.toArray()));
    }
    [TestCase]
    public void TestAdd2()
    {
        List<string> st = new List<string>();
        st.Add("10");
        st.Add("20");
        st.Add("30");
        st.Add("40");
        st.Add("50");
        st.Add("60");
        Assert.AreEqual("10,20,30,40,50,60,,,,", string.Join(",", st.toArray()));
    }

    [TestCase]
    public void TestAdd3()
    {
        List<int> st = new List<int>();
        st.Add(10);
        st.Add(20);
        st.Add(30);
        st.Add(40);
        st.Add(50);
        st.Add(60);
        st.Insert(2, 70);
        Assert.AreEqual("10,20,70,40,50,60,0,0,0,0", string.Join(",", st.toArray()));
    }

    [TestCase]
    public void TestAdd4()
    {
        List<string> st = new List<string>();
        st.Add("10");
        st.Add("20");
        st.Add("30");
        st.Add("40");
        st.Add("50");
        st.Add("60");
        st.Insert(2, "70");
        Assert.AreEqual("10,20,70,40,50,60,,,,", string.Join(",", st.toArray()));
    }

    [TestCase]
    public void TestAdd5()
    {
        List<int> st = new List<int>();
        st.Add(10);
        st.Add(20);
        st.Add(30);
        st.Add(40);
        st.Add(50);
        st.Add(60);
        st.RemoveAt(2);
        Assert.AreEqual("10,20,40,50,60,0,0,0,0,0", string.Join(",", st.toArray()));
    }


    [TestCase]
    public void TestAdd6()
    {
        List<string> st = new List<string>();
        st.Add("10");
        st.Add("20");
        st.Add("30");
        st.Add("40");
        st.Add("50");
        st.Add("60");
        st.RemoveAt(2);
        Assert.AreEqual("10,20,40,50,60,,,,,", string.Join(",", st.toArray()));
    }
    [TestCase]
    public void TestAdd7()
    {
        List<int> st = new List<int>();
        st.Add(10);
        st.Add(20);
        st.Add(30);
        st.Add(40);
        st.Add(50);
        st.Add(60);
        st.Remove(30);
        Assert.AreEqual("10,20,40,50,60,0,0,0,0,0", string.Join(",", st.toArray()));
    }

    [TestCase]
    public void TestAdd8()
    {
        List<string> st = new List<string>();
        st.Add("10");
        st.Add("20");
        st.Add("30");
        st.Add("40");
        st.Add("50");
        st.Add("60");
        st.Remove("30");
        Assert.AreEqual("10,20,40,50,60,,,,,", string.Join(",", st.toArray()));

       }
     }
   }

I am learning data structure so I tried to implement a list for better understanding so any feedback and code review would be helpful.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Does the current code work as intended? \$\endgroup\$
    – Mast
    Mar 23, 2020 at 18:28
  • \$\begingroup\$ At least the sequence of letters in arrayLenght looks consistent. Who is to judge works as intended? No intention is documented. Insert(i, T) looks weird; RemoveArray(i) a reference hog. \$\endgroup\$
    – greybeard
    Mar 23, 2020 at 21:29
  • \$\begingroup\$ the above code works as expected so can you tell me what did look weird in insert in Insert \$\endgroup\$ Mar 30, 2020 at 5:35
  • \$\begingroup\$ Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers. \$\endgroup\$
    – Mast
    Mar 30, 2020 at 6:16

1 Answer 1

2
\$\begingroup\$

Your Insert is wrong, you have to do it the same way as the Remove with shifting Elements. ArrayIndex is not increased to decreased on Remove/Insert.

Better don't call any parameter a parameter or add the DataType to it. Name as you speak. You insert an 'item' or an 'element' to your list. Not a parameter.

Always Doubling the array on overflow can lead to very huge arrays. Imagine you have a 1 Gigabyte Array and you want to add 1 element,you allocate 1 new gigabyte for that. It's better to increase by a fixed amount of items, like 16 or may 1024.

Why do you have to have two Array.Copy on reallocation ? Usual approach is to create a new array and copy existing elements.

var tmp = new T[newarraylength];
Array.Copy(arrayList, tmp, oldarraylength);
arrayList = tmp;

I don't get why you copy something twice.

\$\endgroup\$
2
  • \$\begingroup\$ I have edited my code as you said for array copy but as you said the insert code is wrong but .i have see code codereview.stackexchange.com/questions/144360/… which see the same as mine. \$\endgroup\$ Mar 30, 2020 at 5:43
  • \$\begingroup\$ No, the other code is fine. It does not have any Insert at all, and the place with two Array.Copy handles two sections of the same array. Sure I'm just assuming what insert means, for me it's adding an element in the middle. What you do is replacing an element. \$\endgroup\$
    – Holger
    Mar 31, 2020 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.