public object Add(int index, object o) { if (index < 0) throw new ArgumentOutOfRangeException("Index: " + index); if (index > this.Count) index = this.Count; Node current = this.head; if (this.Empty || index == 0){ this.head = new Node(o, this.head); } else { for (int i = 0; i < index - 1; i++) current = current.Next; current.Next = new Node(o, current.Next); } return o; }
public Node(object data, Node next) { this.data = data; this.next = next; }
public object Remove(int index) { if (index < 0) return null; if (this.Empty) return null; if (index >= this.Count) index = this.Count - 1; Node current = this.head; object result = null; if(index == 0){ result = current.Data; this.head = current.Next; } else{ for (int i = 0; i < index - 1; i++) current = current.Next; result = current.Next.Data; current.Next = current.Next.Next; } return result; }
public LinkedList() { this.head = null; //this.count = 0; }
public Node GetPrevious(Node n) { if (this.head == n) return null; Node temp = this.head; Node pre_n = null; while(temp != null && temp != n){ pre_n = temp; temp = temp.Next; } if(temp != n){ throw new ArgumentOutOfRangeException("Maximum: " + this.Count); } return pre_n; }
public void Clear() { this.head = null; }
public Node Change(Node n1, Node n2) { if (this.head == null || n1 == null || n2 == null) return null; Node pre_n1 = GetPrevious(n1); Node pre_n2 = GetPrevious(n2); if (pre_n1 != null) pre_n1.Next = n2; if (pre_n2 != null) pre_n2.Next = n1; Node temp; temp = n1.Next; n1.Next = n2.Next; n2.Next = temp; if (this.head == n1){ this.head = n2; } else if(this.head == n2){ this.head = n1; } return temp; }