java2-链表【例】

实现链表操作


//链表
//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();
	}	
}

简单的链表增加删除查询操作

风景照一张,当然自己拍的



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值