자바 를 사용 하여 양 방향 링크 데이터 구 조 를 실현 합 니 다.
3481 단어 자바 로 링크 데이터 구조 구현
package link;
public class Link {
/**
* ,
* @author binchen
*
*/
class Node{
private String data;
private Node next; //
public Node(){}
public Node(String data){
this.data = data; //
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
/**
*
*/
public void print(){
System.out.println(this.data + "\t");
if(this.next != null){
this.next.print();
}
}
/**
*
* @param newNode
*/
public void add(Node newNode){ //
if(this.next == null){
this.next = newNode;
}else{ // , next
this.next.add(newNode);
}
}
/**
*
* @param data
* @return
*/
public boolean search(String data){ //
if(this.data.equals(data)){
return true;
}else{ //
if(this.next != null){ // null,
return this.next.search(data);
}
return false;
}
}
/**
*
* @param data
*/
public void deleteNode(Node previous , String name){
if(name.equals(this.data)){
previous.next = this.next;
}else{
if(this.next != null){
this.next.deleteNode(this, name);
}
}
}
};
private Node root; //
public void setRoot(Node root){
this.root = root;
}
public Node getRoot(){
return this.root;
}
/**
*
* @param data
*/
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(); //
}
}
/**
*
* @param name
* @return
*/
public boolean contains(String name){
return this.root.search(name); // node
}
/**
*
* @param name
*/
public void deleteNode(String data){
if(this.contains(data)){ //
//
if(this.root.data.equals(data)){ //
this.root = this.root.next; // ,
}else{
this.root.next.deleteNode(root,data);
}
}
}
}