【資料結構】·【連結串列】·【JAVA版】
阿新 • • 發佈:2019-01-22
和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(); } }