public bool Remove(object key)
        {
            int index      = Hash(key);
            var linkedList = hashArray[index];

            if (linkedList == null)
            {
                return(false);
            }

            CustomLinkedListNode <CustomHashTableItem> previousNode = null;
            CustomLinkedListNode <CustomHashTableItem> currentNode  = linkedList.First;

            while (currentNode != null)
            {
                if (currentNode.Value.Key.Equals(key))
                {
                    linkedList.Remove(currentNode.Value);
                    return(true);
                }
                previousNode = currentNode;
                currentNode  = currentNode.Next;
            }

            return(false);
        }
        private CustomLinkedListNode <CustomHashTableItem> GetPreviousLinkedListItem(object key)
        {
            int index = Hash(key);
            CustomHashTableItem currentItem;
            var linkedList = hashArray[index];

            if (linkedList == null)
            {
                return(null);
            }

            if (linkedList.Length < 2)
            {
                return(null);
            }

            CustomLinkedListNode <CustomHashTableItem> previousNode = null;
            CustomLinkedListNode <CustomHashTableItem> currentNode  = linkedList.First;

            bool isExit = false;

            while (!isExit)
            {
                if (currentNode.Value.Key.Equals(key))
                {
                    isExit = true;
                }
            }

            return(null);
        }
Beispiel #3
0
        public void AddAt(T value, int index)
        {
            if (index < 0 || (index + 1) > Length)
            {
                throw new ArgumentException("Incorrect index " + index);
            }

            var newNode = new CustomLinkedListNode <T>(value);

            if (index == 0)
            {
                newNode.Next = First;
                First        = newNode;
                Length++;
                return;
            }

            if (index == (Length - 1))
            {
                Last.Next = newNode;
                Length++;
                return;
            }

            var previousNode = First;

            for (int i = 1; i < index; i++)
            {
                previousNode = previousNode.Next;
            }

            newNode.Next      = previousNode.Next;
            previousNode.Next = newNode;
            Length++;
        }
Beispiel #4
0
        IEnumerator IEnumerable.GetEnumerator()
        {
            CustomLinkedListNode <T> currentNode = First;

            while (currentNode != null)
            {
                yield return(currentNode.Value);

                currentNode = currentNode.Next;
            }
        }
Beispiel #5
0
        public bool Remove(T value)
        {
            bool result = false;

            if (Length == 0)
            {
                return(result);
            }

            if (Length == 1)
            {
                First = null;
                Length--;
                return(true);
            }

            CustomLinkedListNode <T> previousNode = null;
            CustomLinkedListNode <T> currentNode  = First;
            bool isExit = false;

            while (currentNode != null && !isExit)
            {
                if (currentNode.Value.Equals(value))
                {
                    if (previousNode != null)
                    {
                        previousNode.Next = currentNode.Next;
                        if (currentNode.Next == null)
                        {
                            Last = previousNode;
                        }
                    }
                    else
                    {
                        First = First.Next;
                        if (First.Next == null)
                        {
                            Last = First;
                        }
                    }
                    result = true;
                    Length--;
                    isExit = true;
                }

                previousNode = currentNode;
                currentNode  = currentNode.Next;
            }

            return(result);
        }
Beispiel #6
0
        public void Add(T value)
        {
            var listNode = new CustomLinkedListNode <T>(value);

            if (First == null)
            {
                First  = listNode;
                Last   = listNode;
                Length = 1;
            }
            else  //add to end of list
            {
                Last.Next = listNode;
                Last      = listNode;
                Length++;
            }
        }