彦舜原创,CSDN首发:Java类集中,链表(LinkedList)底层原理代码

package util.java;

/**@author 彦舜 */
public class Collection_LinkedList {
	public static void main(String[] args) {	
	}
}

//Use and encapsulate type Node, create a newly object of class Link  
class Link<T> extends Object implements java.lang.Cloneable, java.lang.Comparable<T>, java.io.Serializable{
	/**Three steps of creating a class*/
	
	/**T cannot be resolved to a type
	 --Add type parameter 'T' to 'Class' */
	//Link changed to Link<T>
	
	/**The type Link<T> must implement the inherited abstract method 
	Comparable<T>.compareTo(T)*/
	//Add unimplemented methods
	/**@Override
	public int compareTo(T o) {
		// TODO Auto-generated method stub
		return 0;
	} */
	@Override
	public int compareTo(T o) {
		return 0;
	}
	
	/**The serializable class Link does not declare a static final 
	serialVersionUID field of type long
	--Add default serial version ID
	private static final long serialVersionUID = 1L; */
	//Add generated serial version ID
	private static final long serialVersionUID = 8180581403709358588L;
	
	//Declare a private variable for root node
	private Node2<T> root;
	
	public void add(T t) {
		//Cannot infer type arguments for Node<>
		Node2<T> variable = new Node2<>(t);
		
		//LinkedList this = new LinkedList();
		/**Add new data into linklist, if in current linkedlist haven't 
		any nodes, first data as node */
		if(this.root == null) {
			this.root = variable;
		}else {
			/**if in the current class has nodes, add a new object or new
			 * node or new element to collection by using the node class */
			this.root.addNode(variable);
		}
	}
	//in the way of recursion, export all data or element in the collection
	public void print() {
		if(this.root != null) {
			this.root.printNode();
		}
	}
}

//Definition a node class
class Node2<T> extends Object implements java.lang.Cloneable, java.lang.Comparable<T>, java.io.Serializable{
	//Add default serial version ID
	private static final long serialVersionUID = 1L;
	//Add generated serial version ID
//	private static final long serialVersionUID = -4298449616877162803L;
	
	//Create private properties
	//The datas of needing to save 
	private T t;
	//This reference save the next node reference
	private Node2<T> node;
	
	//Auto create constructor, every the class object also be named element must save corresponding data
	public Node2(T t) {
		this.t = t;
	}
	
	@Override 
	public int compareTo(T t) {
		return 0;
	}
	
	//Create methods for setter and getter
	public void setT(T t) {
		this.t = t;
	}
	public T getT() {
		return this.t;
	}
	public void setNode(Node2<T> node) {
		this.node = node;
	}
	public Node2<T> getNode(){
		return this.node;
	}
	
	//实现节点的添加,按照先进先出的规则,新节点添加至末节点之后
	public void addNode(Node2<T> node2) {
		//The end condition of recursion, judge this node whether is a final node
		if(this.node == null) {
			this.node = node2;
		}else {
			this.node.addNode(node2);
		}
	}
	
	//In the way of recursion, obtain the saved data of every node
	public void printNode() {
		System.out.println(this.t);
		if(this.node != null) {
//			System.out.println(this.node.getT());
			this.node.printNode();
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值