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

 

 
stack 栈_第1张图片
 

 

 

 

 

 

你可能感兴趣的:(stack 栈)