1.链表
public class Node {
public int data;
public Node next;
public Node() {
}
public Node(int data) {
this.data = data;
}
public void display() {
System.out.print(this.data + " ");
}
}
public class LinkList {
public Node first;
public int elements;
public LinkList() {
}
/**
*
* @Title: insert
* @Description: 添加结点
* @param data
*/
public void insert(int data) {
Node node = new Node(data);
if(first == null) {
first = node;
} else {
node.next = first;
first = node;
}
this.elements ++ ;
}
/**
*
* @Title: display
* @Description: 输出链表的所有结点数据
*/
public void display() {
Node current = first;
while(current != null) {
current.display();
current = current.next;
}
}
/**
*
* @Title: size
* @Description: 链表节点个数
* @return
*/
public int size() {
return this.elements;
}
/**
*
* @Title: deleteFirst
* @Description: 删除
* @throws Exception
*/
public void deleteFirst() throws Exception {
if(elements <= 0) {
throw new Exception("空链表不能删除结节!");
} else {
first = first.next;
elements --;
}
}
}