第一行代码 之 第二篇代码笔记

class link{
	private class node1{
		private String data;
		private node1 next;
		public node1(String data){
			this.data = data;
		}
		public void addnode(node1 newnode){
			if (this.next == null){
				this.next = newnode;
			}
			else  {
				this.next.addnode(newnode);
			}
		}
		public String getnode(int index){
			if (link.this.foot ++ == index){
				return this.data;
			}
			else {
				return this.next.getnode(index);
			}
		}
		public void setnode(int index,String  data){
			if (link.this.foot ++ ==index){
				this.data= data;
			}
			else {
				 this.next.setnode(index, data);
			}
		}
		public boolean containsnode(String data){
			if (data.equals(this.data)){
				return true;
			}
			else {
				if (this.next != null){
					return this.next.containsnode(data);
				}
				else {return false;}
			}
		}
		public void removenode(node1 previous,String data){
			if (data.equals(this.data)){
				previous.next = this.next;
			}
			else {
				this.next.removenode(this,data);
			}
		}
		public void printnode(){
			System.out.println(this.data);
			if (this.next != null){
				this.next.printnode();
			}
		}
	}
	// =====================================================
	private int foot =0;
	private int count =0;
	private node1 root;
	public void add(String data){
		node1 newnode = new node1(data);
		if (this.root == null){
			this.root = newnode;
		}
		else {
			this.root.addnode(newnode);
		}
		this.count ++;
	}
	public String get(int index){
		if (index > this.count -1){
			return null;
		}
		this.foot = 0;
		return this.root.getnode(index);
	}
	public void set(int index , String data){
		if (index > this.count -1){
			return;
		}
		this.foot = 0;
		this.root.setnode(index, data);
	}
	public boolean contains(String data){
		if (data == null || this.root == null){
			return false;
		}
		return this.root.containsnode(data);
	}
	public void remove(String data){
		if (this.contains(data)){
			if (data.equals(this.root.data)){
				this.root = this.root.next;
			}
				else {this.root.next.removenode(this.root,data);}
		}
		this.count --;
	}
	public void print(){
		if (this.root != null){
			this.root.printnode();
		}
	}
}
public class node{
	public static void main(String args[]){
		link lk = new link();
		lk.add("hello");
		lk.add("world");
		lk.add("like");
		lk.add("you");
		lk.print();
		System.out.println("============");
		System.out.println(lk.get(1));
		System.out.println(lk.get(1));
		System.out.println("============");
		lk.set(0,"Mr");
		lk.set(1,"hello");
		lk.print();
		System.out.println("============");
		lk.remove("like");
		lk.remove("hello");
		lk.print();


	}
} 

你可能感兴趣的:(java初学代码)