package 链表的实现;
public class Singlelinkedlist {
public static void main(String[] args) {
Node node = new Node(3);
System.out.println(node.data);
int arr[]={1,2,3};
Node node1 = null;
print(createNodetail(node1,arr));
}
public static Node createNodehead(Node node, int val[]){
for(int i=0;i<val.length;i++){
Node p=new Node(val[i]);
p.next=node;
node=p;
}
return node;
}
public static Node createNodetail(Node node,int val[]){
Node temp = null;
for(int i=0;i<val.length;i++){
Node p=new Node(val[i]);
if(node==null) {
node=temp=p;
}else{
temp.next=p;
temp=p;
}
}
return node;
}
public static void print(Node node){
for(Node p=node;p!=null;p=p.next){
System.out.print(p.data+" ");
}
}
public static class Node{
public int data;
public Node next;
public Node(int data){
this.data = data;
}
}
}
|