java學習筆記——可用鏈表

來源:互聯網
上載者:User

標籤:

NO 鏈表方法名稱 描述
1 public void add(資料類型 對象) 向鏈表中增加資料
2

public int size()

查看鏈表中資料個數
3 public boolean isEmpty() 查看鏈表是否為空白
4 public void clean() 清空鏈表
5 public 資料類型 get(int index) 返回指定索引的資料對象,需要使用自訂類中的Compare()函數方法
6 public boolean contains(資料類型  對象) 查看鏈表中是否包含資料對象,需要使用自訂類中的Compare()函數方法
7 public  void remove(資料類型 對象) 刪除鏈表中資料對象,需要使用自訂類中的Compare()函數方法
8 public 資料類型[] toArray() 轉換為資料對象數組

 

 

 

 

 

 

 

 

 

 

 

 

class Person{
String name;
int age; public Person(String name,int age) { // TODO Auto-generated constructor stub this.name=name; this.age=age; } public boolean Compare(Person person){ if(person==null) return false; if(this==person) return true; if(this.name==person.name&&this.age==person.age) return true; return false; } public void getInfo(){ System.out.println("name:"+name+",age:"+age); }}class Link{ private class Node{ private Person data; private Node next; public Node(Person data) { // TODO Auto-generated constructor stub this.data=data; } /**********************************************/ public void addNode(Node newNode){ if(this.next==null) this.next=newNode; else { this.next.addNode(newNode); } } /*********************************************/ public Person getNode(int index){ if(index==Link.this.foot++)//注意:內部類能直接存取外部類的變數 return this.data; return this.next.getNode(index); } /*********************************************/ public boolean containsNode(Person data){ if(this.data.Compare(data)) return true; else if(this.next==null)/*重點*/ return false; return this.next.containsNode(data); } public void removeNode(Node previous,Person data){ if(this.data.Compare(data)) previous.next=this.next; else this.next.removeNode(this, data); } public void toarrayNode(){ Link.this.retArray[Link.this.foot++]=this.data; if(this.next!=null) this.next.toarrayNode(); } /*********************************************/ } private Node root; private int count=0;//結點個數 private int foot;//為了尋找相關位置節點 private Person [] retArray; public void add(Person data){ Node newNode=new Node(data); if(root==null) root=newNode; else { root.addNode(newNode); } count++; } public int size(){ return count; } public boolean isEmpty(){ return this.count==0; } public void clean(){//java的虛擬機器會自動回收那些分配的空間 root=null; count=0; } public Person get(int index){ if(index>count) return null; this.foot=0; return this.root.getNode(index); } public boolean contains(Person data){ if(data==null) return false; return this.root.containsNode(data); } public void remove(Person data){ if(this.contains(data)){ if(this.root.data==data) this.root=this.root.next; else this.root.next.removeNode(root, data);//重點 this.count--; } } public Person [] toArray(){ if(this.count==0) return null; retArray=new Person[count]; this.foot=0; this.root.toarrayNode(); return retArray; }}public class LinkDemo { public static void main(String[] args) { // TODO Auto-generated method stub Link link=new Link(); link.add(new Person("張三", 20)); link.add(new Person("李四", 21)); link.add(new Person("王五", 22)); System.out.println("size:"+link.size()); Person[] per1=link.toArray(); for(int i=0;i<per1.length;i++) per1[i].getInfo(); Person tmp=new Person("李四", 21); link.remove(tmp); Person[] per2=link.toArray(); for(int i=0;i<per2.length;i++) per1[i].getInfo(); }}

 

java學習筆記——可用鏈表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.