【发布时间】:2015-05-08 07:26:17
【问题描述】:
嗨,我有这个方法可以在 LinkedList 的任何索引处插入一个元素,但是,新元素没有显示在输出中,我错过了什么谢谢! 我在下面展示了部分代码,非常感谢任何帮助!
public class LinkedList<E extends Comparable<E>> implements Iterable<E>
{
// instance data members of list
private Node head; // reference to the first node
private int N; // number of elements stored in the list
private class Node
{
// instance data members of Node
public E item;
public Node next;
// constructors for Node
public Node()
{
item = null; next = null;
}
public Node(E e, Node ptr)
{
item = e; next = ptr;
}
}// end class Node
public void insertAfter(int k, E e){
if (k < 0 || k >= size()){
throw new IndexOutOfBoundsException();}
Node temp=new Node();
temp.item=e;
int index=k-1;
Node current=head;
for (int i=0; i<=N; N++){
if (i==index){
temp.next=current.next;
current.next=temp;
}
}
++N;
}
【问题讨论】:
标签: java linked-list