用链表实现的栈(单向链表,表头插入和删除)

package com.hebtu.java.list;

/**
 * 用链表实现的栈:单向链表,表头插入、表头删除
 * @author Xmh
 *
 */
public class LinkStack {
	private LinkNodeList linkNodeList;
	
	/**
	 * 构造器:初始化栈时,创建存放数据的链表。
	 */
	public LinkStack() {
		linkNodeList = new LinkNodeList();
	}
	
	/**
	 * 压栈操作
	 */
	public void push(int num){
		linkNodeList.insert(num);
	}
	
	/**
	 * 出栈
	 * @return
	 */
	public int pop(){
		LinkNode removeFirst = linkNodeList.removeFirst();
		return removeFirst.getNum();
	}
	
	/**
	 * 判断栈是否为空
	 * @return
	 */
	public boolean isEmpty(){
		return (linkNodeList.isEmpty());
	}
	
	/**
	 * display the stack
	 */
	public void displayStack(){
		System.out.println("LinkStack, top--->bottom:");
		linkNodeList.displayList();
	}
	
	public LinkNodeList getLinkNodeList() {
		return linkNodeList;
	}

	public void setLinkNodeList(LinkNodeList linkNodeList) {
		this.linkNodeList = linkNodeList;
	}
	
	public static void main(String[] args) {
		LinkStack linkStack = new LinkStack();
		// 压栈
		linkStack.push(1);
		linkStack.push(3);
		linkStack.push(5);
		linkStack.push(7);
		linkStack.push(9);
		linkStack.displayStack();
		System.out.println("");
		System.out.println("------------");
		
		// 出栈
		while(!linkStack.isEmpty()){
			int pop = linkStack.pop();
			System.out.print(pop + "  ");
		}
	}
}

/**
 * 节点类
 *
 */
class LinkNode{
	private int num;
	private LinkNode next;
	public LinkNode() {
		super();
	}
	
	public LinkNode(int num) {
		this.num = num;
	}
	
	/**
	 * 打印节点
	 */
	public void displayLink(){
		System.out.print(num + "  ");
	}
	
	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

	public LinkNode getNext() {
		return next;
	}

	public void setNext(LinkNode next) {
		this.next = next;
	}
	
	
}

/**
 * 对节点进行操作的类:比如,向节点中插入数据,删除数据,查找数据等
 * @author Administrator
 *
 */
class LinkNodeList{
	private LinkNode first;
	
	/**
	 * 构造器:初始化空链表,让 first引用指向 null(默认就是指向 null 的)
	 */
	public LinkNodeList() {
		first = null;
	}
	
	/**
	 * 往链表中插入数据:只在表头插入
	 * @param num
	 */
	public void insert(int num){
		// 1.先创建一个节点对象
		LinkNode newLink = new LinkNode(num);
		// 2.1 新对象 的 next 引用,指向 first
		newLink.setNext(first);
		// 2.2 让first指向新插入的对象
		first = newLink;
	}
	
	/**
	 * 从链表中移除头结点,并且返回之。
	 * @return
	 */
	public LinkNode removeFirst(){
		LinkNode temp = first; //首先保存第一个节点的引用,即要输出的节点
		// first--->old next(将first引用指向下下个节点对象)
		first = first.getNext();
		return temp;
	}
	
	/**
	 * 根据key值查找节点
	 * @param key
	 * @return
	 */
	public LinkNode find(int key){
		LinkNode current = first; //开始于 first
		//链表中当前节点的值不匹配 key
		while(current.getNum() != key){
			//如果下一个节点是 null,说明没有匹配到
			if(current.getNext() == null){
				return null;
			}else{ //继续找下一个节点
				current = current.getNext();
			}
		}
		//返回找到的节点
		return current;
	}
	
	/**
	 * 删除指定的值的节点
	 * @param key
	 * @return
	 */
	public LinkNode delete(int key){
		LinkNode current = first; //链表中当前节点
		LinkNode previous = first; //保存第前一个节点
		// 1.先寻找节点
		while(current.getNum() != key){
			if(current.getNext() == null){
				return null;
			}else{
				previous = current; //指向下一个节点
				current = current.getNext();
			}
		}
		// 2.找到之后,删除
		// 2.1 如果是头结点
		if(current == first){
			first = first.getNext();
		}else {
			// 2.2 如果不是头结点			
			previous.setNext(current.getNext()); //pass 当前节点
		}
		//返回删除的节点
		return current;
	}
	
	/**
	 * 打印链表
	 */
	public void displayList(){		
		LinkNode current = first; // 链表的开始节点引用
		while(current != null){
			current.displayLink();
			//指向下一个节点
			current = current.getNext();
		}
	}
	
	/**
	 * 判断链表是否为空
	 * @return
	 */
	public boolean isEmpty(){
		if(first==null){
			return true;
		}else{
			return false;
		}
	}
	
}

输出结果:

LinkStack, top--->bottom:
9  7  5  3  1  
------------
9  7  5  3  1 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值