首页 > 技术文章 > Java手写数组栈

singworld 2019-11-06 09:09 原文

public class ArrayStack{
	private String[] items;  //数组
	private int count;  //栈内元素
	private int n; 	//栈大小

	//初始化
	public ArrayStack(int n){
		this.items = new String[n];
		this.n = n;
		this.count = 0;

		}

	//入栈
	public boolean push(String item){
		
			if(n==count) return false;
			items[count] = item;
			count++;
			return true;
		}


	//出栈
	public String pop(){
		
			if(count == 0) return null;
			String tmp = items[count-1];
			count--;
			return tmp;
		}


	}

推荐阅读