是我的类写错了,还是主函数写错,怎么一直运行不正确??
插不进值
主函数
import java.io.IOException;
import java.util.Scanner;
public class test {
public static void main(String[] args) throws IOException {
System.out.println("请输入链表长度n:");
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
Scanner li = new Scanner(System.in);
int[] b = new int[n];
for (int i = 0; i < b.length; i++) {
b[i] = li.nextInt();
}
LinkListClass p = new LinkListClass();
p.CreatList(b);
System.out.println(p.toString());
System.out.println("请输入要插入的序号及元素");
int i = scanner.nextInt();
int e = scanner.nextInt();
p.insert(i, e);
System.out.println(p.toString());
}
}
节点类
public class LinkNode {
int data;
LinkNode next;
public LinkNode() {
next=null;
}
public LinkNode(int d) {
data =d;
next=null;
}
}
LinkListClass
public class LinkListClass {
LinkNode head;
public LinkListClass() {
head=new LinkNode();
head.next=null;
}
public void CreatList(int[] b) {
LinkNode s ;
for(int i=0;i<b.length;i++)
{
s=new LinkNode(b[i]);
s.next=head.next;
head.next=s;
}
}
public String toString() {
String anString="";
LinkNode p=head.next;
while (p!=null) {
anString+=p.data+"";
p=p.next;
}
return anString;
}
//第二题
public int size() {
LinkNode p=head;
int cnt=0;
while (p.next!=null) {
cnt++;
p.next=p;
}
return cnt;
}
public LinkNode geti(int i) {
LinkNode p=head;
int j=-1;
while (j<1) {
j++;
p=p.next;
}
return p;
}
public void insert(int i,int e) {
if(i<0||i>size())
throw new IllegalArgumentException("设置长度n:不再有效的范围内");
LinkNode sLinkList=new LinkNode(e);
LinkNode pLinkList=geti(i-1);
sLinkList.next=pLinkList.next;
pLinkList.next=sLinkList;
}
}