/// <summary> /// TODO. delete a element. /// </summary> /// <param name="element">TODO. 2.</param> /// <returns>TODO. 3.</returns> public bool DeleteElement(T element) { var currentElement = Head; SortedLinkedListNode <T>?previousElement = null; // iterates through all elements while (currentElement != null) { // checks if the element, which should get deleted is in this list element if ((currentElement.Data is null && element is null) || (currentElement.Data != null && currentElement.Data.Equals(element))) { // if element is head just take the next one as head if (currentElement.Equals(Head)) { Head = Head.Next; return(true); } // else take the prev one and overwrite the next with the one behind the deleted if (previousElement != null) { previousElement.Next = currentElement.Next; return(true); } } // iterating previousElement = currentElement; currentElement = currentElement.Next; } return(false); }
/// <summary> /// TODO. get the whole list. /// </summary> /// <returns>TODO.</returns> public IEnumerable <T> GetListData() { // temp ListElement to avoid overwriting the original SortedLinkedListNode <T>?tempElement = Head; // all elements where a next attribute exists while (tempElement != null) { yield return(tempElement.Data); tempElement = tempElement.Next; } }
/// <summary> /// Adds new node to the start of the list, /// time complexity: O(1), /// space complexity: O(1). /// </summary> /// <param name="data">Contents of newly added node.</param> /// <returns>Added list node.</returns> public SortedLinkedListNode <T> Add(T data) { if (Head == null) { Head = new SortedLinkedListNode <T>(data); Tail = Head; length = 1; } else { var next = Head.Data; // TODO: Get the place to insert the next element. } return(Head); }