链表是一种常见的数据结构,其中运用到了结构体指针,链表可以实现动态存储分配,换而言之,链表是一个功能强大的数组,可以在某个节点定义多种数据类型,可以实现任意的添加,删除,插入节点等。链表分为两类,一种是有头节点的链表,另一种是无头节点的链表。头节点没有数据域的,其他每个节点都有两部分,一是数据域,二是指针域。
public class LinkedList {
private Node head;//头部指针
private int count;//节点数量
public void add(Object data) {
Node node = new Node();
node.data = data;//传入数据
if (count < 1) {//节点数量小于1
this.head = node;
} else {
Node tmp = head;
while (tmp.next != null) {
tmp = tmp.next;
}
tmp.next = node;
}
count++;
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.add(1);
list.add(2);
System.out.println(list.count);
Node tmp = list.head;
while(tmp!=null) {
System.out.println(tmp);
tmp=tmp.next;
}
}
class Node {
private Object data;//存放数据
private Node next;//下一个位置
@Override
public String toString() {
return "Node [data=" + data + ", next=" + next + "]";
}
}
}



