链表
1、结点的定义
每个结点除了可以保存数据之外还必须有对下一个结点的引用,结点就是数据的载体
package lianbiao;
public class Node<T> {
private T data;
private Node nextNode;
public Node() {
}
public Node(T data) {
this.data = data;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public Node getNextNode() {
return nextNode;
}
public void setNextNode(Node nextNode) {
this.nextNode = nextNode;
}
}
package lianbiao;
public class Test2 {
public static void main(String[] args) {
Node<String> root = new Node<>("one");
Node<String> n1 = new Node<>("two");
Node<String> n2 = new Node<>("three");
Node<String> n3 = new Node<>("four");
//维护结点之间的关系
root.setNextNode(n1);
n1.setNextNode(n2);
n2.setNextNode(n3);
print(root);
//使用循环输出节点数据
// Node<String> currentNode = root;
// while (currentNode!=null){
// System.out.print(currentNode.getData()+">>");
// currentNode = currentNode.getNextNode();
// }
}
/**
* 利用递归的方式输出链表数据
* @param currentNode
*/
public static void print(Node currentNode){
if (currentNode==null){
//当为空指向时就不执行函数了,递归的截止条件
return;
}
System.out.print(currentNode.getData()+">>");
print(currentNode.getNextNode());
}
}
2、内部类的方式
package lianbiao;
public class Link<T> {
private Node<T> root;//链表的根节点
private int foot;//表示链表结点的索引
private class Node<T>{
private T data;
private Node nextNode;
public Node() {
}
public Node(T data) {
this.data = data;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public Node getNextNode() {
return nextNode;
}
public void setNextNode(Node nextNode) {
this.nextNode = nextNode;
}
/**
* 添加结点
*/
public void addNode(Node<T> newNode){
if (this.getNextNode()==null){
this.setNextNode(newNode);
}else {
this.getNextNode().addNode(newNode);
}
}
/**
* 输出结点数据
*/
public void printNode(){
System.out.println(this.data);
if (this.nextNode!=null){
this.nextNode.printNode();
}
}
}
//在外部类定义主题能够访问的方法,从而调用内部类的方法
//添加结点
public void add(T data){
Node<T> newNode = new Node<>(data);
if (this.root==null){
this.root = newNode;
}else {
this.root.addNode(newNode);
}
}
//输出数据
public void print(){
if (this.root == null){
return;
}else {
this.root.printNode();
}
}
}
package lianbiao;
public class Test2 {
public static void main(String[] args) {
Link<String> link = new Link<>();
//添加数据
link.add("火车头");
link.add("1车厢");
link.add("2车厢");
link.add("3车厢");
link.add("4车厢");
link.add("车尾");
//输出数据
link.print();
}
}
将节点内定义内部类是为了:
让客户端代码变得简洁,将节点的创建以及节点之间的关系维护封装隐藏
让数据更安全,因为其他的类不能直接访问节点类了,只能通过外部类类访问
在外部类中添加判断链表是否为空和统计链表大小的方法
package lianbiao;
public class Link<T> {
private Node<T> root;//链表的根节点
private int foot;//表示链表结点的索引
private int count;
private class Node<T>{
private T data;
private Node nextNode;
public Node() {
}
public Node(T data) {
this.data = data;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public Node getNextNode() {
return nextNode;
}
public void setNextNode(Node nextNode) {
this.nextNode = nextNode;
}
/**
* 添加结点
*/
public void addNode(Node<T> newNode){
if (this.getNextNode()==null){
this.setNextNode(newNode);
}else {
this.getNextNode().addNode(newNode);
}
}
/**
* 输出结点数据
*/
public void printNode(){
System.out.println(this.data);
if (this.nextNode!=null){
this.nextNode.printNode();
}
}
}
//在外部类定义主题能够访问的方法,从而调用内部类的方法
//添加结点
public void add(T data){
Node<T> newNode = new Node<>(data);//创建结点对象
if (this.root==null){
this.root = newNode;
}else {
this.root.addNode(newNode);
}
count++;
}
//输出数据
public void print(){
if (this.root == null){
return;
}else {
this.root.printNode();
}
}
//判断结点的长度,或者结点的个数
public int size(){
return this.count;
}
//判断结点是否为空
public boolean isEmpty(){
return this.count==0;
}
}
package lianbiao;
public class Test2 {
public static void main(String[] args) {
Link<String> link = new Link<>();
//添加数据
link.add("火车头");
link.add("1车厢");
link.add("2车厢");
link.add("3车厢");
link.add("4车厢");
link.add("车尾");
//输出数据
link.print();
System.out.println(link.size());
System.out.println(link.isEmpty());
}
}