/// <summary> /// Insertion Beginning of list. /// </summary> /// <param name="node"></param> public void InsertBeginningOfaList(CircularLinkedListNode <T> node) { //For Empty List. if (Last == null) { InsertionInAnEmptyList(node); return; } node.Link = Last.Link; Last.Link = node; }
/// <summary> /// Insertion a node before the node . /// </summary> /// <param name="searchedNode"></param> /// <param name="newNode"></param> public void InsertionBeforeANode(T searchedNode, T newNode) { var result = GetPredecessorNode(searchedNode); if (result == null) { return; } if (result.Equals(Last)) { InsertEndOfaList(new CircularLinkedListNode <T>(newNode)); return; } var temp = new CircularLinkedListNode <T>(newNode) { Link = result.Link }; result.Link = temp; }
/// <summary> /// Delete The last Node. /// </summary> public void DeleteLastNode() { //Empty list check. if (Last == null) { return; } //if list contains only one node . if (Last.Link.Equals(Last)) { DeleteOnlyNode(); return; } //get the predecessor node. var pointer = Last.Link; //Iterate to the predecessor. while (!pointer.Link.Equals(Last)) { pointer = pointer.Link; } pointer.Link = Last.Link; Last = pointer; }
/// <summary> /// Insertion in an empty list. /// </summary> /// <param name="node"></param> private void InsertionInAnEmptyList(CircularLinkedListNode <T> node) { Last = node; Last.Link = Last; }
//Constructor. public CircularLinkedList() { Last = null; }
/// <summary> /// Delete only node . /// </summary> private void DeleteOnlyNode() { Last = null; }