Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="intList"></param>
        /// <exception cref="ArgumentNullException">Thrown if intList is null or empty.</exception>
        public SinglyLinkedList(IEnumerable <int> intList)
        {
            if ((intList == null) || (!intList.Any()))
            {
                throw new ArgumentNullException("intList", "intList cannot be null or empty.");
            }


            SinglyLinkedNode current = null;

            Length = 0;
            foreach (var item in intList)
            {
                if (Head == null)
                {
                    Head    = new SinglyLinkedNode(item);
                    current = Head;
                }
                else
                {
                    current.Next = new SinglyLinkedNode(item);
                    current      = current.Next;
                }

                Length++;
            }

            Tail = current;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Return node at the 1 based index.
        /// </summary>
        /// <param name="index">1 based index.</param>
        /// <returns>Node at index, or Null if index exceed the length.</returns>
        /// <exception cref="ArgumentOutOfRangeException">Thrown if index is less than 1.</exception>
        public static SinglyLinkedNode GetNode(this SinglyLinkedList list, int index)
        {
            if (index < 1)
            {
                throw new ArgumentOutOfRangeException("index", "Index must be greater than 0.");
            }

            SinglyLinkedNode current = list.Head;
            int i = 1;

            while ((current != null) && (i < index))
            {
                current = current.Next;
                i++;
            }

            return(current);
        }
Exemplo n.º 3
0
 public SinglyLinkedList(int firstInt)
 {
     Head   = new SinglyLinkedNode(firstInt);
     Tail   = Head;
     Length = 1;
 }