java专题——单向链表(逆序)

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);
        
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值