单链表的实现
单链表插入有两种:头插法和尾插法;
头插法是:point.next=head.next;
head.next=point;
尾插法是: head.next=point;
head=point;
package primDataStructure;
public class nodeList {
public String value;
public nodeList next;
public nodeList() {
next=null;
value=null;
}
public void setValue(String value) {
this.value=value;
}
public void addNode(nodeList node) {
nodeList next=this.next;
this.next=node;
node.next=next;
}
public int indexOfValue(String value) {
int i=0;
nodeList node=this;
while(node.next!=null) {
if(node.value.equals(value))
return i;
node=node.next;
i++;
}
return -1;
}
public static void main(String args[]) {
nodeList node=new nodeList();
node.setValue("0");
for(int i=0;i<10;i++) {
nodeList nextNode=new nodeList();
nextNode.setValue(""+i);
node.addNode(nextNode);
}
nodeList nexts=node;
while(nexts.next!=null) {
System.out.print(" "+nexts.value);
nexts=nexts.next;
}
System.out.println("找到index"+node.indexOfValue("8"));
}
}