//向链表索引i前插入元素 public bool Insert(int index, T element) { Node go = this.GetNodeByIndex(index - 1); if (go == null || go.next == null) { Console.WriteLine("索引非法!"); return false; } Node add = new Node(); add.data = element; add.next = go.next; go.next = add; this._curentLinkLength++; return true; }
要在指定位置前插入元素,那么就要得到插入元素的前一个元素,这样就可以设置新加元素的下一个元素为index位置结点(就是pre结点的next结点),然后设置pre的next结点为新的结点就行了。