採用面向對象的方法設計一個線性表,目的是爲其餘須要用線性表的應用提供線性表對象。this
思路:寫一個線性表類,在類中實現Node的定義,來定義每個點,同時實現増刪改查的操做。spa
class Link{ class Node{ private String data; private Node next; public Node(String data){ this.data=data; } public void add(Node newNode){//節點中的增長方法 if(this.next==null){ this.next=newNode; } else{ this.next.add(newNode); } } public void print(){ System.out.print(this.data+"\t"); if(this.next!=null) this.next.print(); } public boolean search(String data){ if(data.equals(this.data)){ return true; } else{ if(this.next!=null) return this.next.search(data); else return false; } } public void delete(Node pre,String data){ if(data.equals(this.data)) pre.next=this.next; else{ if(this.next!=null){ this.next.delete(this,data); } } } }; private Node root; public void addNode(String data){ Node newNode=new Node(data); if(this.root==null) this.root=newNode; else this.root.add(newNode); } public void printNode(){ if(this.root!=null){ this.root.print(); } } public boolean findNode(String name){ return this.root.search(name); } public void deleteNode(String data){ if(this.findNode(data)){ if(this.root.data.equals(data)){ this.root=this.root.next; } else this.root.next.delete(root,data); } else System.out.println("要刪除的節點不存在"); } }; public class link01{ public static void main(String args[]){ Link H=new Link(); H.addNode("A"); H.addNode("B"); H.addNode("C"); H.addNode("D"); H.addNode("E"); H.addNode("F"); H.printNode(); System.out.println(); H.deleteNode("A"); H.deleteNode("D"); H.printNode(); System.out.println(); System.out.println(H.findNode("E")); H.deleteNode("X"); } }
可是,這樣是不夠的。按照題目的要求,這個線性表不是一個簡單的線性表,看要求「爲其餘須要線性表的應用提供線性表對象」,這個要求體現了線性表的可複用性。所以將上述Node類中的String data改造一下,改成Object定義的數據,由於Object是全部類的父類,所以能夠用其餘任何類來實現初始化,而且用到輸出的時候,沒必要擔憂封裝的數據的不一樣而致使不能實現,只需重載Object中的toString()方法便可。設計
例如:Object ob1=new Person("張三",19);code
Object ob2=new Pet("小花","花色",2);對象
具體代碼以下:blog
package pro; class Link{ class Node{ private Object data; private Node next; public Node(Object ob){ this.data=ob; } public void add(Node newNode){//節點中的增長方法 if(this.next==null){ this.next=newNode; } else{ this.next.add(newNode); } } public void print(){ System.out.print(this.data.toString()+"\t"); if(this.next!=null) this.next.print(); } public boolean search(Object data){ if(data.equals(this.data)){ return true; } else{ if(this.next!=null) return this.next.search(data); else return false; } } public void delete(Node pre,Object data){ if(data.equals(this.data)) pre.next=this.next; else{ if(this.next!=null){ this.next.delete(this,data); } } } }; private Node root; public void addNode(Object data){ Node newNode=new Node(data); if(this.root==null) this.root=newNode; else this.root.add(newNode); } public void printNode(){ if(this.root!=null){ this.root.print(); } } public boolean findNode(Object name){ return this.root.search(name); } public void deleteNode(Object data){ if(this.findNode(data)){ if(this.root.data.equals(data)){ this.root=this.root.next; } else this.root.next.delete(root,data); } else System.out.println("要刪除的節點不存在"); } }; class Person{ private String name; private int age; public Person(String name,int age){ this.name=name; this.age=age; } public String toString(){ return "姓名:"+this.name+" 年齡:"+this.age; } } class Pet{ private String name; private String color; private int age; public Pet(String name,String color,int age){ this.name=name; this.color=color; this.age=age; } public String toString(){ return "寵物名:"+this.name+" 顏色:"+this.color+" 年齡:"+this.age; } } public class Link01{ public static void main(String args[]){ Link H=new Link(); Object ob1=new Person("張三",19); Object ob2=new Pet("大黃","黃色",2); Object ob3=new Person("李四",20); Object ob4=new Pet("小黑","黑色",3); Object ob5=new Person("王五",30); H.addNode(ob1); H.addNode(ob2); H.addNode(ob3); H.addNode(ob4); H.addNode(ob5); H.addNode("F"); H.printNode(); System.out.println(); H.deleteNode(ob2); H.deleteNode(ob4); H.printNode(); System.out.println(); System.out.println(H.findNode(ob1)); H.deleteNode("X"); } }