stack 栈

package javacore;
/**
 * @author baoyou  E-mail:[email protected]
 * @version 创建时间:2015年9月10日 下午2:23:04 
 * des:
 */
public class Stack {
  
	class Node {
        int data;
        Node pre;   

        public Node(int data) {
            this.data = data;
        }
    }
	
	transient  Node head;
	transient  Node current;
     
    public void push(int data) {
        if (head == null) {
            head = new Node(data);
            current = head;
        } else {
            Node node = new Node(data);
            node.pre = current; 
            current = node;  
        }
    }

    public Node pop() {
        if (current == null) {
            return null;
        }

        Node node = current; 
        current = current.pre;   
        return node;
    }

    public static void main(String[] args) {
		Stack stack = new Stack ();
		stack .push(1);
		stack .push(2);
		stack .push(3);
		System.out.println(stack.pop().data);
		System.out.println(stack.pop().data);
		System.out.println(stack.pop().data);
	}
 
}

 

 

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。



 
 
 谢谢您的赞助,我会做的更好!

猜你喜欢

转载自knight-black-bob.iteye.com/blog/2242390