internal void AddFirst(ref LinkedNode <T> n) { n.next = _start;//save the next of n as _start if (_end == null) { _end = n; //if end is null that means that the first value is allsow the last so save the first n as end } if (_start != null) { _start.previous = n; //if _start isnt null the previous of start is n } _start = n; //save the new node=n as _start }
//add the last number of the list internal void AddLast(ref LinkedNode <T> n) { //if there is no node in the list if (_start == null) { //when you add the last nude you could allsow add the first node there the same AddFirst(n); //finish the methode return; } //save the next of end as the new node _end.next = n; //save the previous of end as _end n.previous = _end; //save the node as end _end = n; }
public List <T> TurnLinkedListToList() { //create list List <T> list = new List <T>(); //create tmp node on linled list LinkedNode <T> tmp = _start; //if tmp exist do a loop untill it stops to exist while (tmp != null) { list.Add(tmp.value); //turn tmp to the next value on the list tmp = tmp.next; } //return list return(list); }
public bool RemoveLast(out T valueToRemove) { //if the start and end are the same it means if we move the first is the same as moving the last if (_end == _start) { return(RemoveFirstValue(out valueToRemove)); } //so we use the remove first value //save the value of want to remove valueToRemove = _end.value; //turn end to the end befor him _end = _end.previous; //turn the next value to null _end.next = null; //return that the ramove methode wroked return(true); }
//create the to string of linked_list public override string ToString() { //create a string builder StringBuilder sb = new StringBuilder(); //create tmp node and start it with _start LinkedNode <T> tmp = _start; //if tmp exist do a loop untill it stops to exist while (tmp != null) { //add value to our string builder sb.AppendLine($"{tmp.value.ToString()}, "); //turn tmp to the next value on the list tmp = tmp.next; } //return the string we got return(sb.ToString()); }
} //the end of the list //get the value in the index of index #region Methode public bool GetAt(int index, out LinkedNode <T> valuAt) { //tmp value start it with tmp LinkedNode <T> tmp = _start; //a loop in the size of the index for (int i = 0; i < index && tmp != null; i++) { //move to the next node in the list tmp = tmp.next; } //save defoult value of T valuAt = default(LinkedNode <T>); //if tmp is null return false that means that the index is out of the list if (tmp == null) { return(false); } //save the value of the node in the index valuAt = tmp; //return that we found the index return(true); }