/// <summary> /// initialise from the array /// </summary> /// <param name="array"></param> public void Init(int[] array) { if (array.Length == 0) { throw new ArgumentException(); } RootNode = new Node(array[0]); Node outputNode = RootNode; Node tempNode; for (int i = 1; i < array.Length; i++) { tempNode = new Node(array[i]); outputNode.Next = tempNode; outputNode.Next.Prev = outputNode; outputNode = tempNode; if (array.Length == i + 1) { outputNode.Next = RootNode; RootNode.Prev = outputNode; } } }
/// <summary> /// Delete value from the start position /// </summary> /// <returns></returns> public int DeleteStart() { int res = RootNode.Value; RootNode = RootNode.Next; return res; }
/// <summary> /// Reverse values /// </summary> public void Reverse() { Node temp = RootNode; bool flag = true; while (flag) { Node next = temp.Next; Node prev = temp.Prev; temp.Next = prev; temp.Prev = next; if (next != null) { temp = next; } else { flag = false; } } RootNode = temp; }
/// <summary> /// Add value in the start position /// </summary> /// <param name="value"></param> public void AddStart(int value) { Node tempNode = new Node(value, RootNode); this.RootNode.Prev = tempNode; this.RootNode = tempNode; }
/// <summary> /// Clear LList /// </summary> public void Clear() { this.RootNode = new Node(); }
/// <summary> /// Bubble sort /// </summary> public void Sort() { Node first = RootNode; Node currentNode = RootNode; Node tempNode = currentNode; while (currentNode.Next != null) { Node temp3 = currentNode.Next; // while (currentNode.Next != null) { if (currentNode.Value > currentNode.Next.Value) { if (first == currentNode) { first = currentNode.Next; } tempNode = currentNode.Next.Next; Node temp2 = currentNode.Next; temp2.Next = currentNode; currentNode.Next = tempNode; //currentNode.Next.Next = currentNode; //currentNode.Next = temp2; } else { currentNode = currentNode.Next; } } } RootNode = first; }
/// <summary> /// Add value in the index position /// </summary> /// <param name="value"> int variable</param> /// <param name="index"> position</param> public void AddPosition(int value, int index) { if (this.Size <= index) { throw new IndexOutOfRangeException(); } Node currentNode = RootNode; //Node prevLink; for (int i = 0; i < this.Size; i++) { if (index - 1 == i) { Node positionNode = new Node(value, currentNode, currentNode.Next); positionNode.Prev = currentNode; currentNode.Next = positionNode; } else { currentNode = currentNode.Next; } } }
/// <summary> /// Reverse values /// </summary> public void Reverse() { Node first = RootNode; bool flag = true; Node current = null; Node prev = first; Node next = first.Next; while (flag) { current = next; next = current.Next; current.Next = prev; prev = current; if (next == null) { flag = false; } } first.Next = null; RootNode = current; }
/// <summary> /// Clear LList /// </summary> public void Clear() { RootNode = new Node(); }
/// <summary> /// Add value in the start position /// </summary> /// <param name="value"></param> public void AddStart(int value) { RootNode = new Node(value, RootNode); }
/// <summary> /// Constructor with value, previous node and next node parameters /// </summary> /// <param name="value"></param> /// <param name="prev"></param> /// <param name="next"></param> public Node(int value, Node prev, Node next) { this.prev = prev; this.next = next; this.value = value; }
/// <summary> /// Constructor with value and next node parameters /// </summary> /// <param name="value">int</param> /// <param name="next">Node</param> public Node(int value, Node next) { this.Value = value; this.Next = next; this.Next.Prev = this; }