public void Reverse() { if (First is null || Last is null) { return; } if (First == Last) { return; } var current = First; IDoubleLinkedListNode <T> next; do { next = current.Next; current.Next = current.Prev; current.Prev = next; if (!(next is null)) { current = next; } } while (next != null); Last = First; First = current; }
public void AddFirst(T value) { var tmp = new DoubleLinkedListNode <T> { Next = null, Prev = null, Value = value }; if (First is null && Last is null) { First = tmp; Last = tmp; }
public void Reverse() { IDoubleLinkedListNode <T> prev = null, current = First, next = null; while (current.Next != null) { next = current.Next; current.Next = prev; prev = current; current = next; } current.Next = prev; First = current; }
public void AddLast(T value) { var newElem = new DoubleLinkedListNode <T> { Value = value, Prev = Last, Next = null }; if (First == null) { First = Last = newElem; } Last.Next = newElem; Last = newElem; }
public void Reverse() { IDoubleLinkedListNode <T> temp = null; var current = First; while (current != null) { temp = current.Prev; current.Prev = current.Next; current.Next = temp; current = current.Prev; } if (temp != null) { First = temp.Prev; } }
public void AddFirst(T value) { var newElem = new DoubleLinkedListNode <T> { Value = value, Prev = null, Next = First }; if (First != null) { First.Prev = newElem; } else { First = Last = newElem; } First = newElem; }
public void AddFirst(T item) { if (item != null) { var tmp = new DoubleLinkedListNode <T>(); tmp.Value = item; if (First == null && Last == null) { tmp.Next = Last; tmp.Prev = First; First = Last = tmp; } else { tmp.Next = First; First = tmp; } } }
static void Main(string[] args) { const int size = 5; int[] vec = new int[size] { 4, 1, 5, 7, 2 }; DoubleLinkedList <int> list = new DoubleLinkedList <int>(); list.AddFirst(vec[1]); list.AddLast(vec[2]); list.AddLast(vec[3]); list.AddFirst(vec[0]); list.AddLast(vec[4]); /* ***** */ Console.WriteLine("\n Input data Test\n-------------------\n"); IDoubleLinkedListNode <int> Temp = list.First; for (int i = 0; i < size; i++) { Assert.AreEqual(vec[i], Temp.Value); Console.WriteLine(" Pos: {0,2}, Value: {1,2}\n", i, Temp.Value); Temp = Temp.Next; } /* ***** */ Console.WriteLine("\n Reverse Test\n----------------\n"); list.Reverse(); Temp = list.First; for (int i = size - 1; i >= 0; i--) { Assert.AreEqual(vec[i], Temp.Value); Console.WriteLine(" Pos: {0,2}, Value: {1,2}\n", (size - 1) - i, Temp.Value); Temp = Temp.Next; } }
public void AddLast(T value) { IDoubleLinkedListNode <T> node = new DoubleLinkedListNode <T> { Value = value }; if (Last != null) { var currNode = Last; Last.Next = node; node.Prev = Last; Last = node; } else { Last = node; First = node; } }
public void Reverse() { if (First == null) { return; } var currentElem = First; while (currentElem != null) { var tmp = currentElem.Next; currentElem.Next = currentElem.Prev; currentElem.Prev = tmp; currentElem = currentElem.Prev; } var lastTmp = Last; Last = First; First = lastTmp; }
public void Reverse() { IDoubleLinkedListNode <T> Temp = First; IDoubleLinkedListNode <T> Prev; IDoubleLinkedListNode <T> Next; if (Temp == null) { return; } Last = First; while (Temp != null) { Prev = Temp.Prev; Next = Temp.Next; Temp.Prev = Next; Temp.Next = Prev; First = Temp; Temp = Next; } }
public DoubleLinkedList() { First = null; Last = null; }