public Node Insert(Node aNode, int x) { if (aNode==null) { aNode = new Node (x); aNode.Next = aNode; return aNode; } Node p = aNode; Node previous = null; do { previous = p; p = p.Next; if (x <= p.Value && x >= previous.Value) break; //x satisfy Case1 if (previous.Value > p.Value && (x < p.Value || x > previous.Value)) break; //x satisfy Case2 } while(p != aNode); // when back to starting point, x satisfy Case3 Node xNode = new Node (x); previous.Next = xNode; xNode.Next = p; return xNode; }
public Node(int value) { this.Value = value; this.Next = null; }
public Node(int value, Node next) { this.Value = value; this.Next = next; }
public Node Next { get; set;} // next is read-write public Node () { this.Next = null; }