---
title: "堆栈浅析"
author: "sunwch"
date: "2010-05-18T14:48:03Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2010-05-18T14:48:03Z"
id: 668973
url: "https://sunwch.iteye.com/blog/668973"
---


public class Stack {

	long[] stackArr;
	int nEleme;
	int top;
	
	// initialization
	public Stack(int n){
		this.nEleme = n;
		stackArr = new long[this.nEleme];
		top = -1;
	}
	
	// isEmpty
	public boolean isEmpty(){
		return (top == -1) ? true : false;
	}
	
	// push
	public void push(int value){
		if(isFull())
			return;
		stackArr[++top] = value;
	}
	
	// pop
	public long pop(){
		if(top < 0)
			return 0L;
		return stackArr[top--];
	}
	
	public int getSize(){
		return nEleme;
	}
	
	public boolean isFull(){
		return (top == nEleme) ? true : false;
	}
	
	public static void main(String[] args){
		Stack sk = new Stack(5);
		sk.push(10);
		sk.push(20);
		sk.push(30);
		sk.push(40);
		sk.push(50);
		System.out.println(sk.isEmpty() + "\tSize=" + sk.getSize());
		int size = sk.getSize();
		for(int i=0;i<size;i++){
			System.out.print(sk.pop() + "\t");
		}
	}
}
 其为单通道数据输出，顺序为后进先出（LIFO）