/// <summary> /// Add the given Link into the list /// </summary> /// <param name="Link">The Link to add to the list</param> /// <param name="index">The index at which to insert this entity. Leave -1 to push to the end of the list</param> /// <exception cref="ArgumentException">The Link already has a parent list</exception> public void Add(GDDisplayLinkedListLink Link, int index = -1) { if (Link.ParentList != null) { throw new Exception("Link was already in a list"); } if (index > size) { throw new ArgumentException("Index is out of length of the list", "index"); } Link.ParentList = this; // Case: List is empty, set the link as the whole list if (List == null) { List = Link; Last = Link; } else { GDDisplayLinkedListLink link = null; if (index > 0 && index < size) { link = List.GetNextLink(index); } if (link != null) { if (link != Last) { link.Next.Previous = Link; } else { Last = Link; } Link.Next = link.Next; Link.Previous = link; link.Next = Link; } else if (index == 0) { List.Previous = Link; Link.Next = List; List = Link; } else { link = Last; Last = Link; Link.Next = link.Next; Link.Previous = link; link.Next = Link; } } size++; }