package 单链表;
public class LinkProcess {
/**
* 节点类
* @author Administrator
*
* @description
*
* 20142014年7月28日上午9:14:05
*
*/
private static Node root;
static class Node{
int info;
Node next;
Node(int info){
this.info = info;
}
/**
* 删除
* @param pre
* @param info
*/
public void delete(Node pre,int info){
if(this.info == info){
//定位到所需点
pre.next = this.next;
}else{
//继续递归,直到找到该点
if(this.next != null){
this.next.delete(this, info);
}
}
}
/**
* 搜索
* @param info
* @return
*/
public boolean search(int info){
if(this.info == info){
return true;
}else{
if(this.next != null){
return this.next.search(info);
}else{
return false;
}
}
}
}
/**
* 增加节点
* @param info
* @return
*/
private static Node addNode(int info){
if(null == root){
return new Node(info);
}else{
Node tmp = root;
while(tmp.next != null){
tmp = tmp.next;
}
tmp.next = new Node(info);
}
return root;
}
/**
* 打印序列
* @param root
*/
private static void printNode(Node root){
if(null == root){
System.out.println("NULL");
}
Node tmp = root;
while(tmp != null){
System.out.println(tmp.info);
tmp = tmp.next;
}
}
/**
* 反转序列
* @param root
*/
private Node reverseNode(Node root){
if(null == root){
return null;
}
Node p_pre = root;
Node p_cur = root.next;
p_pre.next = null;
while(p_cur != null){
Node p_next = p_cur.next;
p_cur.next = p_pre;
p_pre = p_cur;
p_cur = p_next;
}
return p_pre;
}
/**
* 删除节点
* @param info
*/
private static void deleteNode(int info){
if(root.info == info){
//删除节点为根节点
if(null != root.next){
root = root.next;
}else{
root = null;
}
}else{
//删除节点为非根节点
root.next.delete(root, info);
}
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
root = addNode(0);
addNode(1);
addNode(2);
printNode(root);
deleteNode(1);
printNode(root);
}
}