void MoveToFront(int value) { var node = GetNode(value); if (node != null && this.head != node) { if (node.prev != null) { node.prev.next = node.next; } else { this.tail = node.next; } if (node.next != null) { node.next.prev = node.prev; this.head.next = node; node.prev = this.head; node.next = null; this.head = node; } } Console.WriteLine("printing from tail"); PrintLL(this.tail); Console.WriteLine("printing from head"); PrintLL(this.head, false); }
public DLLNode(int key, int value) { this.value = value; this.prev = null; this.next = null; this.key = key; }
DLLNode GetNode(int nodeValue) { DLLNode node = null; var temp = this.tail; while (temp != null) { if (temp.value == nodeValue) { node = temp; return(node); } temp = temp.next; } return(node); }
void DeleteTail() { if (this.tail != null) { var node = this.tail; this.tail = node.next; if (this.tail != null) { this.tail.prev = null; } node.next = null; node.prev = null; } PrintLL(this.tail); }
//Program() //{ // this.head = null; // this.tail = null //} void InsertLL(int value) { var node = new DLLNode(1, value); if (this.head == null) { this.head = node; this.tail = node; } else { this.head.next = node; node.prev = this.head; this.head = node; } PrintLL(this.tail); }
public void ReverseDLL() { DLLNode node1 = null, node2 = null, node3 = null, node4 = null, node5 = null; node5 = new DLLNode() { Value = 5, Next = null, }; node4 = new DLLNode() { Value = 4, }; node3 = new DLLNode() { Value = 3, }; node2 = new DLLNode() { Value = 2, }; node1 = new DLLNode() { Value = 1, Prev = null, Next = node2 }; node2.Next = node3; node2.Prev = node1; node3.Next = node4; node3.Prev = node2; node4.Next = node5; node4.Prev = node3; node5.Prev = node4; DLLNode rHead = ReverseDLL(node1); }
static void PrintLL(DLLNode head, bool fromTail = true) { var temp = head; while (temp != null) { Console.Write(temp.value + "-->"); if (fromTail) { temp = temp.next; } else { temp = temp.prev; } } Console.WriteLine(); }
private DLLNode ReverseDLL(DLLNode lst) { DLLNode tmp = null; var current = lst; //swap previos and next pointers while (current != null) { tmp = current.Prev; current.Prev = current.Next; current.Next = tmp; current = current.Prev; } if (tmp != null) { lst = tmp.Prev; } return(lst); }
public void SetPrev(DLLNode prev) { this._prev = prev; }
public void SetNext(DLLNode next) { this._next = next; }
public DLLNode(int data, DLLNode prev, DLLNode next) { this._data = data; _prev = prev; _next = next; }
public DLLNode(int data) { this._data = data; _prev = null; _next = null; }