Example #1
0
        public void Add(int elem)
        {
            MyListItem <int> newElem = new MyListItem <int>(elem);

            if (startElem == null)
            {
                startElem = newElem;
            }
            else
            {
                GetItem(Count - 1).nextElem = newElem;
            }
        }
Example #2
0
        public MyListItem <int> GetItem(int index)
        {
            if (Count == 0 || index >= Count)
            {
                throw new ArgumentOutOfRangeException();
            }

            MyListItem <int> item = startElem;

            for (int i = 0; i < index; i++)
            {
                item = item.nextElem;
            }

            return(item);
        }
Example #3
0
 public void RemoveAt(int index)
 {
     if (index == 0)
     {
         if (startElem.nextElem == null)
         {
             startElem = null;
         }
         else
         {
             startElem = startElem.nextElem;
         }
     }
     else if (index == Count - 1)
     {
         GetItem(index - 1).nextElem = null;
     }
     else
     {
         GetItem(index - 1).nextElem = GetItem(index + 1);
     }
 }
Example #4
0
        public bool Remove(int elem)
        {
            if (Count == 0)
            {
                return(false);
            }

            for (int i = 0; i < Count; i++)
            {
                if (elem.Equals(this[i]))
                {
                    if (i == 0)
                    {
                        if (startElem.nextElem == null)
                        {
                            startElem = null;
                        }
                        else
                        {
                            startElem = startElem.nextElem;
                        }
                    }
                    else if (i == Count - 1)
                    {
                        GetItem(i - 1).nextElem = null;
                    }
                    else
                    {
                        GetItem(i - 1).nextElem = GetItem(i + 1);
                    }

                    return(true);
                }
            }

            return(false);
        }