和C++并没有差别不大,主要是指针改为了引用变量,其他的链式结构基本可以参照这个
至于树的话注意下递归就大致可以了
package com.sun.study.test; class Link{ public int data; public Link next; public Link(int data) { this.data = data; } public void display(){ System.out.println("数据为:"+data); } } class LinkList{ private Link first; public LinkList(){ first=null; } public boolean isEmpty(){ return(first == null); } public void insert(int data){ Link newLink = new Link(data); newLink.next = first; first = newLink; } public void delete(int data){ Link current = first; Link del = null; for(;current!=null;current=current.next){ if(current.next.data == data){ del=current.next; current.next = del.next; break; } } } public void display(){ Link current= first; for(;current != null;current = current.next) System.out.println(current.data); } } class LinkListApp{ public static void main(String[] args){ LinkList list = new LinkList(); list.insert(88); list.insert(45); list.insert(85); list.insert(90); list.insert(8); list.insert(4); list.insert(5); list.insert(9); System.out.println(list.isEmpty()); list.display(); System.out.println("---------------"); System.out.println(list.isEmpty()); list.delete(90); list.display(); } }