网站建设资讯

NEWS

网站建设资讯

链表java最短代码 java链表的长度

用JAVA编写链表类,要求编写能够从头部添加节点。

public class ZLinkedList {

创新互联客户idc服务中心,提供资阳托管服务器、成都服务器、成都主机托管、成都双线服务器等业务的一站式服务。通过各地的服务中心,我们向成都用户提供优质廉价的产品以及开放、透明、稳定、高性价比的服务,资深网络工程师在机房提供7*24小时标准级技术保障。

private int size;

private Node head;

public ZLinkedList(){

size = 0;

}

public 卜漏void headInsert(Object obj){

//if(null== obj) {// do something}

Node temp = new Node(obj);

if(size ==0){

head = temp;

}else{

temp.setNext(head);

head = temp;

}

size++;

}

public void preOrder(){

int length = size;

Node temp = head;

for(int i= 0;i  length;i ++){

System.out.println(temp.getValue());

temp = temp.getNext();

}

}

private static class Node{

private Object value;

private Node next;

Node(){

}

Node(Object val){

this.value = val;

}

public Object 型弯烂getValue() {

return value;

}

public void setValue(Object value) {

this.value = value;

}

public Node getNext() {

return next;

}

public void setNext(Node next) {

this.next = next;

}

}

public static void main(String[] args) {

ZLinkedList 闹中test = new ZLinkedList();

test.headInsert("1");

test.headInsert("2");

test.headInsert("3");

test.preOrder();

}

}


本文标题:链表java最短代码 java链表的长度
文章地址:http://cdweb.net/article/dsppdig.html