实现链表操作
//链表
//2012.7.25
class DoIt{
class Node{
private String data;
private Node next;
public void setNext(Node next){
this.next = next;
}
public Node getNext(){
return this.next;
}
public void setData(String data){
this.data = data;
}
public String getData(){
return this.data;
}
public boolean add(Node head,String data){ //增加操作
if(head.next==null){
Node a = new Node();
head.next = a;
a.data = data;
return true;
}else{
return add(head.next,data);
}
}
public void print(Node last){ //打印操作
System.out.print(last.data + "\t");
if(last.next!=null){
print(last.next);
}
}
public boolean searchData(Node last,String a){
if(last.data.equals(a)){
return true;
}
if(last.next==null){
return false;
}else{
return searchData(last.next,a);
}
}
public void delet(Node last,String a){
if(last.next.data.equals(a)){
last.next = last.next.next;
}else{
delet(last.next,a);
}
}
};
public Node head;
public boolean check(){ //判断是否有链表存在
if(head==null){
return true;
}else{
return false;
}
}
public boolean addNode(String data){ //增加函数
if(check()){
Node tree = new Node();
head = tree;
head.data = data;
return true;
}
return head.add(head,data);
}
public void printof(){ //打印函数
if(check()){
System.out.println("没有数据!");
}
head.print(head);
System.out.println("");
}
public boolean search(String data){
if(check()){
return false;
}else{
return head.searchData(head,data);
}
}
public void delet(String data){
if(search(data)){
if(head.data.equals(data)){
head = head.next;
return;
}
head.delet(head,data);
}
}
};
public class List{
public static void main(String args[]){
DoIt l = new DoIt();
l.addNode("A");
l.addNode("B");
l.addNode("C");
l.addNode("D");
l.addNode("E");
l.printof();
l.delet("H");
l.delet("F");
l.printof();
}
}
简单的链表增加删除查询操作
风景照一张,当然自己拍的