package com.hb.ds.sigleLinkList;
class ListNode {
ListNode next;
int value;
public ListNode(int value){
this.next = null;
this.value = value;
}
public void printNodeValue(){
System.out.print(this.value+" ");
}
}
public class SingleLinkList {
private ListNode first;
private int length;
public SingleLinkList(){
this.first = null;
this.length =0;
}
public void insertBeforeFirst(int value){
ListNode node = new ListNode(value);
if(first==null){
first = node;
}else{
node.next = first;
first = node;
}
length++;
}
public void printList(){
ListNode temp = first;
while(temp != null){
temp.printNodeValue();
temp = temp.next;
}
}
public static void main(String[] args){
SingleLinkList slinkList = new SingleLinkList();
slinkList.insertBeforeFirst(12);
slinkList.insertBeforeFirst(11);
slinkList.insertBeforeFirst(10);
slinkList.insertBeforeFirst(9);
slinkList.insertBeforeFirst(8);
slinkList.printList();
}
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/9474419/viewspace-478448/ ,如需转载,请注明出处,否则将追究法律责任。