public class Node {
private Object data;
private Node nextNode;
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public Node getNextNode() {
return nextNode;
}
public void setNextNode(Node nextNode) {
this.nextNode = nextNode;
}
}
public class Lianbiao {
private Node headNode;
private int size;
/**
* 增加node
*
* @param node
*/
void addNode2(Node node) {
if (size == 0) {
headNode = node;
} else {
node.setNextNode(headNode);
headNode = node;
}
size++;
}
/**
* 查询数据
*
* @param data
* @return
*/
private Node findNode(Object data) {
int sizeTemp = size;
Node xianzaiNode = headNode;
while (sizeTemp > 0) {
if (xianzaiNode.getData().equals(data)) {
return xianzaiNode;
} else {
xianzaiNode = xianzaiNode.getNextNode();
}
sizeTemp--;
}
return null;
}
public static void main(String[] args) {
Node node1 = new Node();
node1.setData("北京航空航天大学");
Node node2 = new Node();
node2.setData("西安交通大学");
Lianbiao lianbiao = new Lianbiao();
lianbiao.addNode2(node1);
lianbiao.addNode2(node2);
Node node = lianbiao.findNode("北京航空航天大学");
Object object = node.getData();
System.out.println(object);
}
}
java实现链表 增加 查找
最新推荐文章于 2023-04-14 23:47:08 发布