简单的实现下链表,最近复习下数据结构
public class linklist<Item>{ private Node first; private int size; private class Node{ Item item; Node next; } public linklist(){ first=null; size=0; } public void insert(Item value){ Node oldfirst=first; first=new Node(); first.item=value; first.next=oldfirst; size++; } public int size(){ return size; } //delete from head public boolean delete(){ if(size!=0){ first=first.next; size--; return true; } return false; } //search one item from the list public Item search(Item value){ Node p=first; while(p!=null){ if(p.item==value){ return value; } p=p.next; } return null; } //output the whole list public void display(){ Node p=first; while(p!=null){ System.out.print(p.item+" "); p=p.next; } System.out.println(); } }
end
原文:http://www.cnblogs.com/wuxiongliu/p/4364870.html