标签:node 更新 tno turn 头结点 rem new on() and
public class LinkList {
private Node firstNode;
private Integer position;
public LinkList() {
super();
}
public LinkList(Node firstNode) {
super();
this.firstNode = firstNode;
}
public Node getFirstNode() {
return firstNode;
}
public void setFirstNode(Node firstNode) {
this.firstNode = firstNode;
}
public Integer getPosition() {
return position;
}
public void setPosition(Integer position) {
this.position = position;
}
/**
* 无位置参数:在链表的末尾添加
*
* @param node
* @return
*/
public LinkList add(Node node) {
// 从头结点开始,依次找下去知道为空为止
Node currentNode = this.firstNode;
if (firstNode.getNextNode() == null) {
node.setPriorNode(firstNode);
firstNode.setNextNode(node);
return this;
}
while (currentNode.getNextNode() != null) {
currentNode = currentNode.getNextNode();
}
currentNode.setNextNode(node);
currentNode.getPriorNode().setNextNode(currentNode);
node.setPriorNode(currentNode);
return this;
}
/**
* 在指定位置添加节点
*
* @param node
* @param position
* @return
*/
public boolean add(Node node, int position) {
int count = 0;
Node currentNode = this.firstNode;
if (position == 1) {
if (firstNode.getNextNode() == null) {
node.setPriorNode(firstNode);
firstNode.setNextNode(node);
return true;
}
node.setNextNode(firstNode.getNextNode());
node.setPriorNode(firstNode);
firstNode.setNextNode(node);
return true;
}
while (count < position) {
if (currentNode.getNextNode() == null) {
if (count == position - 1) {
add(node);
return true;
}
return false;
}
currentNode = currentNode.getNextNode();
count++;
}
currentNode.getPriorNode().setNextNode(node);
node.setPriorNode(currentNode.getPriorNode());
node.setNextNode(currentNode);
currentNode.setPriorNode(node);
node.setPriorNode(currentNode);
return true;
}
/**
* 移除指定位置的node
*
* @param position
* @return
*/
public boolean remove(int position) {
Node canditate = find(position);
if (canditate != null && canditate.getNextNode() != null) {
canditate.getPriorNode().setNextNode(canditate.getNextNode());
canditate.getNextNode().setPriorNode(canditate.getPriorNode());
} else if (canditate != null && canditate.getNextNode() == null) {
canditate.getPriorNode().setNextNode(null);
} else {
return false;
}
return true;
}
/**
* 根据指定位置查找node
*
* @param position
* @return
*/
public Node find(int position) {
int count = 0;
Node node = this.firstNode;
while (count < position) {
if (node.getNextNode() != null) {
node = node.getNextNode();
}
if (node.getNextNode() == null && count < position - 1) {
return null;
}
count++;
}
return node;
}
/**
* 根据位置更新node信息
*
* @param newvalue
* @param position
* @return
*/
public boolean update(String newvalue, int position) {
Node canditate = find(position);
if (canditate != null) {
canditate.setNodevalue(newvalue);
} else {
return false;
}
return true;
}
}
package projectlinklist;
/**
*实体类
*nodevalue为节点的值
*nextNode为下一个节点
*/
public class Node {
private Node priorNode;
public Node getPriorNode() {
return priorNode;
}
public void setPriorNode(Node priorNode) {
this.priorNode = priorNode;
}
private String nodevalue;
private Node nextNode;
public String getNodevalue() {
return nodevalue;
}
public Node(String nodevalue) {
super();
this.nodevalue = nodevalue;
}
public Node() {
super();
}
public void setNodevalue(String nodevalue) {
this.nodevalue = nodevalue;
}
public Node getNextNode() {
return nextNode;
}
public void setNextNode(Node nextNode) {
this.nextNode = nextNode;
}
}
标签:node 更新 tno turn 头结点 rem new on() and
原文地址:http://www.cnblogs.com/Phoenisy/p/6551384.html