示例#1
0
        public T Remove(int index)
        {
            var current = head;
            T   removed;

            if (index < 0 || index >= count)
            {
                throw new IndexOutOfRangeException("That index is not in the list");
            }
            else if (index == 0)
            {
                removed = head.Data;
                head    = head.next;
            }
            else if (index == (count - 1))
            {
                removed = tail.Data;
                for (int i = 0; i < count; i++)
                {
                    if (i == index - 1)
                    {
                        break;
                    }
                    else
                    {
                        current = current.next;
                    }
                }
                tail         = current;
                current.next = null;
            }
            else if (count == 1)
            {
                removed = head.Data;
                head    = null;
                tail    = null;
            }
            else
            {
                for (int i = 0; i < count; i++)
                {
                    if (i == index - 1)
                    {
                        break;
                    }
                    else
                    {
                        current = current.next;
                    }
                }
                removed      = current.next.Data;
                current.next = current.next.next;
            }
            count--;
            return(removed);
        }
示例#2
0
        public void Add(T data)
        {
            CustomLinkedNode <T> newNode = new CustomLinkedNode <T>()
            {
                Data = data
            };

            if (head == null)
            {
                head    = newNode;
                current = newNode;
                tail    = newNode;
                count   = 1;
            }
            else
            {
                tail.next = newNode;
                tail      = newNode;
                count++;
            }
        }
示例#3
0
        public string GetData(int index)
        {
            string returnValue = null;

            CustomLinkedNode current = Head;

            if ((index < 0) || (index >= Count))
            {
                throw new IndexOutOfRangeException();
            }
            else
            {
                for (int i = 0; i < index; i++)
                {
                    current = current.Next;
                }

                returnValue = current.Data;
            }

            return(returnValue);
        }
示例#4
0
        public void Add(string inputData)
        {
            if (Head == null)
            {
                Head = new CustomLinkedNode(inputData);

                Count++;
            }
            else
            {
                CustomLinkedNode current = Head;

                while (current.Next != null)
                {
                    current = current.Next;
                }

                current.Next = new CustomLinkedNode(inputData);

                Count++;
            }
        }
示例#5
0
        public T GetData(int index)
        {
            var current = head;

            if (index < 0 || index >= count)
            {
                throw new IndexOutOfRangeException("That index is not in the list");
            }
            else
            {
                for (int i = 0; i < count; i++)
                {
                    if (i == index)
                    {
                        break;
                    }
                    else
                    {
                        current = current.next;
                    }
                }
                return(current.Data);
            }
        }
示例#6
0
 public CustomLinkedNode(string inputData)
 {
     this.Data = inputData;
     this.Next = null;
 }