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();
}
}
}
彦舜原创,优快云首发:Java类集中,链表(LinkedList)底层原理代码
最新推荐文章于 2025-07-15 11:54:02 发布
730

被折叠的 条评论
为什么被折叠?



